testngsaucelabsselenium4

@AfterMethod tearDowns for running the entire class using - selenium 4


I am facing tear down issues (org.openqa.selenium.SessionNotCreatedException) when running an entire class which holds many methods. Tests are ran on saucelabs.

when a single method is run, it works absolutely fine but while running the entire class or a suite, it is not working. Also, few methods are passing while the others are going in tear down. What could be the possibilities ?

Config: TestNG - 7.4.0 Maven - 3.8.4 Selenium - 4.1.3

Code: Codes are various but we are using this below for all the methods (it runs on multiple browsers)

     private ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();
    public WebDriver getWebDriver() {
            // Starts the ThreadLocal<Boolean> for checking the evidon banner as false
            hasEvidonBeenChecked.set(false);
            if (!BaseTest.isLocal) {
                return webDriver.get();
            } else {
                return localWebDriver;
            }
        }
 @org.testng.annotations.Test(dataProvider = "remoteBrowsers", priority = 1)
public void TestPractive(String browser, String version, String os, String platformversion,
        String devicename, Method method) throws InterruptedException, OfferNotFoundException, IOException,
        NullPointerException, IndexOutOfBoundsException, ArrayIndexOutOfBoundsException {
    this.createDriver(browser, version, os, platformversion, devicename, method.getName());
    WebDriver driver = this.getWebDriver();
    this.setupImplicitWait(driver, 20);

Error Default suite Total tests run: 9, Passes: 3, Failures: 3, Skips: 3 Configuration Failures: 3, Skips: 3

[AsyncHttpClient-1-8] ERROR org.asynchttpclient.netty.request.NettyRequestSender - Can't write request [AsyncHttpClient-1-10] ERROR org.asynchttpclient.netty.request.NettyRequestSender - Can't write request [AsyncHttpClient-1-9] ERROR org.asynchttpclient.netty.request.NettyRequestSender - Can't write request java.io.IOException: Stream closed

FAILED CONFIGURATION: @AfterMethod tearDown java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.JavascriptExecutor.executeScript(String, Object[])" because the return value of "java.lang.ThreadLocal.get()" is null

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. Build info: version: '4.1.0', revision: '87802e897b' System info: host: 'W105482ZY2', ip: '192.168.29.171', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.1' Driver info: org.openqa.selenium.remote.RemoteWebDriver Command: [null, newSession {capabilities=[Capabilities {browserName: MicrosoftEdge, browserVersion: latest-1, sauce:options:, desiredCapabilities=Capabilities {browserName: MicrosoftEdge, browserVersion: latest-1, sauce:options: , platformName: Windows 10, resolution: 1920x1080}, seleniumVersion: 4.1.0}}] Capabilities {}

TestNG XML testng xml


Solution

  • There was an issue with the saucelab url, which was closing the treads after few sessions.

    Actual: // Launch remote browser and set it as the current thread webDriver.set(new RemoteWebDriver( new URL("https://" + username + ":" + accesskey + "@ondemand.saucelabs.com:443/wd/hub"),,capabilities));

    Expected: new URL("https://" + username + ":" + accesskey + "@ondemand.us-west-1.saucelabs.com:443/wd/hub"),capabilities));