@test注解中的并行执行

时间:2018-07-10 18:50:11

标签: java selenium testng

我有testNG测试,它将返回要测试的URL列表,并且它要我要同时至少运行2个URL一次执行。.什么是最好的方法?

@Test
public static void test1() throws Exception {

    Configuration conf = Configuration.getConfiguration();

    List<String> urls = conf.getListOfUrls(); // returns list of urls to test against

    setup(); //has capabilities for IE browser

    for (int i = 0; i < urls.size(); i++) {

        WebDriver.get(urls.get(z));

        //do stuff

        }

2 个答案:

答案 0 :(得分:1)

您必须在TestNG中使用DataProvider来获取它。可以说在数据提供程序中传递多个数据集,即列表中包含的url = conf.getListOfUrls();

下面是简单的示例:

  @Test(dataProvider="testdata")
public void simpleTest(String url) {

    System.setProperty("webdriver.chrome.driver",
            System.getProperty("user.dir") + File.separator + "drivers\\chromedriver.exe");

    WebDriver driver = new ChromeDriver();
    driver.get(url);
}

@DataProvider(name="testdata", parallel=true)
public Object[][] data(){
    return new Object[][] {
        {"http://www.google.com"},
        {"http://www.seleniumhq.org"}
    };  

答案 1 :(得分:0)

您可以使用类似于以下代码的并行执行:

package com.demo.core;


import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class PlayField {

    private WebDriver driver;

    public static WebDriver getChromeDriver() throws MalformedURLException {
        System.setProperty("webdriver.chrome.driver",
                           "D:\\ECLIPSE-WORKSPACE\\playground\\src\\main\\resources\\chromedriver-2.35.exe");

        return new ChromeDriver();
    }

    @BeforeMethod
    public void setUp() throws MalformedURLException {
        driver = getChromeDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().window().maximize();
    }

    @Test
    public void test1() throws MalformedURLException, InterruptedException {
        driver.navigate().to("Url1");
        // do anything
    }

    @Test
    public void test2() throws MalformedURLException, InterruptedException {
        driver.navigate().to("Url2");
        //do anything
    }

}

以及在testng.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="First suite" verbose="8" parallel="methods" thread-count="2">
    <test name="Default test1">
        <classes>
            <class name="com.demo.core.PlayField">
            </class>
        </classes>
    </test>
</suite> 

,然后使用surefire插件或直接运行此xml。
它将同时打开两个浏览器。
希望对您有帮助。