Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在@test注释中并行执行_Java_Selenium_Testng - Fatal编程技术网

Java 在@test注释中并行执行

Java 在@test注释中并行执行,java,selenium,testng,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

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

        }
@测试
公共静态void test1()引发异常{
Configuration conf=Configuration.getConfiguration();
List url=conf.getListoForls();//返回要测试的URL列表
setup();//具有IE浏览器的功能
对于(int i=0;i
您可以使用类似以下代码的并行执行:

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。
它将同时打开两个浏览器。

希望这对您有所帮助。

您必须在TestNG中使用DataProvider来获取它。假设在Data Provider中传递多个数据集,即URL包含在列表URL=conf.getListoForls()中

下面是一个简单的例子:

  @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"}
    };  

在使用并行测试的同时,将您的列表拆分为Thank@murali的一半副本。它现在正在启动2个浏览器,但问题是打开浏览器后应该执行一些操作,这些操作只在启动的浏览器中的一个上执行。如何让这些操作在所有浏览器上运行已打开的ser实例。请帮助!@Noob在数据提供中尝试一次而不使用parallel=true,如果它工作正常,那么我们需要进一步查看。我将查看2row