java 为什么我们在 selenium 中有 Actions 类时需要 Robot 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49459040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do we need Robot class when we have Actions class in selenium
提问by Rupali
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 类的帮助下,执行 Enter :
Robot r=new Robot();
r.keyPress(KeyEvent.VK_ENTER);
With the help of Actions class,perform Enter :
在 Actions 类的帮助下,执行 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.
为什么我们需要两个类来执行相同的操作?Robot 类和 Actions 类有什么区别?TIA。
回答by DebanjanB
Robot Class
机器人班
Robot Classis defined in java.awtpackage within java.desktopmodule. This class is used to deal with the native system input events associated with Test Automationwhere control over the Mouseand Keyboardis needed. The primary purpose of Robot Classis to facilitate Automated Testingof Java platform implementations. Using Robot Classto generate input events differs from posting events to the Java AWT event queueor AWT componentsas using Robot Classevents 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.
机器人类在java.desktop模块内的java.awt包中定义。此类用于处理与测试自动化相关的本机系统输入事件,其中需要控制鼠标和键盘。Robot Class的主要目的是促进Java 平台实现的自动化测试。使用机器人类生成输入事件不同于将事件发布到Java AWT 事件队列或AWT 组件,因为使用机器人类事件是在平台的本机输入队列中生成的。举个例子Robot.mouseMove
实际上会移动鼠标光标,而不仅仅是生成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 AWTExceptionwill be thrown when trying to construct Robot objects. For example, X-Window systemswill throw the exception if the XTEST 2.2 standard extensionis not supported (or not enabled) by the X server.
此时值得一提的是,某些平台需要特殊权限或扩展才能访问低级输入控件。如果当前平台配置不允许输入控制,则在尝试构造 Robot 对象时将抛出AWTException。例如,如果X 服务器不支持(或未启用)XTEST 2.2 标准扩展,则X-Window 系统将抛出异常。
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 Classis defined in org.openqa.selenium.interactionspackage and is the User-Facing APIfor emulating complex user gestures when using Selenium. While Test Automationthrough Seleniumyou can use this class rather than using the Keyboard or Mouse directly. Actions Classimplements the Builder Patternwhich can build a CompositeActioncontaining all actions specified by the below mentioned method calls :
Actions 类在org.openqa.selenium.interactions包中定义,是User-Facing API,用于在使用Selenium时模拟复杂的用户手势。虽然测试自动化通过硒,你可以使用这个类,而不是直接使用键盘或鼠标。Actions 类实现了Builder Pattern,它可以构建一个CompositeAction包含由下面提到的方法调用指定的所有操作:
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)
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();
回答by sandeep shewale
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
主要区别在于 Actions 类使用鼠标和键盘进行模拟,而 Robot 类启用实际的鼠标和键盘,因此您可以看到鼠标光标的移动。有关更多详细信息,请单击此链接 -> Selenium 中的机器人类