How to handle Firefox browser confirmation messages in Test Automation? - java

I am currently working on the selenium+java test automation project.
I have encountered a problem that while running many test classes together in testng.xml, firefox display
confirm resubmission popup. when I am running a single class this message does not popup. This confirmation popup comes when the last set of classes begins to run. How can I resolve this problem?

May be, you can try to simulate pressing Enter key or Spacebar and see if that works.
import java.awt.Robot;
import java.awt.event.KeyEvent;
public void pressEsc(WebDriver driver) {
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
} catch (Exception e) {
e.printStackTrace(); }
}

Related

Java.awt.Robot keyPress and keyRelease not working at all

Whenever I try using the java.awt.Robot keyPress or keyRelease, it gives me the error message pid(25807)/euid(501) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!!. No matter how simple I make the code, it keeps giving this error message. This is my code:
package com;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
public class JavaRobotExample {
public static void main(String[] args) throws AWTException {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
}
}
This is the textbook example I found, yet it still gives that error message. I've been searching for so long trying to figure out what is wrong. I use MacOS Mojave 10.14.2 on a MacBook Pro, and I have given Eclipse (what I use to compile the code) privacy access. Here is a screenshot of everything I use for this:
My code
Does anyone else have this issue? I should also note that the error message is sent multiple times, even though it only presses the key once.
Follow the steps below,
Settings -> Security & Privacy
Scroll down to "Accessibility"
Click the lock button at the bottom and unlock the security and
privacy preferences
Click on '+' icon and your program (Eclipse IDE in my case) and try
running again
The Eclipse IDE still shows "This is NOT allowed. Please call TIS/TSM in main thread!!!" but both Keyboard and Mouse events work.

How to use Ctrl+Shift+R in selenium webdriver

Ctrl+Shift+R is the shortcut key for a hard reload and refresh. How can I apply this shortcut in selenium Java?
I tried with following code but could not get the results.
String selectAll = Keys.chord(Keys.CONTROL,Keys.SHIFT,"r");
driver.findElement(By.tagName("html")).sendKeys(selectAll);
I also tried with action class. But it is not working.
PS: No errors were displayed. But, it does not perform the action and Ctrl+A is working.
To refresh and reload WebPage you can simulate the usage of Ctrl+Shift+R through:
driver.navigate().refresh();
You can try Robot class as an alternative to perform Ctrl+Shift+R
import below packages :
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
And use below code :
Robot robot = new Robot();
// press key Ctrl+Shift+r
robot.keyPress(KeyEvent.VK_CONTROL);
robot.delay(100);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.delay(100);
robot.keyPress(KeyEvent.VK_R);
// relase key Ctrl+Shift+r
robot.delay(100);
robot.keyRelease(KeyEvent.VK_R);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_CONTROL);
Important Note: Don't forget to release all the key (keyRelease) what you have used in keyPress to perform your action using Robot class, else you may observe unexpected keyboard behavior.

selenium chrome driver select certificate popup confirmation not working

