"org.openqa.selenium.NoSuchSessionException: invalid session id" when running tests with invocationCount attribute

682 views
Skip to first unread message

mycheap...@gmail.com

unread,
Mar 14, 2023, 1:50:54 PM3/14/23
to Selenium Users
Hello,

@Test(invocationCount = 3, threadPoolSize = 3)
public void test1() throws InterruptedException {
System.out.println("Test 1");
WEBDRIVER_THREADLOCAL.get().get("https://www.google.com/");
Thread.sleep(3000);
System.out.println("title is " + WEBDRIVER_THREADLOCAL.get().getTitle());
}

BaseTest

public WebDriver driver;
public static ThreadLocal<WebDriver> WEBDRIVER_THREADLOCAL = new ThreadLocal<WebDriver>();

@BeforeMethod
public void launch() throws MalformedURLException {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--remote-allow-origins=*");
System.setProperty("webdriver.chrome.driver", ".//resources//chromedriver.exe");
driver = new ChromeDriver(chromeOptions);
WEBDRIVER_THREADLOCAL.set(driver);
driver.manage().window().maximize();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@AfterMethod
public void afterSuite() {
WEBDRIVER_THREADLOCAL.get().close();
}

Console:
Test 1
Test 1
Test 1
title is Google
title is Google
// Could not get the third title. 

Stacktrace:
org.openqa.selenium.NoSuchSessionException: invalid session id
Build info: version: '4.1.2', revision: '9a5a329c5a'
System info: host: 'DESKTOP-MI634SK', ip: '192.168.1.216', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.4.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [00db712d7263d967ecd6fe23b85f68f7, getTitle {}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 111.0.5563.65, chrome: {chromedriverVersion: 111.0.5563.19 (378a38865270..., userDataDir: C:\Users\\AppData\...}, goog:chromeOptions: {debuggerAddress: localhost:52574}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:52574/devtoo..., se:cdpVersion: 111.0.5563.65, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
Session ID: 00db712d7263d967ecd6fe23b85f68f7
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:558)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:613)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:617)
at org.openqa.selenium.remote.RemoteWebDriver.getTitle(RemoteWebDriver.java:332)
at thread.pool.invocation.counts.tests.FirstTest.test1(FirstTest.java:14)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at org.testng.internal.thread.ThreadUtil.lambda$execute$0(ThreadUtil.java:64)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)

I think I somewhat understand why I am getting this error. I am closing the browser at @AfterMethod, but because I am using THREADLOCAL, it should not close all the browsers. Currently, test can only get google title first two times and then all browsers get closed so I am getting the error when test tries to get the title third time. How can I close the browsers after test repeated 3 times? Thank you!
Reply all
Reply to author
Forward
0 new messages