简体   繁体   中英

Cross-Browser-Testing - How to configure parallel execution of tests in testng.xml

I want to execute parallel tests on different browsers by using TestNG. This is my testng.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<suite name="Parallel Tests" verbose="2" parallel="tests" thread-count="4">

        <!--Set test parameters to execute test in Mozilla Firefox browser on Windows platform. -->

        <test name="Linux+firefox Test1" parallel="classes" thread-count="2">

                <parameter name="platform" value="LINUX"/>
                <parameter name="browser" value="firefox"/>
                <parameter name="url" value="http://hh.de/"/>
                <parameter name="version" value="58.0b7"/>

            <classes>
                <class name="suchen.Portalsuche"/>
                <class name="suchen.HVV"/>
            </classes>
        </test>

        <!-- Set test parameters to execute test in Google Chrome browser on Windows platform. -->
        <test name="Linux+chrome Test1" parallel="classes" thread-count="2">

                <parameter name="platform" value="LINUX"/>
                <parameter name="browser" value="chrome"/>
                <parameter name="url" value="http://hh.de/"/>
                <parameter name="version" value="62.0.3202.62"/>

            <classes>
                <class name="suchen.Portalsuche"/>
                <class name="suchen.HVV"/>
            </classes>
        </test>

</suite>

It launches the first browser (Mozilla Firefox) but it doesn't neither execute the two tests nor launching the second browser(Google Chrome).

Also this is the code snippet which I'm currently using for launching those browsers:

@Parameters({ "platform", "browser", "url", "version" })
    @BeforeTest(alwaysRun = true)
    public void setup(String platform, String browser, String url, String version) throws MalformedURLException {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setPlatform(org.openqa.selenium.Platform.LINUX);
        System.setProperty("java.net.preferIPv4stack", "true");
        caps.setCapability("SeleniumTests", "redhat5 && amd64");

        if (browser.equalsIgnoreCase("firefox")) {
            System.out.println("Executing on Firefox");
            String Hub = "http://localhost:4444/wd/hub";
            caps = DesiredCapabilities.firefox();
            caps.setBrowserName("firefox");
            System.setProperty("webdriver.gecko.driver", "/opt/geckodriver.exe");

            driver = new RemoteWebDriver(new URL(Hub), caps);
            driver.manage().window().maximize();
            driver.navigate().to(url);

        } else if (browser.equalsIgnoreCase("chrome")) {
            System.out.println("Executing on Chrome");
            String Hub = "http://localhost:4444/wd/hub";
            caps = DesiredCapabilities.chrome();
            caps.setBrowserName("chrome");
            ChromeOptions options = new ChromeOptions();
            System.setProperty("webdriver.chrome.driver", "/opt/chromedriver.exe");
            caps.setCapability(ChromeOptions.CAPABILITY, options);

            options.addArguments("--start-maximized");
            driver = new RemoteWebDriver(new URL(Hub), caps);
            driver.navigate().to(url);
        }
    }

Thanks for your help. I appreciate it.

Remove the parallel attribute at test tag level and reset the thread count at the suite tag level to 2.

The testNG xml should look like this:

<?xml version="1.0" encoding="UTF-8" ?>
<suite name="Parallel Tests" verbose="2" parallel="tests" thread-count="2">

        <!--Set test parameters to execute test in Mozilla Firefox browser on Windows platform. -->

        <test name="Linux+firefox Test1" >

                <parameter name="platform" value="LINUX"/>
                <parameter name="browser" value="firefox"/>
                <parameter name="url" value="http://hh.de/"/>
                <parameter name="version" value="58.0b7"/>

            <classes>
                <class name="suchen.Portalsuche"/>
                <class name="suchen.HVV"/>
            </classes>
        </test>

        <!-- Set test parameters to execute test in Google Chrome browser on Windows platform. -->
        <test name="Linux+chrome Test2" >

                <parameter name="platform" value="LINUX"/>
                <parameter name="browser" value="chrome"/>
                <parameter name="url" value="http://hh.de/"/>
                <parameter name="version" value="62.0.3202.62"/>

            <classes>
                <class name="suchen.Portalsuche"/>
                <class name="suchen.HVV"/>
            </classes>
        </test>

</suite>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM