Selenium InternetExplorerDriver 没有关注 window
Selenium InternetExplorerDriver doesn't get focus on the window
我的项目包括 Selenium webdriver、JAVA、Maven、TestNG、Jenkins、Allure(报告)。我有一些包含 100 多个测试用例的测试套件,我通过 3 种不同的浏览器迭代它们(使用 TestNG 并行测试 运行)。
有一个测试无法通过,除非我真的在看 window 并看到测试 运行。
我将解释:我要测试什么? 我们的 JS 开发人员创建了一个功能,只有当用户关注 window,然后图像幻灯片将开始移动和更改图像。
在 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);
我的项目包括 Selenium webdriver、JAVA、Maven、TestNG、Jenkins、Allure(报告)。我有一些包含 100 多个测试用例的测试套件,我通过 3 种不同的浏览器迭代它们(使用 TestNG 并行测试 运行)。
有一个测试无法通过,除非我真的在看 window 并看到测试 运行。
我将解释:我要测试什么? 我们的 JS 开发人员创建了一个功能,只有当用户关注 window,然后图像幻灯片将开始移动和更改图像。
在 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
andWM_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 theSendInput()
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 amutex
) 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);