Vinay M S
Vinay M S

Reputation: 25

TestNg Non-Parallel execution of multiple browsers giving "org.openqa.selenium.NoSuchSessionException: invalid session id"

I'm trying to execute my code in both Chrome and Firefox using TestNg non-parallelly. In first Test "Chrome" works properly the second Test "Firefox" browser is opened but throws the following exception 'org.openqa.selenium.NoSuchSessionException: invalid session id'.

org.openqa.selenium.NoSuchSessionException: invalid session id
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'thundurus', ip: '172.17.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.15.0-70-generic', java.version: '1.8.0_222'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 78.0.3904.108, chrome: {chromedriverVersion: 78.0.3904.105 (60e2d8774a81..., userDataDir: /tmp/.com.google.Chrome.Ug9Qqh}, goog:chromeOptions: {debuggerAddress: localhost:37143}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 1340b5921cffb4a50d3542b199d22db6
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.perform(RemoteWebDriver.java:618)
    at org.openqa.selenium.interactions.Actions$BuiltAction.perform(Actions.java:639)
    at modules.actions.moveOver(actions.java:11)
    at pageModels.homePage.hover_menu(homePage.java:22)
    at testCases.testClass.base(testClass.java:14)
    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:133)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.testng.TestRunner.privateRun(TestRunner.java:770)
    at org.testng.TestRunner.run(TestRunner.java:591)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
    at org.testng.SuiteRunner.run(SuiteRunner.java:304)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
    at org.testng.TestNG.runSuites(TestNG.java:1032)
    at org.testng.TestNG.run(TestNG.java:1000)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

This is the exception I get after Chrome has finished execution. In the above exception, the browser parameter passed for Second Test is still "Chrome".

TestNg file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="2">

    <test thread-count="5" name="Chrome">
        <parameter name="browser" value="Chrome"/>
            <!-- <groups>
                <run>
                    <include name="exe.home"/>
                </run>
            </groups> -->
            <classes>
                <class name="modules.openBrowser" />
                <class name="testCases.testClass"/>
            </classes>
        </test> <!-- Test -->

        <test thread-count="5" name="FireFox">
    <parameter name="browser" value="Firefox"/>
        <!-- <groups>
            <run>
                <include name="exe.home"/>
            </run>
        </groups> -->
        <classes>
            <class name="modules.openBrowser" />
            <class name="testCases.testClass"/>
        </classes>
    </test> <!-- Test -->

</suite> <!-- Suite -->

Browser Handling class:

public class openBrowser implements auto_constant{
    public static WebDriver driver = null;

  @BeforeTest(description = "Checking the browser and launching it")
  @Parameters({"browser"})
  public void beforeTest(String browser) {

      extentReports.attRepo(browser);

        if(browser.equalsIgnoreCase("Chrome")) {
            WebDriverManager.chromedriver().setup();
            if(Property.getProperty("head").equalsIgnoreCase("false")) {
                driver = new ChromeDriver();
            }else {
                ChromeOptions options = new ChromeOptions();
                options.addArguments("--headless");
                driver = new ChromeDriver(options);
            }
        }else if(browser.equalsIgnoreCase("Firefox")) {
            driver = null;
            WebDriverManager.firefoxdriver().setup();
            if(Property.getProperty("head").equalsIgnoreCase("false")) {
                driver = new FirefoxDriver();
            }else {
                FirefoxOptions options = new FirefoxOptions();
                options.addArguments("headless");
                driver = new FirefoxDriver(options);
            }
        }
        driver.manage().window().maximize();
        driver.get(url);
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  }

  @AfterTest(description = "Terminating the browser instance and reports")
  public void afterTest() {
      extentReports.extent.flush();
      driver.close();
  }

I even tried removing the "static" from WebDriver still the same exception.

Thank you.

Upvotes: 0

Views: 867

Answers (3)

Alok kumar
Alok kumar

Reputation: 11

This code seems fine to me. Please be sure that you are using compatible Firefox version with selenium & its webdriver

Upvotes: 0

SeleniumNT
SeleniumNT

Reputation: 60

You have defined WebDriver as Static.

public static WebDriver driver = null;

Remove static and try again.

Upvotes: 1

sedhu madhavan
sedhu madhavan

Reputation: 11

There may be two issues in your script.

  1. You may be closing or quitting your browser before the script ends, which may cause the session to become invalid. Looking into your test script may give clear picture

  2. In the second case you may be using static variable to create page objects. When you run in parallel and if you static page objects then memory will be allocated only once to the first created page object and session will be invalid when your first script closes. Looking into the test script may give clear picture.

Upvotes: 1

Related Questions