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 with a gap of of 500 mili seconds is added after every key press
robot.keyPress(KeyEvent.VK_S);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_T);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_A);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_S);
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. The Actions class allow you to build a Chain of Actions and perform them which is based on the WebDriver API following the W3C Specification. 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 :
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();