Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

java - Why do we need Robot class when we have Actions class in selenium

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m 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 :

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...