Selenium Actions 还是 Java AWT Robot?
Selenium Actions or Java AWT Robot?
到目前为止,我一直在使用 Selenium Actions 库在我们的自动化项目中执行 mouse/keyboard 操作。
最近,我发现了 Java AWT 机器人 class。它与 Selenium Actions 库有何可比性?其中一个是否有一些极端情况由另一个解决?限制?稳定?性能注意事项?
这些工具的工作方式存在巨大差异。 Selenium
使用 WebDriver API 并向浏览器发送命令以执行操作(通过 "JSON wire protocol")。
Java AWT Robot 使用本机系统事件来控制鼠标和键盘。
如果您正在做浏览器自动化,理想情况下,您永远不要使用 Robot
之类的东西,因为通常 selenium 提供的功能绰绰有余。但是,有些情况下会打开浏览器或本机 OS 弹出窗口,例如 upload/download 文件 - 这可以 also 解决使用 Robot - 虽然通常有 selenium 特定的 solutions/workarounds 可以帮助避免使用 Robot
。这些解决方法的关键思想是 "since we cannot control the popups, just don't let them to be opened"。
例如,当您在 Firefox 中下载文件时,您会收到一个文件浏览器弹出窗口,建议您选择位置和文件名。这是您无法使用 selenium
操作的东西。但是,你可以做的是让 Firefox 知道哪些文件类型以及你想自动保存下载的位置,而不显示弹出窗口。参见 Access to file download dialog in Firefox。
相关主题:
- Java AWT Robot | Selenium Uses
- Selenium WebDriver and HTML Window location by using Java
- One solution for File Upload using Java Robot API with Selenium WebDriver by Java
- Use of Robot Class In Selenium WebDriver For Automation Purpose
机器人Class
Robot Class is defined in java.awt package within java.desktop 模块。此 class 用于处理与 Test Automation 相关的本机系统输入事件,其中控制 Mouse 和 [=48=需要]键盘。 Robot Class 的主要目的是促进 自动化测试 Java 平台实施。使用 机器人 Class 生成输入事件不同于将事件发布到 Java AWT 事件队列 或 AWT 组件 as using Robot Class 事件在平台的本机输入队列中生成。例如 Robot.mouseMove
将实际移动鼠标光标而不是仅仅生成 鼠标移动事件.
此时值得一提的是,某些平台需要特殊权限或扩展才能访问 low-level 输入控件。如果当前平台配置不允许输入控制,则在尝试构造 Robot 对象时将抛出 AWTException。例如,如果 X-Window systems 不支持(或未启用)XTEST 2.2 标准扩展 ,则将抛出异常 X服务器.
一个例子:
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);
操作数 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. 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。 Test Automation 通过 Selenium 您可以使用此 class 而不是直接使用键盘或鼠标。 Actions Class 实现了 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)
一个例子:
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();
我个人更喜欢 class 操作来执行任何鼠标或键盘事件。
如果在某些环境中使用动作 class 存在技术故障,那么我们可以使用 Robot class.
到目前为止,我一直在使用 Selenium Actions 库在我们的自动化项目中执行 mouse/keyboard 操作。
最近,我发现了 Java AWT 机器人 class。它与 Selenium Actions 库有何可比性?其中一个是否有一些极端情况由另一个解决?限制?稳定?性能注意事项?
这些工具的工作方式存在巨大差异。 Selenium
使用 WebDriver API 并向浏览器发送命令以执行操作(通过 "JSON wire protocol")。
Java AWT Robot 使用本机系统事件来控制鼠标和键盘。
如果您正在做浏览器自动化,理想情况下,您永远不要使用 Robot
之类的东西,因为通常 selenium 提供的功能绰绰有余。但是,有些情况下会打开浏览器或本机 OS 弹出窗口,例如 upload/download 文件 - 这可以 also 解决使用 Robot - 虽然通常有 selenium 特定的 solutions/workarounds 可以帮助避免使用 Robot
。这些解决方法的关键思想是 "since we cannot control the popups, just don't let them to be opened"。
例如,当您在 Firefox 中下载文件时,您会收到一个文件浏览器弹出窗口,建议您选择位置和文件名。这是您无法使用 selenium
操作的东西。但是,你可以做的是让 Firefox 知道哪些文件类型以及你想自动保存下载的位置,而不显示弹出窗口。参见 Access to file download dialog in Firefox。
相关主题:
- Java AWT Robot | Selenium Uses
- Selenium WebDriver and HTML Window location by using Java
- One solution for File Upload using Java Robot API with Selenium WebDriver by Java
- Use of Robot Class In Selenium WebDriver For Automation Purpose
机器人Class
Robot Class is defined in java.awt package within java.desktop 模块。此 class 用于处理与 Test Automation 相关的本机系统输入事件,其中控制 Mouse 和 [=48=需要]键盘。 Robot Class 的主要目的是促进 自动化测试 Java 平台实施。使用 机器人 Class 生成输入事件不同于将事件发布到 Java AWT 事件队列 或 AWT 组件 as using Robot Class 事件在平台的本机输入队列中生成。例如 Robot.mouseMove
将实际移动鼠标光标而不是仅仅生成 鼠标移动事件.
此时值得一提的是,某些平台需要特殊权限或扩展才能访问 low-level 输入控件。如果当前平台配置不允许输入控制,则在尝试构造 Robot 对象时将抛出 AWTException。例如,如果 X-Window systems 不支持(或未启用)XTEST 2.2 标准扩展 ,则将抛出异常 X服务器.
一个例子:
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);
操作数 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. 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。 Test Automation 通过 Selenium 您可以使用此 class 而不是直接使用键盘或鼠标。 Actions Class 实现了 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)
一个例子:
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();
我个人更喜欢 class 操作来执行任何鼠标或键盘事件。 如果在某些环境中使用动作 class 存在技术故障,那么我们可以使用 Robot class.