TestNG | Run parallel tests in Eclipse with Maven and RemoteWebDriver

Maven is a tool that can be used for building and managing any Java-based project. It is used by many frameworks, and you should be able to run them successfully in Eclipse after completing this guide.

Important: This document includes references to third-party products, TestNG, Eclipse, and Maven. The user interface and usage of third-party products are subject to change without notice. For the latest published information about TestNG, see https://testng.org/doc/documentation-main.html. For the latest published information about Eclipse, see https://www.eclipse.org/documentation. For the latest published information about Maven, see https://maven.apache.org/guides/index.html.

Prerequisites

  • Minimum Java 1.7 JDK installed 

  • JDK for Eclipse-installed JREs (see the article Setting JDK in Eclipse on Stack Overflow)

  • Window > Preferences > Java > Installed JREs > Add
  • Maven 3.3.x installed (for more information, see this Maven tutorial)
  • Eclipse TestNG plugin installed (search for it in the Eclipse Marketplace)

Project setup

Unzip and import the project files below into Eclipse or clone from the git repo: https://bitbucket.org/uvadevildog/rwdperfectodemo

MvnTestNgRWDDemo.zip

Copy
<suite thread-count="5" data-provider-thread-count="5" name="DemoSuite">
<parameter name="perfectoCloud" value="YOUR_MOBILECLOUD"/>
<!--  Ex. mobilecloud.perfectomobile.com  -->
<parameter name="perfectoUserName" value="YOUR_USER_NAME"/>
<!--  Ex. tester@perfectomobile.com  -->
<parameter name="perfectoPassword" value="YOUR_PASSWORD"/>
<parameter name="seleniumGridHost" value="YOUR_GRID"/>
<!--  Ex. 12.34.567.890:4444  -->
<test name="Test Mobile and Desktop WebSite" parallel="methods">
    <classes>
        <class name="ParallelRWDTest"/>
    </classes>
</test>
<!--  Test  --></suite><!--  Suite  -->

If Maven and TestNG are set up correctly in Eclipse, you should be able to run the project using both xml files in the root of the project.

  • If you see project compilation errors: Right-click the MvnTestNgRWDDemo project name and select Maven > Update Project.

  • First try to build and run the tests with Maven: Right-click the pom.xml file and select Run As > Maven install.

  • If Maven has downloaded the dependencies and built the project correctly, you can also execute the tests using only TestNG: Right-click the testng.xml file and select Run As > TestNG Suite.

Project files

The following project files are relevant here:

  • pom.xml file: 

    • Tells Maven what libraries to download and use for the project

    • Tells the maven-surefile-plugin where to find the testng.xml file

    • Specifies what dependencies to download so no additional jars need to be added to the project

    Copy
    <project ... >
        ...
       <properties>
           ...
            <testngFile>testng.xml</testngFile>
       </properties>
       <build>
            ...
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.16</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>${basedir}/${testngFile}</suiteXmlFile>
                    </suiteXmlFiles>
            ...
       </build>
       <dependencies>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-remote-driver</artifactId>
                <version>${selenium.version}</version>
            </dependency>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>${testng.version}</version>
            </dependency>      ...
       </dependencies>
    </project>
  • testng.xml file: 

    • Tells TestNG where to look for tests

    • Includes the ParallelRWDTest class

    • Includes your credentials as the parameter values.

    Copy
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <suite thread-count="5" data-provider-thread-count = "5" name="DemoSuite" >
        <parameter name="perfectoCloud" value="YOUR_MOBILECLOUD"/> 
        <parameter name="perfectoUserName" value="YOUR_USER_NAME"/>
        <parameter name="perfectoPassword" value="YOUR_PASSWORD"/>
        <parameter name="seleniumGridHost" value="YOUR_GRID"/> 
        <test name="Test Mobile and Desktop WebSite" parallel="methods">
            <classes>
                <class name="ParallelRWDTest"/>
            </classes>
        </test> <!-- Test -->
    </suite> <!-- Suite -->
  • ParallelRWDTest.java file:

    • Contains one TestNG @Test and one @Dataprovider (the dataprovider function creates a test for each object it creates)

    • Creates RemoteWebDriver objects for both Perfecto cloud devices and desktop browsers on the specified Selenium grid

    • Runs all five desktop and device tests shown in the code below in parallel

    Copy
    @DataProvider(parallel = true)
    public Object[][] capabilitiesProvider() {
        return new Object[][] {
            new Object[]{"iPhone Test", specifyCaps("model=iPhone.*", "network=.*")},
            new Object[]{"Desktop FF Test", DesiredCapabilities.firefox()},
            new Object[]{"Android Test", specifyCaps("platformName=Android")},
            new Object[]{"iPad Test", specifyCaps("model=iPad.*")},
            new Object[]{"Desktop Chrome", DesiredCapabilities.chrome()},
        };
    }

    @Test(dataProvider = "capabilitiesProvider")
    public void TestWebSite(String testName, DesiredCapabilities caps) {    
        RemoteWebDriver driver = getNewDeviceDriver(caps);
        ...