本文介绍了当硒中有Actions类时,为什么需要Robot类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在学习硒研究,当我探索与键盘和鼠标主题的交互时,我找到了这段代码。 在Robot类的帮助下,执行输入: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);借助Actions类,执行输入:With the help of Actions class,perform Enter :Actions action = new Actions(driver); action.sendKeys(Keys.ENTER).build().perform();为什么我们需要两个类都执行相同的动作? Robot类和Actions类有什么区别? TIA。Why do we need both the class to perform same actions? What is the difference between Robot class and Actions class? TIA.推荐答案 机器人课程 机器人类 是在 java.awt java中的strong> 包.desktop 模块。该类用于处理与 Test Automation 相关的本机系统输入事件,其中需要控制 Mouse 和 Keyboard 。 Robot Class 的主要目的是促进Java平台实现的自动测试。使用机器人类生成输入事件与使用机器人类将事件发布到 Java AWT事件队列或 AWT组件不同。 em>事件是在平台的本机输入队列中生成的。例如, Robot.mouseMove 实际上将移动鼠标光标,而不是仅生成 Mouse Move Event 。Robot ClassRobot 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.在这一点上,值得一提的是,某些平台需要特殊的特权或扩展才能访问低级输入控件。如果当前平台配置不允许输入控制,则尝试构造Robot对象时将抛出AWTException 。例如,如果 X服务器不支持(或未启用) XTEST 2.2标准扩展名,则 X-Window系统将引发异常。 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.示例:Robot robot = new Robot();// Press keys using robot. A gap of of 500 mili seconds is added after every key pressrobot.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); 动作类 动作类在 org.openqa.selenium。交互 包,并且是面向用户的API ,用于在使用 Selenium 时模拟复杂的用户手势。通过 Selenium 进行 Test Automation 时,可以使用此类而不是直接使用Keyboard或Mouse。 Actions类实现了 Builder Pattern ,它可以构建 CompositeAction ,其中包含以下提到的方法调用指定的所有操作:Actions ClassActions 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 : build() click(WebElement目标) > clickAndHold(WebElement目标) > contextClick(WebElement目标) doubleClick(WebElement目标) dragAndDrop(WebElement源,WebElement目标) moveToElement(WebElement目标,int xOffset,int yOffset) perform() > sendKeys(WebElement目标,java.lang.CharSequence ...键) 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(); 这篇关于当硒中有Actions类时,为什么需要Robot类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 13:16