Search code examples
javaseleniumtestng

TestNG Based Selenium Tests not running in Parallel


I am using the below TestNG Config to enable parallel execution of Selenium tests.

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

<suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">

    <test name="Suite Test">
        <classes>
            <class name="SampleTest">
                <methods>
                    <include name="firstTest"/>
                    <include name="secondTest"/>
                    <include name="thirdTest"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

Java Code:

@Test(dataProvider = "TestData")
public void firstTest(String data){
   //Code
}

@Test(dataProvider = "TestData")
public void secondTest(String data){
   //Code
}

@Test(dataProvider = "TestData")
public void thirdTest(String data){
   //Code
}

The Selenium tests are expected to run in parallel. I expect 2 browsers to be open and run the test script.

But I see only 1 browser and all 3 tests run one after the other and not in parallel. I have tried using test, methods, class, instance options for "parallel" attribute.

Any help?


Solution

  • This is due to a bug in TestNG 6.13.1 [ See GITHUB-1636 for more details ]

    I have fixed this in the latest SNAPSHOT of TestNG (6.14-SNAPSHOT) and this should be available for use in the released version of TestNG (6.14).

    But until then, please alter your suite xml file to look like below :

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    
    <suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">
        <test name="Suite Test" parallel="methods" thread-count="2" verbose="1">
            <classes>
                <class name="SampleTest">
                    <methods>
                        <include name="firstTest"/>
                        <include name="secondTest"/>
                        <include name="thirdTest"/>
                    </methods>
                </class>
            </classes>
        </test>
    </suite>
    

    The work-around is basically to add the attributes parallel="methods" thread-count="2" at the <test> level also.