使用@DataProvider并行运行测试

huangapple go评论75阅读模式
英文:

Run tests in parallel using @Dataprovider

问题

以下是翻译好的部分:

我想要为10个用户运行登录测试。用户凭证已保存在Excel表中。因此,我想要一次性运行3个测试,即前3行将启动3个专用的Chrome浏览器,然后再运行3个,最后只运行1个。

但问题是,浏览器还会从不同的行中获取数据。

为了解决这个问题,我尝试在测试方法中使用synchronized关键字,但浏览器不会并行打开,它们会按顺序打开、执行测试,然后退出。

我该如何解决这个问题?我希望每一行都有一个专用的Chrome浏览器。

public class DemoParallelTesting{

	WebDriver wdriver;

	@BeforeMethod
	public synchronized void parallelDemo() throws Exception {
		wdriver = new ChromeDriver();
		wdriver.get("https://www.baseURL.com");
	}

	@Test(dataProvider = "loginData")
	public void Registration_data(String testcasename, String sUserName, String sPassword) throws Exception {
		// 进行登录操作
	}
	
	@DataProvider(name = "loginData", parallel = true)
	public Object[][] getData() {
		String filepath= System.getProperty("user.dir") + "/src/test/resources/testData/" + "loginData.xlsx";
		Object data[][] = testData(filepath, "Sheet1");
		return data;
	}

	public Object[][] testData(String filepath, String sheetName) {
		// 读取Excel文件并返回数据
		return data;
	}
}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" data-provider-thread-count="3">
  <test name="DemoTest" parallel="methods">
    <classes>
      <class name="rough.DemoParallelTesting"/>
    </classes>
  </test>
</suite>
英文:

I want to run login test for 10 users. The user credentials are saved in an excel sheet. So, instead of running this test row after row, I want to run 3 in one go, means top 3 rows will have 3 dedicated chrome browsers launched, and then 3 more and then only 1.

使用@DataProvider并行运行测试

But issue is, browsers picking data from different rows also.

使用@DataProvider并行运行测试

To overcome this issue I tried using synchronized keyword in Test Method but then browsers are not opening in parallel, they open sequentially, execute test and quit.

How can I fix this issue? I want one dedicated chrome browser for each row.

public class DemoParallelTesting{

WebDriver wdriver;

@BeforeMethod
public synchronized void parallelDemo() throws Exception {
//	public void parallelDemo() throws Exception {
	wdriver = new ChromeDriver();
	wdriver.get(&quot;https://www.baseURL.com&quot;);
}

@Test(dataProvider = &quot;loginData&quot;)
public void Registration_data(String testcasename, String sUserName, String sPassword) throws Exception {
	
	// Do login 
}

@DataProvider(name = &quot;loginData&quot;, parallel = true)
public Object[][] getData() {
	String filepath= System.getProperty(&quot;user.dir&quot;) + &quot;/src/test/resources/testData/&quot; + &quot;loginData.xlsx&quot;;
	Object data[][] = testData(filepath, &quot;Sheet1&quot;);
	return data;

}

public Object[][] testData(String filepath, String sheetName) {
	// read excel file
	return data;

}

testng.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE suite SYSTEM &quot;https://testng.org/testng-1.0.dtd&quot;&gt;
&lt;suite name=&quot;Suite&quot; data-provider-thread-count=&quot;3&quot;&gt;
  &lt;test name=&quot;DemoTest&quot; parallel=&quot;methods&quot;&gt;
    &lt;classes&gt;
      &lt;class name=&quot;rough.DemoParallelTesting&quot;/&gt;
    &lt;/classes&gt;
  &lt;/test&gt; &lt;!-- Test --&gt;
&lt;/suite&gt; &lt;!-- Suite --&gt;

答案1

得分: 1

代替从Excel中获取数据,使用数据提供者类来执行此测试。在这里,您需要进行以下更改:

  1. 在Registration_data定义中添加数据提供者类:
@Test(dataProvider = "loginData", dataProviderClass = DataProviderSource.class)
public void Registration_data(String testcasename, String sUserName, String sPassword) throws Exception {
      
        // 进行登录操作
}
  1. 在数据提供者类中创建以下方法:
@DataProvider(parallel = true)
public static Object[][] loginData() {
    Object[][] param = new Object[10][2];
    param[0][0] = test_user1;
    param[0][1] = passUser1;

    param[1][0] = test_user2;
    param[1][1] = passUser2;

    param[2][0] = test_user3;
    param[2][1] = passUser3;
    // ... 继续添加其他数据
}
英文:

Instead of picking up from the excel, use data provider class to do this test. Here, you need to change:

  1. Add Data provider class in the Registration_data definition
@Test(dataProvider = &quot;loginData&quot;, dataProviderClass = DataProviderSource.class)
    public void Registration_data(String testcasename, String sUserName, String sPassword) throws Exception {
          
            // Do login 
    }
  1. Create method as below in dataprovider class:
@DataProvider(parallel = true)
       public static Object[][] loginData() {
        		Object[][] param = new Object[10][2];
        		param[0][0] = test_user1;
        		param[0][1] = passUser1;
        		
        		param[1][0] = test_user2;
        		param[1][1] = passUser2;
        		
        		param[2][0] = test_user3;
        		param[2][1] = passUser3;
        }

huangapple
  • 本文由 发表于 2020年8月19日 05:54:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63477162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定