This page is hosted for free by zzz.com.ua, if you are owner of this page, you can remove this message and gain access to many additional features by upgrading your hosting to PRO or VIP for just 32.50 UAH.
Do you want to support owner of this site? Click here and donate to his account some amount, he will be able to use it to pay for any of our services, including removing this ad.

Robots helping to automate tests

robot-img

Time from time when you develop automation test you need to do some actions not inside browser window, for example you need to upload file but you can’t do this using

WebElement.sendKeys()

so in this case you could use ROBOT, the example of  robot using view in the code below

StringSelection stringSelection = new StringSelection("file name");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);//put name of your file in a clipboard
Robot robot;
try {
robot = new Robot();
robot.mouseMove(102,157);// moving mouse to point 102,157 in your case it can be other point 
robot.mousePress(KeyEvent.BUTTON1_DOWN_MASK); // click on left mouse key
robot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);// release of mouse key
 // entering of your file name using CTRL + V robot.keyPress(KeyEvent.VK_CONTROL);// click on CTRL button
robot.keyPress(KeyEvent.VK_V);// click on V button
robot.keyRelease(KeyEvent.VK_V);// release of CTRL button
robot.keyRelease(KeyEvent.VK_CONTROL);// release of V button
robot.keyPress(KeyEvent.VK_ENTER);// click on Enter button
robot.keyRelease (KeyEvent.VK_ENTER);// release of Enter button
robot.mouseMove(397,156);
robot.mousePress(KeyEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(KeyEvent.BUTTON1_DOWN_MASK);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease (KeyEvent.VK_ENTER);
} catch (AWTException e) {
Assert.fail("Error while dealing with non-browser window");
}
waitInSeconds(20);
Element success = new Element(By.xpath("//div[contains(@class,'alert alert-success')]"));
if(success.isPresent())
{
System.out.print("Test passed");
}
else
{
Assert.fail("Test not passed");
}

Short about public class Robot (information taken from here)

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.
Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform’s native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events.