0

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>
8
  • where are you initializing driver? Commented Mar 21, 2022 at 6:26
  • Can you share your testng.xml file as well? Commented Mar 21, 2022 at 7:35
  • @cruisepandey updated on main comment :) Commented Mar 21, 2022 at 15:09
  • @GauravLad inside @beforeMethod, there is... driver = new ChromeDriver(); Commented Mar 21, 2022 at 15:36
  • Be sure to use driver.quit(). driver.close() won't necessarily quit the driver. (some drivers will quit if there are no longer any browser windows open...) It should also help to include a sleep after quitting the driver. This gives the browser/driver time to cleanup things... especially the file that drops the sessionID. Commented Mar 21, 2022 at 20:15

1 Answer 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();
        } 
      
}  
Sign up to request clarification or add additional context in comments.

5 Comments

I tried and get a different error msg (I've tried that before), now I'm getting that the mouse hover action doesn't work but is weird because if I run just that test, it works..
That's a different error though, not sure and may be that particular function/step would give details
Element is not able to click now (WaitForElementToBeClickeable Method) [[ChromeDriver: chrome on MAC (74b549057fe70f0d04d7c576e5a89df1)] -> xpath: //*[@id='top-links']/ul/li[2]/a/span[1]]org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=99.0.4844.83) This is the error I got. Page tested is http://opencart.abstracta.us and if you look that xpath, there is @dheeraj
You are trying to click the element which is no more available on the page, use proper waits or expected conditions to do the click action
I have tried with the new waits for selenium 4 using thisnew WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.visibilityOf(element)); or this one driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); and the element is just present in the first test case, for the next ones it doesn't

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.