Search code examples
seleniumselenium-webdrivertestngsaucelabsbrowserstack

How to run TestNG test nodes in sequence but all inside classes in parallel for different browser capabilities?


I want to run my automation suite (implemented using Selenium Webdriver (Java) + TestNG + Maven) in parallel against multiple browser capabilities. I can run it in parallel for different capabilities using below sample testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="5" name="Functional Test Suite" parallel="tests">
    <test name="Windows-FF-37.0" preserve-order="true" >
        <parameter name="browser" value="Firefox"/>
        <parameter name="version" value="37.0"/>
        <parameter name="os" value="Windows"/>
        <parameter name="os_version" value="8.1"/>
        <parameter name="device" value=""/>
        <classes>
            <class name="uat.mycompany.tests.FirstTest"/>
            <class name="uat.mycompany.tests.SecondTest"/>
            <class name="uat.mycompany.tests.ThirdTest"/>
            <class name="uat.mycompany.tests.FourthTest"/>
        </classes>
    </test>
    <test name="MAC-Yosemite-Chrome-36.0" preserve-order="true">
        <parameter name="browser" value="Chrome"/>
        <parameter name="version" value="36.0"/>
        <parameter name="os" value="OS X"/>
        <parameter name="os_version" value="Yosemite"/>
        <parameter name="device" value=""/>
        <classes>
            <class name="uat.mycompany.tests.FirstTest"/>
            <class name="uat.mycompany.tests.SecondTest"/>
            <class name="uat.mycompany.tests.ThirdTest"/>
            <class name="uat.mycompany.tests.FourthTest"/>
        </classes>
    </test>
</suite>

But when I run this testng.xml, it starts with 'FirstTest' on both VMs; one with Chrome+Mac & another with Forefox+Windows configuration. I DO NOT want this, because the data modified by one script fails another script.

My requirement is to run all classes from one node in parallel and nodes in sequence. As a work around, I can always put classes name in different order in different test nodes, but since I am having hundreds of test classes, it is not feasible solution.

Also, eventually I want to set up these parameters through some properties file so that I can manage the run environments easily.


Solution

  • You can follow the below steps:

    • Define 2 different suites (XML files) for different tests.
    • for each suite mention: thread-count="4" parallel="classes"
    • Create a third suite (xml file) to run the above 2 files sequentially (Don't mention thread-count & parallel parameters for suite.)

    First XML file:

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite thread-count="5" name="Functional Test Suite1" parallel="classes">
        <test name="Windows-FF-37.0" preserve-order="true" >
            <parameter name="browser" value="Firefox"/>
            <parameter name="version" value="37.0"/>
            <parameter name="os" value="Windows"/>
            <parameter name="os_version" value="8.1"/>
            <parameter name="device" value=""/>
            <classes>
                <class name="uat.mycompany.tests.FirstTest"/>
                <class name="uat.mycompany.tests.SecondTest"/>
                <class name="uat.mycompany.tests.ThirdTest"/>
                <class name="uat.mycompany.tests.FourthTest"/>
            </classes>
        </test>
    </suite>
    

    Second XML file:

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite thread-count="5" name="Functional Test Suite2" parallel="classes">
        <test name="MAC-Yosemite-Chrome-36.0" preserve-order="true">
            <parameter name="browser" value="Chrome"/>
            <parameter name="version" value="36.0"/>
            <parameter name="os" value="OS X"/>
            <parameter name="os_version" value="Yosemite"/>
            <parameter name="device" value=""/>
            <classes>
                <class name="uat.mycompany.tests.FirstTest"/>
                <class name="uat.mycompany.tests.SecondTest"/>
                <class name="uat.mycompany.tests.ThirdTest"/>
                <class name="uat.mycompany.tests.FourthTest"/>
            </classes>
        </test>
    </suite>
    

    Third XML file (Need to execute this file):

    <suite name="suite of suites">
        <suite-files>
            <suite-file path="Functional Test Suite1.xml" />
            <suite-file path="Functional Test Suite2.xml" />
        </suite-files>
    </suite>