AMA: Why I am getting Session id is null?

Sajitha Pathirana
2 min readFeb 15, 2022
Photo by Branko Stancevic on Unsplash

Here is my video series on integrating Cross-Browser Support Framework in your Test Framework. If you haven’t watched it yet, definitely check that out, you will have something to learn.

org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()? The session ID is null is a frequent exception that we see while executing tests using WebDriver.

This can happen due to a few reasons. Here I am sharing the below reasons I have experienced.

Scenario 1

This can be due to the browser and the respective driver. you are using. Each driver has its own way of calling browser.close(). If you call driver.quit() multiple times with chrome driver, it will ignore.

Scenario 2

If you have declared your Webdriver as a static variable, this might cause problems, especially when you are running tests in parallel.

public static WebDriver driver = DriverFactory.getWebDriver(); 
//Which was causing the issue.

You need to re-assign a driver object each time you start a test to avoid this. The best way is to thread localize the Webdriver.

public static ThreadLocal<WebDriver> driver=new ThreadLocal<>();
public static WebDriver getDriver() {
if(driver.get()==null){
driver.set(...);
}
return driver.get();
}

Scenario 3

If you are using BDD and encountering this when you are executing the second data set of your example table,

  • This can be due to a static page object you have initialized during the first execution and then the driver has been quit().
  • Now during the second run when you try to access the elements of the static page object you have already initialized, with the previous driver, you get this exception.
  • This is because as per the theory, static variable load once for the entire JVM life cycle.

--

--

Sajitha Pathirana

A Test automation enthusiast, passionate to help the teams to enhance their testing journey with his decade of experience in the field.