Selenium InternetExplorerDriver doesn't get focus on the window
Asked Answered
G

1

1

My project includes Selenium webdriver, JAVA, Maven, TestNG, Jenkins, Allure (reports). I have a few suites of tests with 100+ test cases, and I iterate them through 3 different browsers (the tests run in parallel using TestNG).

There is one test that can't pass unless I am actually watching the window and see the test run.

I will explain: what am I trying to test? our JS developers created a feature that only if the user has focus on the window, then a image slideshow will start to move and change images.

On Firefox and Chrome it pass great- I don't need to see the test. The focus can be on other tabs or browsers, and the driver will emulate everything. On IEdriver its not like that!!

I have tried to add many capabilities to the driver and still nothing (some of them solved me some other issues):

}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 ***");
Gastrotrich answered 7/8, 2018 at 11:51 Comment(0)
H
1

Seems you have opted to add all the InternetExplorerDriver related capabilities.

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.

Solution

As an interim solution you can add the capability:

ieCapabilities.setCapability("requireWindowFocus", false);
Homespun answered 7/8, 2018 at 11:59 Comment(2)
If you are going to quote something, make sure you use the proper Markdown symbol, >, to indicate that you are quoting.Pearlene
Thanks @DebanjanB, it works! I already had that capability, but it was set to "true" (not sure why). I hope it won't mess with my other tests (clicks, etc.).Gastrotrich

© 2022 - 2024 — McMap. All rights reserved.