Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🐛 Bug]: WebDriver loses connection to grid/times out after file download in chrome #11838 #13605 #14894

Open
mprataps-tibco opened this issue Dec 12, 2024 · 5 comments

Comments

@mprataps-tibco
Copy link

What happened?

After migrating from selenium 3 to selenium 4 (4.8.1 & 4.16.1) we have an issue that heavily impacts our automation runs. Since we have thousand of tests, we are hitting this issue quite regularly. Issue is relevant only when running on remote grid.
It seem to happen when there is file download happening in browser, in Chrome it leads to Chrome Download page to open and then close again. Most of the times it just works, however sometimes when trying to get webDriver.getWindowHandles() or get alert or some other operations (we hit it most often with webDriver.getWindowHandles()) - there is timeout happening.
I created simple test that reproduces the issue (you may need to run it several times, even though there is a counter to run test 100 times).
The only dependencies required for the test are:

<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.7.1</version>
        </dependency>

Grid run using docker images: selenium/hub:4.8.0-20230123/selenium/node-chrome:4.8.0-20230123

docker run -d -p 4442-4444:4442-4444 --net grid --name selenium-hub480 selenium/hub:4.8.0-20230123

docker run -d --net grid -e SE_EVENT_BUS_HOST=selenium-hub480 -e SE_NODE_GRID_URL=http://localhost:4444/ --shm-size="2g" -e SE_EVENT_BUS_PUBLISH_PORT=4442 -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 -e GEOMETRY=1680x1050x24 -e SCREEN_WIDTH=1680 -e SCREEN_HEIGHT=1050 -e SE_ENABLE_TRACING=false -P selenium/node-chrome:4.8.0-20230123
We also tried the recommendation https://www.selenium.dev/documentation/grid/configuration/cli_options/#enabling-managed-downloads-by-the-node provided #13605 (comment) .

How can we reproduce the issue?

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.ITestContext;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

public class SeleniumTest {
    private WebDriver webDriver;
    private String initialOpenedWindow;

    @BeforeClass(alwaysRun = true)
    public void setUp(ITestContext testContext) {
        webDriver = getWebDriver();
    }

    @AfterClass(alwaysRun = true)
    public void finalizeTest() {
        webDriver.quit();
    }

    @AfterMethod(alwaysRun = true)
    public void closeExtraWindows() {
        try {
            new WebDriverWait(webDriver, Duration.ofSeconds(1)).until(ExpectedConditions.alertIsPresent());
            Alert alert = webDriver.switchTo().alert();
            String text = alert.getText(); //<<<<<< Could fail here as well with org.openqa.selenium.WebDriverException: java.net.ConnectException: Connection refused
            alert.accept();
        } catch (NoAlertPresentException | TimeoutException e) {
            //
        }
        for (String windowHandle : webDriver.getWindowHandles()) { //<<<<<< fails here with org.openqa.selenium.TimeoutException: java.util.concurrent.TimeoutException
            if (!windowHandle.equals(initialOpenedWindow)) {
                webDriver.switchTo().window(windowHandle);
                webDriver.close();
            }
        }
        webDriver.switchTo().window(initialOpenedWindow);
    }

    @DataProvider
    public Iterator<Object[]> counter() {
        List<Object[]> data = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            data.add(new Object[]{i});
        }
        return data.iterator();
    }

    @Test(dataProvider = "counter")
    public void test_timeoutOnGettingWindowHandles(int number) {
        initialOpenedWindow = webDriver.getWindowHandle();
        //Open some page
        webDriver.get("https://testfiledownload.com/"); // <<<< may as well fail here with org.openqa.selenium.TimeoutException: timeout: Timed out receiving message from renderer: 89.805
        //download file to have downloads open and close
        webDriver.findElement(By.xpath("//a[contains(@href, '1Mb')]")).click();
    }

    public WebDriver getWebDriver() {
        final WebDriver driver;
        //Only Remote configuration
        try {
            driver = new RemoteWebDriver(new URL("http://localhost:4444"), getDesiredCapabilities());
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
            driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(90));
        } catch (MalformedURLException e) {
            throw new IllegalStateException("Invalid remoteWebDriverUrl:" + e);
        }
        driver.manage().window().maximize();
        return driver;
    }

    public ChromeOptions getDesiredCapabilities() {
        ChromeOptions browserOptions = getOptions();
        LoggingPreferences logPrefs = new LoggingPreferences();
        logPrefs.enable(LogType.BROWSER, Level.INFO);
        browserOptions.setCapability(ChromeOptions.LOGGING_PREFS, logPrefs);
        browserOptions.setCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
        return browserOptions;
    }

    private ChromeOptions getOptions() {
        final ChromeOptions options = new ChromeOptions();
        options.addArguments("test-type");
        options.addArguments("start-maximized");
        options.addArguments("disable-application-cache");
        options.addArguments("disable-popup-blocking");
        options.addArguments("disable-infobars");
        options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
        options.addArguments("no-sandbox", "window-size=1680,1050");
        Map<String, Object> preferences = new HashMap<>();
        preferences.put("plugins.always_open_pdf_externally", Boolean.TRUE);
        preferences.put("credentials_enable_service", Boolean.FALSE);
        preferences.put("profile.password_manager_enabled", Boolean.FALSE);
        options.setExperimentalOption("prefs", preferences);
        return options;
    }

}

