简体   繁体   English

Selenium Actions还是Java AWT Robot?

[英]Selenium Actions or Java AWT Robot?

Until now I have used the Selenium Actions library in order to perform mouse/keyboard actions in our automation project. 到目前为止,我已经使用了Selenium Actions库来在我们的自动化项目中执行鼠标/键盘操作。

Recently, I have discovered the Java AWT Robot class. 最近,我发现了Java AWT Robot类。 How is it comparable to Selenium Actions library? 它与Selenium Actions库相比如何? Is there some corner-cases in one of them that the other solve? 其中一个中有一些角落案例是另一个解决了吗? restrictions? 限制吗? stability? 稳定性? performance considerations? 性能考虑?

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"). Selenium使用WebDriver API并向浏览器发送命令以执行操作(通过“JSON有线协议”)。

Java AWT Robot uses native system events to control the mouse and keyboard. Java AWT Robot使用本机系统事件来控制鼠标和键盘。

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. 如果您正在进行浏览器自动化,理想情况下,您不会使用像Robot这样的东西,因为通常selenium提供的功能绰绰有余。 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 . 虽然,有些时候有一个浏览器或本地OS弹出打开,例如,上传/下载文件-这是什么, 可以解决了机器人-虽然通常有特定的硒的解决方案/解决方法,可以帮助避免使用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. 例如,当您在Firefox中下载文件时,您将获得一个文件浏览器弹出窗口,建议您选择位置和文件名。 This is something you cannot manipulate with using selenium . 这是使用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. 但是,你可以做的是让Firefox知道哪些文件类型以及你想在哪里自动保存下载,而不显示弹出窗口。 See Access to file download dialog in Firefox . 请参阅Firefox中的“访问文件下载”对话框

Related topics: 相关话题:

Robot Class 机器人类

Robot Class is defined in java.awt package within java.desktop module. Robot类java.desktop模块的java.awt包中定义。 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. 此类用于处理与Test Automation关联的本机系统输入事件,其中需要控制鼠标键盘 The primary purpose of Robot Class is to facilitate Automated Testing of Java platform implementations. Robot Class的主要目的是促进Java平台实现的自动化测试 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. 使用Robot Class生成输入事件不同于将事件发布到Java AWT事件队列AWT组件,因为在平台的本机输入队列中使用Robot类事件生成。 As an example Robot.mouseMove will actually move the mouse cursor instead of just generating Mouse Move Event . 作为示例, Robot.mouseMove实际上将移动鼠标光标而不是仅生成鼠标移动事件

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. 如果当前平台配置不允许输入控制,则在尝试构造Robot对象时将抛出AWTException 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 . 例如,如果X服务器不支持(或未启用) XTEST 2.2标准扩展 ,则X-Window系统将抛出异常。

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 . Actions类org.openqa.selenium.interactions包中定义,是用于在使用Selenium时模拟复杂用户手势的User-Facing API 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 . Actions类允许您构建一个动作链并根据W3C规范执行基于WebDriver API 的动作链 While Test Automation through Selenium you can use this class rather than using the Keyboard or Mouse directly. 通过Selenium 测试自动化时 ,您可以使用此类而不是直接使用键盘或鼠标。 Actions Class implements the Builder Pattern which can build a CompositeAction containing all actions specified by the below mentioned method calls : Actions类实现了Builder Pattern ,它可以构建一个CompositeAction,它包含下面提到的方法调用指定的所有动作:

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

I personally prefer the actions class to do any mouse or keyboard events. 我个人更喜欢动作类来做任何鼠标或键盘事件。 If there is a technical glitch using actions class in certain environments then we can use Robot class. 如果在某些环境中使用动作类存在技术故障,那么我们可以使用Robot类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM