Divagar
Divagar

Reputation: 31

Session ID is null. Using Web Driver after calling quit()? code executes properly in chrome,ie11 and edge browser but not in firefox 55.0.3

This is my code for calling the webdriver.

public class WebDriverUtils {

private static final String karyahost = EnvironmentUtils.getProperty("karya.host", "");
private static final String karyaport = EnvironmentUtils.getProperty("karya.port", "");
private static final long timeOut = 15;
public static RemoteWebDriver webDriver = null;
private static WebDriverWait wait = null;

/**
 * Close the browser
 */
public static void closeBrowser() throws Exception{
    if (webDriver == null) {
        return;
    }
    webDriver.close();
    Thread.sleep(4000);
    webDriver.quit();
    webDriver = null;
}

/***
 * Wait for web element
 * @param element - element to be search
 */
public static void waitForElement(By element) {
    wait.until(ExpectedConditions.visibilityOfElementLocated(element));
}


/***
 * Go to the URL
 * @param url - url
 * @throws Exception
 */
public static void gotoUrl(String url) throws Exception {
    openBrowser();

    webDriver.navigate().to(getUrl(url));
}


/**
 * Launch the browser
 * @return
 * @throws Exception
 */
public static RemoteWebDriver openBrowser() throws Exception {
    if (webDriver != null) {
        return webDriver;
    }
    String browserType = EnvironmentUtils.getProperty("suite.browser.type", "FIREFOX");
    if (browserType.equalsIgnoreCase("CHROME")) {
        System.setProperty("webdriver.chrome.driver", FileUtils.getFile("driver/chromeDriver.exe")
                                                               .getAbsolutePath());
        webDriver = new ChromeDriver();
    } else {
        if (browserType.equalsIgnoreCase("FIREFOX")) {
            Runtime.getRuntime().exec("taskkill /F /IM geckodriver.exe");
            System.setProperty("webdriver.gecko.driver", FileUtils.getFile("driver/geckodriver.exe")
                    .getAbsolutePath());
            webDriver = new FirefoxDriver();
            maximizeBrowser();

        } else {
            if (browserType.equalsIgnoreCase("IE")) {
                System.setProperty("webdriver.ie.driver", FileUtils.getFile("driver/IEDriverServer.exe")
                        .getAbsolutePath());
                webDriver = new InternetExplorerDriver();
            } else {
                if (browserType.equalsIgnoreCase("Edge")) {
                    System.setProperty("webdriver.edge.driver", FileUtils.getFile("driver/MicrosoftWebDriver.exe")
                            .getAbsolutePath());
                    webDriver = new EdgeDriver();
                }
            }
        }
    }

    if (webDriver == null) {
        throw new Exception("Invalid browser type: " + browserType);
    }
    setWait(10);
    maximizeBrowser();
    wait = new WebDriverWait(webDriver, timeOut);
    return webDriver;
}

In firefox the first test case runs fine but when i run the 2nd testcase after i have given webDriver.quit() and webDriver = null so that i can run the test case one after the other by closing and opening the browser,But i get the following error message.

org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '3.5.3', revision: 'a88d25fe6b', time: '2017-08-29T12:42:44.417Z'
System info: host: 'KARYA-45154KR', ip: '192.168.159.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_91'
Driver info: driver.version: SuiteDriver
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:131)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:646)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:703)
    at org.openqa.selenium.remote.RemoteWebDriver.close(RemoteWebDriver.java:532)
    at com.karya.utils.WebDriverUtils.closeBrowser(WebDriverUtils.java:40)
    at com.karya.test.suite.SuiteDriver.cleanUp(SuiteDriver.java:37)
    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(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:525)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:202)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:130)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:308)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1158)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1083)
    at org.testng.TestNG.run(TestNG.java:999)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

It works fine chrome 61.0.3163.79 version,IE 11 and Edge browser.

Upvotes: 2

Views: 20634

Answers (3)

Manny
Manny

Reputation: 156

I was facing the same issue.

Installing the latest released driver from https://github.com/mozilla/geckodriver/releases solved my issue.

Upvotes: 0

Chitrakannan
Chitrakannan

Reputation: 1

If you are using Page Factory, make sure you are passing the driver to the pageObject class to initialize it. More likely, it is the issue.

Upvotes: 0

Tarun Lalwani
Tarun Lalwani

Reputation: 146490

Your problem is your closeBrowser method.

webDriver.close();
Thread.sleep(4000);
webDriver.quit();

Each driver has subtle differences in their working. When you have a chrome driver. You can call quit on the webdriver object any number of time

webDriver.quit();
webDriver.quit();
webDriver.quit();

This is because the chromedriver will ignore these. When you execute close method, it closes the current window. If no more windows are left, the browser also is quit. quit on other case will always close all windows and quit the driver also.

In case of firefox, the first close, closes the current window and quits the driver. Next time you send the quit, it has no session id to quit a driver. Thats why you get the error Session ID is null. Using WebDriver after calling quit(). So ground rule, close is equivalent to quit if only 1 window is open

So you replace your code as

public static void closeBrowser() throws Exception{
    if (webDriver == null) {
        return;
    }
    webDriver.quit();
    webDriver = null;
}

And all should work

Upvotes: 5

Related Questions