[英]Unable to run parallel test via TestNg from DATA provider
Below is my XML piece.下面是我的 XML 片。
<?xml version="1.0" encoding="UTF-8"?>
<suite name='Automation' threadCount="5" parallel="methods">
<tests>
<parameter name='clientName' value='Five' />
<test name='PA'>
<classes>
<class name='TC_INC_1'>
</class>
</classes>
</test>
So I am loading the required data from excel through DATA PROVIDER in TestNg.因此,我通过 TestNg 中的 DATA PROVIDER 从 excel 加载所需的数据。 What I wanted to achieve is to run each row in different threads.
我想要实现的是在不同的线程中运行每一行。 Lets say I had 5 rows of data
假设我有 5 行数据
1- Go to Google.com
2- Go to Facebook.com
3- Go to Dollarama.com
4- Go to Walmart.com
5- Go to KegSteak.com
And say I am running two thread means two browsers.并说我正在运行两个线程意味着两个浏览器。 I want both browsers run parallelly executing each of the row in any sequence.
我希望两个浏览器以任何顺序并行运行每一行。
Thread 1 - 1- Go to Google.com Thread 2- 2- Go to Facebook.com First test done - browser closed and opens again. Thread 1 - 1- Go to Google.com Thread 2- 2- Go to Facebook.com First test done - browser closed and opens again.
Now it should pick the 3 and fourth row.现在它应该选择第 3 行和第 4 行。 Thread 1 - 3- Go to Dollarama.com Thread 2- 4- Go to Walmart.com
线程1-3- Z5F075AE3E1F9D0382BB8C4632991F96F96FZ to dollarama.z4d236d9a2d9a2d9a2d102c56ad1c50da4bec50da4bec50z thread 2- 4- 4-
browser closed and opens again.浏览器关闭并再次打开。 Thread 1 - 5- Go to KegSteak.com
螺纹 1 - 5- Go 到 KegSteak.com
[![testdata][1]][1] [![测试数据][1]][1]
What I actually see is two browsers open and one of the browser runs the url and the other just becomes static after launching chrome.我实际看到的是打开了两个浏览器,其中一个浏览器运行 url,另一个在启动 chrome 后变成 static。
Any fixes?有什么修复吗?
With Local WebDriver variable使用本地 WebDriver 变量
Make sure, you launch and tear-down your WebDriver within a test method:确保在测试方法中启动和拆除 WebDriver:
@Test(dataProvider = "provideUrls")
public void navigateByUrlTest(String url){
WebDriver driver = ...
driver.get(url);
// do something
driver.quit();
}
//I know this implemented to get data from Excel, but just for example..
@DataProvider(parallel = true)
public Object[][] provideUrls() {
return new Object [][] {
{"https://google.com"},
{"https://facebook.com"},
{"https://dollarama.com"},
{"https://walmart.com"},
{"https://kegSteak.com"}
};
}
With Global Thread-Safe WebDriver variable使用全局线程安全 WebDriver 变量
WebDriver configuration can be moved to @BeforeMethod/@AfterMethod
methods. WebDriver 配置可以移动到
@BeforeMethod/@AfterMethod
方法。
NOTE : ThreadLocal
wrapper should be used for WebDriver
instance in this case.注意:在这种情况下,应将
ThreadLocal
包装器用于WebDriver
实例。 This ensures we will keep separate WebDriver instances per each thread.这确保我们将为每个线程保留单独的 WebDriver 实例。
protected ThreadLocal<WebDriver> driverThreadSafe = new ThreadLocal<WebDriver>();
@BeforeMethod
public void launchDriver() {
driverThreadSafe.set(new ChromeDriver());
}
@AfterMethod
public void quitDriver() {
driverThreadSafe.get().quit();
}
@Test(dataProvider = "provideUrls")
public void test(String url){
WebDriver driver = driverThreadSafe.get();
driver.get(url);
// do something, but do not quit the driver
}
Configure Threads Count配置线程数
<suite name='Automation' threadCount="5"
- this will not work for DaraProvider
parallelism. <suite name='Automation' threadCount="5"
- 这不适用于DaraProvider
并行性。
You have to pass dataproviderthreadcount
testNG argument with thread count for data-provider.您必须将
dataproviderthreadcount
testNG 参数与数据提供者的线程计数一起传递。
eg programmatically, add this method to the current class (or parent base test class)例如,以编程方式,将此方法添加到当前 class(或父基测试类)
@BeforeSuite
public void setDataProviderThreadCount(ITestContext context) {
context.getCurrentXmlTest().getSuite().setDataProviderThreadCount(5);
}
References参考
TestNG parallel Execution with DataProvider TestNG 与 DataProvider 并行执行
https://testng.org/doc/documentation-main.html#running-testng https://testng.org/doc/documentation-main.html#running-testng
https://www.baeldung.com/java-threadlocal https://www.baeldung.com/java-threadlocal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.