Webdriver.io/Selenium tests fail when the window is in the background on Chrome 87
This is a bug in Chrome 87:
https://bugs.chromium.org/p/chromedriver/issues/detail?id=3641&sort=-id
Workaround
Node JS
The workaround is to set the "localState" in Webdriver.io's desiredCapabilities
like the below in Node.JS/Chimpy:
chimpOptions.webdriverio.desiredCapabilities = {
chromeOptions: {
args: ["--no-sandbox", ...],
prefs: {...}
},
localState: {
"browser.enabled_labs_experiments": ["calculate-native-win-occlusion@2"],
},
},
...
};
Java
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeLocalStatePrefs = new HashMap<String, Object>();
List<String> experimentalFlags = new ArrayList<String>();
experimentalFlags.add("calculate-native-win-occlusion@2");
chromeLocalStatePrefs.put("browser.enabled_labs_experiments", experimentalFlags);
options.setExperimentalOption("localState", chromeLocalStatePrefs);
Previous Answer
The other workaround is to leave a small lip of the background Chrome window underneath your active browser/IDE/etc.
In the image below, you can see a small amount of the Chrome window running the test.
the latest chrome - chromedriver has resolved this issue
I am using C# and facing same issue. I have added a workaround by adding minimize and maximize window like below. Usually we assert page title, hence the switching to window is bringing the focus and other test actions are passing. below one is the workaround for taking screenshot failure.
private void MinMaxWindow(ChromeDriver driver)
{
driver.Manage().Window.Minimize();
driver.Manage().Window.Maximize();
}
Edit, Dev has given workaround like below.
Java
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeLocalStatePrefs = new HashMap<String, Object>();
List<String> experimentalFlags = new ArrayList<String>();
experimentalFlags.add("calculate-native-win-occlusion@2");
chromeLocalStatePrefs.put("browser.enabled_labs_experiments", experimentalFlags);
options.setExperimentalOption("localState", chromeLocalStatePrefs);
Python
chrome_options = webdriver.ChromeOptions()
experimentalFlags = ['calculate-native-win-occlusion@2']
chromeLocalStatePrefs = { 'browser.enabled_labs_experiments' : experimentalFlags}
chrome_options.add_experimental_option('localState',chromeLocalStatePrefs);