[英]Parallel execution with in @test annotation
i have testNG test which will return list of url to test against and it executes one by one i want atleast 2 urls to run at the same time.. what would be the best way to do this ? 我有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
}
You have to use DataProvider in TestNG to get it. 您必须在TestNG中使用DataProvider来获取它。 Lets say in Data Provider pass multiple datasets ie urls contains in List urls = conf.getListOfUrls(); 可以说在数据提供程序中传递多个数据集,即URL包含在列表中urls = conf.getListOfUrls();
Below is the simple example: 下面是简单的示例:
@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"}
};
You can use parallel execution like below code : 您可以像下面的代码一样使用并行执行:
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
}
}
and in testng.xml : 并在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>
and then run this xml using surefire plugin or directly. 然后使用surefire插件或直接运行此xml。
It will open two browsers at the same time. 它将同时打开两个浏览器。
Hope this helps you. 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.