Lukas Quatro
Lukas Quatro

Reputation: 41

Maven+Selenium 2: How to run tests with one single browser open

I'm trying to run some tests, seven to be specific, I've created my xml file and all and it works correctly, my test file has a package which has the seven tests in it, so the thing is, when the xml file is executed it opens a new browser window for each test (in case you were wondering, I do have the driver quit and close functions, they are at the @afterTest annotation) and I don't want that, what I need is just to get one browser open at all times, e.g. after finishing test one, close that browser because for the second test a second browser is open, what I'll like to get is to close each browser after each test is finished, is it a way to achieve that?

Here is my pom file:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.enterprise.automation</groupId>
<artifactId>enterpriseAutomation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>barcoAutomation</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- added -->
    <suiteFile>src/test/resources/barcoColor.xml</suiteFile>
</properties>

<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8</version>
        <!-- <classifier>jdk17</classifier> -->
    </dependency>
    <dependency>
        <groupId>org.uncommons</groupId>
        <artifactId>reportng</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.42.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.10-FINAL</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.10-FINAL</version>
    </dependency>

</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>src/test/resources/myTestsFile.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/test/resources/</directory>
        </resource>
    </resources>
</build>

EDIT: the reason i don't want the seven browsers opened is because we are testing a SPA which has a lot of javascript and it consumes a bit too much pc resources, so if more than one browser are opened the computer performance drops

I also saw the "-1", if you could tell how to improve the question details I'll appreciate it

Thanks

Upvotes: 0

Views: 4185

Answers (2)

niharika_neo
niharika_neo

Reputation: 8531

As far as I understand, if you say you want "to get is to close each browser after each test is finished", you need to use @AfterMethod and @BeforeMethod. This would ensure that after every @Test runs, the browser gets closed.

With @AfterTest, this would happen just once - note Test in @AfterTest maps to <test> in the testng xml and does not map to @Test in your code.

@AfterSuite would just close your browser once in the entire execution, which would mean if you wanted a clean session on each @Test, that won't happen.

If you do not want a clean session, then @Before/@AfterSuite would work as well.

Upvotes: 0

StrikerVillain
StrikerVillain

Reputation: 3776

Instead of @AfterTest and @BeforeTest use @BeforeSuite and @AfterSuite

Create a BaseTest class containing @BeforeSuite and @AfterSuite.

public class BaseTest{
    static WebDriver driver;
    @BeforeSuite
    public void start() {
        driver = new FFDriver();
        driver.get(url);
    }


    @AfterSuite
    public void end() {
        driver.close();
        driver.quit();
    }
}

Create Test classes.

public class Test1{
    static WebDriver driver;
    @Test
    public void test1() {
        driver = BaseTest.driver;
        //code goes here
    }
}


public class Test2{
    static WebDriver driver;
    @Test
    public void test2() {
        driver = BaseTest.driver;
        //code goes here
    }
}

Modify testNG xml file as follows.

<suite name="Regression Testing" parallel="classes">
    <test verbose="2" name="Test1">
        <classes>
            <class name="BaseTest"/>
            <class name="Test1"/>
        </classes>
    </test>
    <test verbose="2" name="Test2">
        <classes>
            <class name="Test2"/>
        </classes>
    </test>
</suite>

The @BeforeSuite is run first followed by Test1, Test2 and @AfterSuite. Driver instance is obtained in each Test class by setting driver as static in BaseTest.

Let me know if this helps you.

Upvotes: 2

Related Questions