Selenium Actions 还是 Java AWT Robot?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28005007/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:47:58  来源:igfitidea点击:

Selenium Actions or Java AWT Robot?

javaseleniumselenium-webdriverawtrobot

提问by Johnny

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. 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?

最近,我发现了 Java AWT Robot 类。它与 Selenium Actions 库相比如何?其中一个是否有其他解决的极端情况?限制?稳定?性能考虑?

回答by alecxe

There is a huge difference in terms of how do these tools work. Seleniumuses 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 Robotsince 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 alsosolved 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".

如果您正在执行浏览器自动化,理想情况下,您永远不要使用类似的东西,Robot因为通常 selenium 提供的功能已经绰绰有余。虽然,有些时候没有打开,例如浏览器或本地OS弹出,上传/下载文件-这一点是可以解决了机器人-虽然通常有特定的硒的解决方案/变通方法,可以帮助避免使用Robot. 这些解决方法的关键思想是“因为我们无法控制弹出窗口,所以不要让它们被打开”。

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.

例如,当您在 Firefox 中下载文件时,您会看到一个文件浏览器弹出窗口,建议您选择位置和文件名。这是您无法通过使用来操纵的东西selenium。但是,您可以做的是让 Firefox 知道您想要自动保存下载的文件类型和位置,而不显示弹出窗口。请参阅在 Firefox 中访问文件下载对话框

Related topics:

相关话题:

回答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.mouseMovewill 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 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 Classis defined in org.openqa.selenium.interactionspackage and is the User-Facing APIfor emulating complex user gestures when using Selenium. The Actions class allow you to build a Chain of Actionsand perform them which is based on the WebDriver APIfollowing the W3C Specification. 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 类允许您构建一个动作链并执行它们,它基于遵循W3C 规范WebDriver API。虽然测试自动化通过,你可以使用这个类,而不是直接使用键盘或鼠标。Actions 类实现了可以构建CompositeActionBuilder Pattern包含由下面提到的方法调用指定的所有操作:

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 Madhusudan

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 类。