Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I was going through the selenium learning and when I was exploring interaction with keyboard and mouse topic, I found this code. With the help of Robot class, perform Enter :

Robot r=new Robot();

r.keyPress(KeyEvent.VK_ENTER);

With the help of Actions class, perform Enter :

Actions action = new Actions(driver); 

action.sendKeys(Keys.ENTER).build().perform();

Why do we need both the class to perform the same actions? What is the difference between the Robot class and Actions class? 

1 Answer

0 votes
by (62.9k points)

Robot Class

Robot Class is defined in java.awt package within java.desktop module. This class is used to deal with the native system input events associated with Test Automation where control over the Mouse and Keyboard is needed. The primary purpose of Robot Class is to facilitate Automated Testing of Java platform implementations. Using Robot Class to generate input events differs from posting events to the Java AWT event queue or AWT components as using Robot Class events are generated in the platform's native input queue. As an example, Robot.mouseMove will actually move the mouse cursor instead of just generating Mouse Move Event.

At this point it is worth to mention, some platforms requires special privileges or extensions to access low-level input control. If the current platform configuration does not allow input control, an AWTException will be thrown when trying to construct Robot objects. For example, X-Window systems will throw the exception if the XTEST 2.2 standard extension is not supported (or not enabled) by the X server.

An Example :

Robot robot = new Robot();

// Press keys using robot. A gap of of 500 mili seconds is added after every key press

robot.keyPress(KeyEvent.VK_R);

Thread.sleep(500);

robot.keyPress(KeyEvent.VK_U);

Thread.sleep(500);

robot.keyPress(KeyEvent.VK_P);

Thread.sleep(500);

robot.keyPress(KeyEvent.VK_A);

Thread.sleep(500);

robot.keyPress(KeyEvent.VK_L);

Thread.sleep(500);

robot.keyPress(KeyEvent.VK_I);

Actions Class

Actions Class is defined in org.openqa.selenium.interactions package and is the User-Facing API for emulating complex user gestures when using Selenium. While Test Automation through Selenium you can use this class rather than using the Keyboard or Mouse directly. Actions Class implements the Builder Pattern which can build a CompositeAction containing all actions specified by the below mentioned method calls :

  • build()

  • click(WebElement target)

  • clickAndHold(WebElement target)

  • contextClick(WebElement target)

  • doubleClick(WebElement target)

  • dragAndDrop(WebElement source, WebElement target)

  • moveToElement(WebElement target, int xOffset, int yOffset)

  • perform()

  • sendKeys(WebElement target, java.lang.CharSequence... keys)

An Example :

Actions act = new Actions(driver);

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement electronics = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li/a[@href='/electronics']")));

act.moveToElement(electronics).perform();

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s automation testing training

Browse Categories

...