gpt4 book ai didi

java - 将启用工作和测试的 Selenium 键盘操作

转载 作者:行者123 更新时间:2023-12-01 14:27:15 25 4
gpt4 key购买 nike

我正在使用 Java 使用 Selenium 和 ChromeDriver。

我注意到我可以执行单击或双击等鼠标操作,但在后台运行测试时继续处理我的任务。

有没有办法用 Selenium 键盘来做到这一点?

请注意,我希望在提示窗口(如文件选择提示)而不是浏览器本身上单击文本,同时继续使用我自己的键盘。

我的代码:

robot = new Robot();
for (char c : textToType.toCharArray()) {
int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
if (KeyEvent.CHAR_UNDEFINED == keyCode) {
logger.error("Key code not found for character '" + c + "'");
} else {
try {
robot.keyPress(keyCode);
robot.delay(10);
robot.keyRelease(keyCode);
robot.delay(10);
} catch (Exception e) {
if (c == '_') {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_MINUS);
robot.keyRelease(KeyEvent.VK_MINUS);
robot.keyRelease(KeyEvent.VK_SHIFT);
}
if (c == ':') {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT);
}
}
}
}
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_ENTER);
} catch (Exception ex) {
logger.error(ex.getMessage());
}

最佳答案

Selenium是一个免费(开源)的自动化测试套件,适用于跨不同浏览器和平台的 Web 应用程序。但它不具备处理所有键盘和鼠标事件的能力,例如右键单击拖放clickAndHold 等。


处理键盘按键/事件

键盘键/事件可以与 WebDriver 一起处理通过以下方式:

  • 使用 Action 类处理键盘按键/事件。
  • 使用 sendkeys chord 处理键盘按键/事件。
  • 使用 Robot 类处理键盘按键/事件。

You can find a detailed relevant discussion in Why do we need Robot class when we have Actions class in selenium


你的用例

但是,执行鼠标操作(如单击或双击)并在后台运行测试时继续处理您的任务将违反所有最佳实践,因为:

  • 测试执行必须在受控环境中执行以获得优化的性能。
  • 当您的@Tests 正在执行时,它应该没有人工干预
  • 特别是当您的@TestsSelenium 时,而测试执行 进行中 测试环境不应被干预,因为:

    • At the lowest level, the behavior of actions class is intended to mimic the remote end's behavior with an actual input device as closely as possible, and the implementation strategy may involve e.g. injecting synthesized events into a browser event loop. Therefore the steps to dispatch an action will inevitably end up in implementation-specific territory. However there are certain content observable effects that must be consistent across implementations. To accommodate this, the specification requires that remote ends perform implementation-specific action dispatch steps, along with a list of events and their properties. This list is not comprehensive; in particular the default action of the input source may cause additional events to be generated depending on the implementation and the state of the browser (e.g. input events relating to key actions when the focus is on an editable element, scroll events, etc.).

  • 此外,

    • An activation trigger generated by the WebDriver API user needs to be indistinguishable from those generated by a real user interacting with the browser. In particular, the dispatched events will have the isTrusted attribute set to true. The most robust way to dispatch these events is by creating them in the browser implementation itself. Sending OS-specific input messages to the browser's window has the disadvantage that the browser being automated may not be properly isolated from a user accidentally modifying input source state. Use of an OS-level accessibility API has the disadvantage that the browser's window must be focused, and as a result, multiple WebDriver instances cannot run in parallel.

    • An advantage of an OS-level accessibility API is that it guarantees that inputs correctly mirror user input, and allows interaction with the host OS if necessary. This might, however, have performance penalties from a machine utilisation perspective.

  • 此外,

    • Robot Class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations. Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events.

  • 最后,根据 Internet Explorer and Native Events :

    • As the InternetExplorerDriver is Windows-only, it attempts to use so-called "native", or OS-level events to perform mouse and keyboard operations in the browser. This is in contrast to using simulated JavaScript events for the same operations. The advantage of using native events is that it does not rely on the JavaScript sandbox, and it ensures proper JavaScript event propagation within the browser. However, there are currently some issues with mouse events when the IE browser window does not have focus, and when attempting to hover over elements.

  • 浏览器焦点:

    • The challenge is that IE itself appears to not fully respect the Windows messages we send the IE browser window (WM_MOUSEDOWN and WM_MOUSEUP) if the window doesn't have the focus. Specifically, the element being clicked on will receive a focus window around it, but the click will not be processed by the element. Arguably, we shouldn't be sending messages at all; rather, we should be using the SendInput() API, but that API explicitly requires the window to have the focus. We have two conflicting goals with the WebDriver project.

    • First, we strive to emulate the user as closely as possible. This means using native events rather than simulating the events using JavaScript.

    • Second, we want to not require focus of the browser window being automated. This means that just forcing the browser window to the foreground is sub-optimal.

You can find a detailed discussion in Way to open Selenium browser not overlapping my current browser

  • 此外,您需要将浏览器保持最大化,因为最小化的浏览器会出现找不到元素的错误。

You can find a detailed discussion in How to execute tests with selenium webdriver while browser is minimized


引用

您可以在以下位置找到相关的详细讨论:

关于java - 将启用工作和测试的 Selenium 键盘操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61932569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com