NoSuchSessionException Session ID is null. Using WebDriver after calling quit()? when running the java tests in parallel

Y_Sh picture Y_Sh · Jul 31, 2018 · Viewed 14.6k times · Source

Getting NoSuchSessionException Session ID is null. Using WebDriver after calling quit()? error when running the java test in parallel via cross-browsers.

The test execution looks fine but in the end the NoSuchSessionException error appeared. I do understand that the driver should be initialized somewhere but it's not clear how to do that.

The project info: selenium WD, java, testng, maven. Test is being built on jenkins and run on saucelabs.

Main class looks like:

  public class NewCustomerReg extends RemoteTestBase {
  @Test (dataProvider = "browsers")

  public void RegisterNewUser (String browser, String version, String os, 
  Method method) throws Exception {

    this.createRemoteDriver(browser, version, os, method.getName());
    Application app = new Application(driver);

    app.homePage().homePageDisplayed();
    Log.info("Validate Home Page");

    app.homePage().registerToOrder.click();
    Log.info("Click Register To Order link on the home page");

    app.registerToOrderPage().registerAsNewCustomer.click();
    Log.info("Click on Register As New Customer button");

    driver.close();
 }

The RemoteTestBase that are extended by the main class:

public class RemoteTestBase {

public WebDriver driver;
private static String baseUrl;
ConfigFileReader configRead;
protected PropertyLoader propertyRead;
public Logger Log = Logger.getLogger(BasicTest_Local.class.getName());
private static final String SAUCE_ACCESS_KEY = 
System.getenv("SAUCE_ACCESS_KEY");
private static final String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME");

@BeforeMethod

@DataProvider(name = "browsers", parallel = true)
public static Object[][] sauceBrowserDataProvider(Method testMethod) throws 
JSONException {

    String browsersJSONArrayString  = 
    System.getenv("SAUCE_ONDEMAND_BROWSERS");
    System.out.println(browsersJSONArrayString);
    JSONArray browsersJSONArrayObj = new JSONArray(browsersJSONArrayString);

    Object[][] browserObjArray = new Object[browsersJSONArrayObj.length()] 
    [3];
    for (int i=0; i < browsersJSONArrayObj.length(); i++) {
        JSONObject browserObj = 
    (JSONObject)browsersJSONArrayObj.getJSONObject(i);
        browserObjArray[i] = new Object[]{browserObj.getString("browser"), 
    browserObj.getString("browser-version"), browserObj.getString("os")};
    }
    return browserObjArray;
   }

   void createRemoteDriver(String browser, String version, String os, String 
   methodName) throws Exception {

    DesiredCapabilities capabilities = new DesiredCapabilities();
    Class<? extends RemoteTestBase> SLclass = this.getClass();
    capabilities.setCapability("browserName", browser);
    if (version != null) {
        capabilities.setCapability("browser-version", version);
    }
    capabilities.setCapability("platform", os);
    capabilities.setCapability("name", SLclass.getSimpleName());
    capabilities.setCapability("tunnelIdentifier", "hdsupply");


    driver = (new RemoteWebDriver(new URL("http://" + SAUCE_USERNAME + ":" + 
    SAUCE_ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub"), capabilities));

    randomuser = new RandomDataSelect();
    configRead = new ConfigFileReader();
    propertyRead = new PropertyLoader();
    baseUrl = propertyRead.getProperty("site.url");
    getURL();
}



private void getURL () {

    driver.get(baseUrl);
    driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
    this.annotate("Visiting page..." + driver.toString());


}

private void printSessionId() {

    String message = String.format("SauceOnDemandSessionID=%1$s job- 
    name=%2$s",
            (((RemoteWebDriver) driver).getSessionId()).toString(), "some job 
    name");
    System.out.println(message);
}



@AfterMethod(description = "Throw the test execution results into saucelabs")
public void tearDown(ITestResult result) throws Exception {

    ((JavascriptExecutor) driver).executeScript("sauce:job-result=" + 
 (result.isSuccess() ? "passed" : "failed"));
    printSessionId();
    driver.quit();
}
void annotate(String text)
{
    ((JavascriptExecutor) driver).executeScript("sauce:context=" + text);
}
}

The error log:

Session ID is null. Using WebDriver after calling quit()?
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03- 
11T20:26:55.152Z'
System info: host: 'gfmwsb01lds.hsi.hughessupply.com', ip: '10.224.196.74', 
os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-504.8.1.el6.x86_64', 
java.version: '1.8.0_161'
Driver info: driver.version: RemoteWebDriver
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using 
WebDriver after calling quit()?
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03- 
11T20:26:55.152Z'
System info: host: 'gfmwsb01lds.hsi.hughessupply.com', ip: '10.224.196.74', 
os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-504.8.1.el6.x86_64', 
java.version: '1.8.0_161'
Driver info: driver.version: RemoteWebDriver
at 
org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.
java:125)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.
java:545)
at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions
$RemoteTimeouts.implicitlyWait(RemoteWebDriver.java:779)
at com.hdsupplysolutions.tests.RemoteTestBase.getURL(RemoteTestBase.java:83)
at 
com.hdsupplysolutions.tests.RemoteTestBase.createRemoteDriver(RemoteTestBase.
java:75)
at 
com.hdsupplysolutions.tests.***.RegisterNewUser(NewCustomerRegi.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.
java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at 
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocation
Helper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
at org.testng.internal.TestMethodWithDataProviderMethodWorker.call(
TestMethodWithDataProviderMethodWorker.java:71)
at org.testng.internal.TestMethodWithDataProviderMethodWorker.call(
TestMethodWithDataProviderMethodWorker.java:14)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.
java:624)
at java.lang.Thread.run(Thread.java:748)

Answer

Corey Goldberg picture Corey Goldberg · Jul 31, 2018

Here is an explanation of what is happening:

You are calling driver.close() in RegisterNewUser. This will close the current window. If no open windows remain, the driver quits. So, when your tearDown calls driver.quit() the session has already ended and you get an error.

Solutions you could use:

  • don't call driver.close() in your test and let the teardown handle it

or

  • in teardown, check for a valid session and only call driver.quit() if one exists