java - Selenium InternetExplorerDriver 没有将焦点放在窗口上

标签 java selenium selenium-webdriver selenium-iedriver desiredcapabilities

我的项目包括 Selenium webdriver、JAVA、Maven、TestNG、Jenkins、Allure(报告)。我有几套包含 100 多个测试用例的测试套件,我通过 3 个不同的浏览器迭代它们(测试使用 TestNG 并行运行)。

除非我实际观察窗口并看到测试运行,否则有一项测试无法通过。

我将解释:我想测试什么?我们的 JS 开发人员创建了一项功能,只有当用户将焦点放在窗口上时,图像幻灯片才会开始移动和更改图像。

在 Firefox 和 Chrome 上,它通过得很好 - 我不需要看测试。焦点可以位于其他选项卡或浏览器上,驱动程序将模拟一切。在 IEdriver 上不是这样的!!

我尝试向驱动程序添加许多功能,但仍然一无所获(其中一些解决了我的其他问题):

}else if (browser.equalsIgnoreCase("ie")) {

    String exeServiceIEdriver = Consts.ieDriverPath;
    System.setProperty("webdriver.ie.driver", exeServiceIEdriver);
      DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
      ieCapabilities.setCapability("nativeEvents", false);
      ieCapabilities.setCapability("unexpectedAlertBehaviour", "accept");
      ieCapabilities.setCapability("ignoreProtectedModeSettings", true);
      ieCapabilities.setCapability("disable-popup-blocking", true);
      ieCapabilities.setCapability("enablePersistentHover", true);
      ieCapabilities.setCapability("ignoreZoomSetting", true);
        //ieCapabilities.setCapability("version", "12"); does it work?? don't think so..
      ieCapabilities.setCapability("requireWindowFocus", true);
        //ieCapabilities.setCapability("browser_version", "9.0");  // Does NOT work. need user agent
      ieCapabilities.setCapability("IE_ENSURE_CLEAN_SESSION", true);  // Does NOT work. need user agent
      ieCapabilities.setCapability("browserAttachTimeout",5000);
      ieCapabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS,true);
      ieCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
      ieCapabilities.setCapability(CapabilityType.SUPPORTS_APPLICATION_CACHE,false);

    driver = new InternetExplorerDriver(ieCapabilities);
    Log.info("\n*** Starting IE Browser ***");

最佳答案

您似乎已选择添加所有 InternetExplorerDriver 相关功能。

Browser Focus

The challenge is that IE itself appears to not fully respect the Windows messages we send to 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.

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

An additional consideration is the possibility of multiple IE instances running under multiple WebDriver instances, which means any such bring the window to the foreground solution will have to be wrapped in some sort of synchronizing construct (probhably a mutex) within the IE driver's C++ code. Even so, this code will still be subject to race conditions, if, for example, the user brings another window to the foreground between the driver bringing IE to the foreground and executing the native event.

The discussion around the requirements of the driver and how to prioritize these two conflicting goals is ongoing. The current prevailing wisdom is to prioritize the former over the latter, and document that your machine will be unavailable for other tasks when using the IE driver. However, that decision is far from finalized, and the code to implement it is likely to be rather complicated.

解决方案

作为临时解决方案,您可以添加以下功能:

ieCapabilities.setCapability("requireWindowFocus", false);

关于java - Selenium InternetExplorerDriver 没有将焦点放在窗口上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51726121/

相关文章:

selenium - 规范远程时如何运行 Protractor 测试

java - 如何模拟 HttpSession 并将其作为参数传递给与 session 一起使用的方法?

java - 使用定界符,读到第一个定界符,然后换行

javascript - 处理 Protractor 中的未知错误

java - 运行 cucumber 测试改变变量?

python - 无法通过在 python 中使用 xpath 和 selenium 来单击元素

java - 使用 Selenium webdriver 从网页中提取表数据

c# - 如何使用 C# 在 Selenium WebDriver (Selenium 2) 中最大化浏览器窗口?

java - 插入新记录时,列表将覆盖先前添加的记录

java - 模块之间的依赖管理