Why do we need Robot class when we have Actions cl

2020-04-14 19:05发布

问题:

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 same actions? What is the difference between Robot class and Actions class? TIA.

回答1:

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


回答2:

The Main Difference is Actions class simulates with mouse and keyboard and Robot class enables the actual mouse and keyboard so you can see the movement of the mouse cursor. For more Details please Click on this Link -> Robot class in Selenium