繁体   English   中英

@test注解中的并行执行

[英]Parallel execution with in @test annotation

我有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

        }

您必须在TestNG中使用DataProvider来获取它。 可以说在数据提供程序中传递多个数据集,即URL包含在列表中urls = 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"}
    };  

您可以像下面的代码一样使用并行执行:

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。
它将同时打开两个浏览器。
希望这对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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