Relevant log output

org.openqa.selenium.TimeoutException: java.util.concurrent.TimeoutException
Build info: version: '4.8.1', revision: '8ebccac989'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '11.5', java.version: '11.0.11'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Command: [3839d34358ce97b41b3b4835f04df7e5, getWindowHandles {}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 109.0.5414.74, chrome: {chromedriverVersion: 109.0.5414.74 (e7c5703604da..., userDataDir: /tmp/.com.google.Chrome.ClYLXp}, goog:chromeOptions: {debuggerAddress: localhost:41993}, goog:loggingPrefs: {browser: INFO}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: LINUX, proxy: Proxy(), se:bidiEnabled: false, se:cdp: ws://localhost:4444/session..., se:cdpVersion: 109.0.5414.74, se:vnc: ws://localhost:4444/session..., se:vncEnabled: true, se:vncLocalAddress: ws://172.18.0.3:7900, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: ignore, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: 3839d34358ce97b41b3b4835f04df7e5

	at org.openqa.selenium.remote.http.netty.NettyHttpHandler.makeCall(NettyHttpHandler.java:65)
	at org.openqa.selenium.remote.http.AddSeleniumUserAgent.lambda$apply$0(AddSeleniumUserAgent.java:42)
	at org.openqa.selenium.remote.http.Filter.lambda$andFinally$1(Filter.java:56)
	at org.openqa.selenium.remote.http.netty.NettyHttpHandler.execute(NettyHttpHandler.java:49)
	at org.openqa.selenium.remote.http.AddSeleniumUserAgent.lambda$apply$0(AddSeleniumUserAgent.java:42)
	at org.openqa.selenium.remote.http.Filter.lambda$andFinally$1(Filter.java:56)
	at org.openqa.selenium.remote.http.netty.NettyClient.execute(NettyClient.java:99)
	at org.openqa.selenium.remote.tracing.TracedHttpClient.execute(TracedHttpClient.java:55)
	at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:181)
	at org.openqa.selenium.remote.TracedCommandExecutor.execute(TracedCommandExecutor.java:51)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:598)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:602)
	at org.openqa.selenium.remote.RemoteWebDriver.getWindowHandles(RemoteWebDriver.java:454)
	at SeleniumTest.closeExtraWindows(SeleniumTest.java:53)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
	at org.testng.internal.invokers.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:69)
	at org.testng.internal.invokers.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:361)
	at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:296)
	at org.testng.internal.invokers.TestInvoker.runConfigMethods(TestInvoker.java:823)
	at org.testng.internal.invokers.TestInvoker.runAfterConfigurations(TestInvoker.java:792)
	at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:768)
	at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:221)
	at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
	at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:969)
	at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:194)
	at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
	at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at org.testng.TestRunner.privateRun(TestRunner.java:829)
	at org.testng.TestRunner.run(TestRunner.java:602)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:437)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:431)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:391)
	at org.testng.SuiteRunner.run(SuiteRunner.java:330)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1256)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1176)
	at org.testng.TestNG.runSuites(TestNG.java:1099)
	at org.testng.TestNG.run(TestNG.java:1067)
	at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
	at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
Caused by: java.util.concurrent.TimeoutException
	at java.base/java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1886)
	at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2021)
	at org.asynchttpclient.netty.NettyResponseFuture.get(NettyResponseFuture.java:206)
	at org.openqa.selenium.remote.http.netty.NettyHttpHandler.makeCall(NettyHttpHandler.java:59)
	... 46 more

Operating System

MacOS / Windows 11

Selenium version

selenium-java 4.8.1 , selenium-java 4.16.1

What are the browser(s) and version(s) where you see this issue?

Chrome 109.0.5414.74 , Chrome 120.0.6099.109

What are the browser driver(s) and version(s) where you see this issue?

chromedriverVersion: 109.0.5414.74 ,chromedriverVersion: 120.0.6099.109

Are you using Selenium Grid?

4.8.0-20230123 , 4.16.1

Copy link

@mprataps-tibco, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

@diemol
Copy link
Member

diemol commented Dec 12, 2024

Can you first confirm the issue happens in the latest versions of Selenium? We don't troubleshoot old versions.

@mprataps-tibco
Copy link
Author

mprataps-tibco commented Dec 13, 2024

We have tried the selenium version 4.16.1 as well and we are facing the same issue there as well. It is intermittent issue and affects 1-2% test cases. But as the number of test cases are allot, we are affected by this in every run.

@pujagani
Copy link
Contributor

Latest version of Selenium is 4.27.0 https://github.com/SeleniumHQ/selenium/releases/tag/selenium-4.27.0. I will be happy to look into it once you confirm the issue persists with latest Selenium version. Are you facing the same error is the latest version of Chrome too or only mentioned specific versions of Chrome?

@mprataps-tibco
Copy link
Author

We require some time to upgrade it to latest version i.e. 4.27.0 as it includes infrastructure side changes as well. Will update it in few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants