'Selenium + TestNG + Java -> org.openqa.selenium.NoSuchSessionException: invalid session id
I'm trying to run all my tests together instead one by one but I'm getting this error msg.
I'm using TestNG, Selenium 4.1.2, Java, chromedriver manage by import io.github.bonigarcia.wdm.WebDriverManager;
This is my BaseClass code, (where I create my setup() method):
@BeforeMethod(alwaysRun = true)
public void before(@Optional("chrome") String browser) {
try {
WebDriverManager.chromedriver().setup();
WebDriverManager.firefoxdriver().setup();
if(browser.equals("chrome")){
driver = new ChromeDriver();
}else{
driver = new FirefoxDriver();
}
driver.get(URL);
driver.manage().window().maximize();
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterMethod(alwaysRun = true)
public void after() {
try {
driver.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I have 2 classes and If I run all my tests, they just run and pass the first test of every class and for the other ones just send me the same error org.openqa.selenium.NoSuchSessionException: invalid session id.
NOTE: If I run one by one manually it works..!!
But if I run e.g "maven clean test" which will run a xml within POM, or right click on xml the same error msg appears, I've searched but all mention something about driver.close() or driver.quit() and anything works. Any help could be great :D
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Portafolio-Selenium-Project</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.8.10/aspectjweaver-1.8.10.jar"
</argLine>
<useSystemClassLoader>false</useSystemClassLoader>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/config.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Selenium Project - Tests Cases">
<test name="Header Page Tests">
<parameter name="browser" value="chrome"/>
<classes>
<class name="Tests.HeaderPageTests" />
</classes>
</test>
<test name="Register Page Tests">
<parameter name="browser" value="chrome"/>
<classes>
<class name="Tests.RegisterPageTests" />
</classes>
</test>
</suite>
Solution 1:[1]
probably the driver.close() in After suite will resolve the issue as there is no session when the other test being started.
Can you try by changing below,
@AfterSuite
public void after_suite()
{
try {
driver.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dheeraj |