I am automating tests using selenium chromewebdriver 3.7. Whenever I lauch the site, I get a certificate selection popup like the one below
However I am not able to click on the OK button. These are the options I have tried
//I have tried getWindowHandle like this
String handle= driver.getWindowHandle();
this.driver.switchTo().window(handle);
//I have alos tried switching and accept
driver.switchTo().alert().accept();
//I have also tried to force the enter key like this
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
// I also tried this way
Scanner keyboard = new Scanner(System.in);
keyboard.nextLine();
All my trials have failed. How can I click on OK on this popup window?
This is the closest solution I found which is not working Link here
I also had problems with accepting the warning for using a signed certificate. The solution of #eskoba worked like a charm. The functions are NOT final, because I let the enter button press for 10 times. I made this, because the webdriver needs a long time until it actually calls the url. In the meantime he starts pressing already.
In Python:
def threaded_function():
#Calls the website
browser.get(url)
def threaded_function2():
#Presses 10 times
for i in range(0,10):
pyautogui.press('enter')
#Calling the website and pressing 10 times in the same time
thread2 = Thread(target = threaded_function2)
thread2.start()
thread = Thread(target = threaded_function)
thread.start()
If still actual, I had same issue on Mac, and solution was simple:
for chrome is set AutoSelectCertificateForUrls policy like that:
defaults write com.google.Chrome AutoSelectCertificateForUrls -array-add -string '{"pattern":"[*.]example.com","filter":{"ISSUER":{"CN":"**cert issuer**"}, "SUBJECT":{"CN": "**cert name**"}}}'
for safari:
security set-identity-preference -c "**cert name**" -s "**example.com**"
then use it in code like
subprocess.call() in python
I had the same problem and I was able to solve it by using the robot, creating function for the url and passing it to a different thread.
Runnable mlauncher = () -> {
try {
driver.get(url);
} catch (Exception e) {
e.printStackTrace();
}
};
public void myfunction {
try {
Thread mthread = new Thread(mlauncher);
mthread.start
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
} catch (Exception e) {
e.printStackTrace();
}
One suggestion would be, use Sikuli to click on OK button in the certificate.
Steps:
Take screenshot of OK button and save it.
Download sikuli-script.jar and add it to Project's Build path.
Take a screenshot of the UI Element to be clicked and save it locally.
Add the following code to the test case.
Screen s=new Screen();
s.click(“image name”);
Other functions Sikuli provides can be found here.
You can also skip being prompted when a certificate is missing, invalid, or self-signed.
You would need to set acceptInsecureCerts in DesiredCapabilities and pass that when you create a driver instance.
for example, in Python:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.CHROME.copy()
caps['acceptInsecureCerts'] = True
driver = webdriver.Chrome(desired_capabilities=caps)

Java Webdriver: How to save the page same as "save page as" in firefox?

Similar question asked below
How to save complete web page
But there is no answer yet. The expected result is to get many files, some file to store image, etc.
I used the following,it will pop up a windows saying to save the file
val a=new FirefoxDriver()
a.get("http://www.baidu.com")
val b=new Actions(a)
b.action.keyDown(Keys.ALT).keyDown(Keys.F4).keyUp(Keys.ALT).perform();
But then how to click the save button? The following doesn't work
b.sendKeys(Keys.ENTER)
We can use Robot utility in Java to handle this:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
Robot robot = new Robot();
// press Ctrl+S the Robot's way
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_S);
Thread.sleep(2000L);
// press Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
To use Robot utility you have to import following Java utilities:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
When you use Robot utility the x axis and y axis has to be metioned appropriately however it will differ system to system instead we can use AUTO IT.
You can refer here http://www.autoitscript.com/forum/forum/9-example-scripts/

One solution for File Upload using Java Robot API with Selenium WebDriver by Java

I saw that lots of people have Problems uploading a file in a test Environment with Selenium WebDriver. I use the selenium WebDriver and java, and had the same problem. I finally have found a solution, so i will post it here hoping that it helps someone else.
When i need to upload a file in a test, i click with Webdriver in the button and wait for the window "Open" to pop. And then i copy the path to the file in the clipboard and then paste it in the "open" window and click "Enter". This is working because when the window "open" pops up, the focus is always in the "open" button.
You will need these classes and method:
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
public static void setClipboardData(String string) {
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
And that is what i do, just after opening the "open" window:
setClipboardData("C:\\path to file\\example.jpg");
//native key strokes for CTRL, V and ENTER keys
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
And that´s it. It is working for me, i hope it works for some of you.
Actually, there is an in-built technique for this, too. It should work in all browsers and operating systems.
Selenium 2 (WebDriver) Java example:
// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.xpath("//input[#type='file']"));
fileInput.sendKeys("C:/path/to/file.jpg");
The idea is to directly send the absolute path to the file to an element which you would usually click at to get the modal window - that is <input type='file' /> element.
Thanks Alex,
Java Robot API helped me for uploading file. I was fedup with File Upload using WebDriver. Following is the code I used (Small modification to yours):
Robot robot = new Robot();
robot.delay(1000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(1000);
Thanks Alex! I needed this since I could not get the sendKeys function to work when used via Play framework 2.1 (fluentlenium wrapper). I am testing over Firefox [17.0.7] for Mac and had to make a few mods to get it working. Below is an approximation of the final snippet I used.
val file = new File(PATH_TO_IPA)
val stringSelection: StringSelection = new StringSelection(file.getAbsolutePath)
Toolkit.getDefaultToolkit.getSystemClipboard().setContents(stringSelection, null)
val robot: Robot = new Robot()
// Cmd + Tab is needed since it launches a Java app and the browser looses focus
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_TAB)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_TAB)
robot.delay(500)
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_SHIFT)
robot.keyPress(KeyEvent.VK_G)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_SHIFT)
robot.keyRelease(KeyEvent.VK_G)
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_V)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_V)
robot.keyPress(KeyEvent.VK_ENTER)
robot.keyRelease(KeyEvent.VK_ENTER)
robot.delay(500)
robot.keyPress(KeyEvent.VK_ENTER)
robot.keyRelease(KeyEvent.VK_ENTER)
The switching of application on Mac is much better to do with AppleScript. AppleScript is integrated to system, so it will be always functional and does not depend on order of apps on Cmd+Tab. Your test/app will be less fragile.
https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html
You need only detect you are on mac and has name of the application.
Runtime runtime = Runtime.getRuntime();
String[] args = { "osascript", "-e", "tell app \"Chrome\" to activate" };
Process process = runtime.exec(args);

Categories