Selenium Actions or Java AWT Robot?

2019-01-26 14:44发布

问题:

Until now I have used the Selenium Actions library in order to perform mouse/keyboard actions in our automation project.

Recently, I have discovered the Java AWT Robot class. How is it comparable to Selenium Actions library? Is there some corner-cases in one of them that the other solve? restrictions? stability? performance considerations?

回答1:

There is a huge difference in terms of how do these tools work. Selenium uses the WebDriver API and sends commands to a browser to perform actions (through the "JSON wire protocol").

Java AWT Robot uses native system events to control the mouse and keyboard.

If you are doing browser automation, ideally, you don't ever use things like Robot since usually the functionality provided by selenium is more than enough. Though, there are cases when there is a browser or native OS popup opened, for example, to upload/download a file - this is something that can be also solved with Robot - though usually there are selenium-specific solutions/workarounds that can help avoiding using Robot. The key idea of these workarounds is "since we cannot control the popups, just don't let them to be opened".

For example, when you download a file in Firefox, you are getting a file browser popup suggesting you to choose a location and filename. This is something you cannot manipulate with using selenium. But, what you can do , is let Firefox know which file types and where do you want to save downloads automatically, without showing the popup. See Access to file download dialog in Firefox.

Related topics:

  • Java AWT Robot | Selenium Uses
  • Selenium WebDriver and HTML Window location by using Java
  • One solution for File Upload using Java Robot API with Selenium WebDriver by Java
  • Use of Robot Class In Selenium WebDriver For Automation Purpose


回答2:

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 :

  • 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();