使用@DataProvider并行运行测试

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

Run tests in parallel using @Dataprovider

问题

以下是翻译好的部分:

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

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

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

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

  1. public class DemoParallelTesting{
  2. WebDriver wdriver;
  3. @BeforeMethod
  4. public synchronized void parallelDemo() throws Exception {
  5. wdriver = new ChromeDriver();
  6. wdriver.get("https://www.baseURL.com");
  7. }
  8. @Test(dataProvider = "loginData")
  9. public void Registration_data(String testcasename, String sUserName, String sPassword) throws Exception {
  10. // 进行登录操作
  11. }
  12. @DataProvider(name = "loginData", parallel = true)
  13. public Object[][] getData() {
  14. String filepath= System.getProperty("user.dir") + "/src/test/resources/testData/" + "loginData.xlsx";
  15. Object data[][] = testData(filepath, "Sheet1");
  16. return data;
  17. }
  18. public Object[][] testData(String filepath, String sheetName) {
  19. // 读取Excel文件并返回数据
  20. return data;
  21. }
  22. }

testng.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
  3. <suite name="Suite" data-provider-thread-count="3">
  4. <test name="DemoTest" parallel="methods">
  5. <classes>
  6. <class name="rough.DemoParallelTesting"/>
  7. </classes>
  8. </test>
  9. </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.

  1. public class DemoParallelTesting{
  2. WebDriver wdriver;
  3. @BeforeMethod
  4. public synchronized void parallelDemo() throws Exception {
  5. // public void parallelDemo() throws Exception {
  6. wdriver = new ChromeDriver();
  7. wdriver.get(&quot;https://www.baseURL.com&quot;);
  8. }
  9. @Test(dataProvider = &quot;loginData&quot;)
  10. public void Registration_data(String testcasename, String sUserName, String sPassword) throws Exception {
  11. // Do login
  12. }
  13. @DataProvider(name = &quot;loginData&quot;, parallel = true)
  14. public Object[][] getData() {
  15. String filepath= System.getProperty(&quot;user.dir&quot;) + &quot;/src/test/resources/testData/&quot; + &quot;loginData.xlsx&quot;;
  16. Object data[][] = testData(filepath, &quot;Sheet1&quot;);
  17. return data;
  18. }
  19. public Object[][] testData(String filepath, String sheetName) {
  20. // read excel file
  21. return data;
  22. }

testng.xml

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

答案1

得分: 1

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

  1. 在Registration_data定义中添加数据提供者类:
  1. @Test(dataProvider = "loginData", dataProviderClass = DataProviderSource.class)
  2. public void Registration_data(String testcasename, String sUserName, String sPassword) throws Exception {
  3. // 进行登录操作
  4. }
  1. 在数据提供者类中创建以下方法:
  1. @DataProvider(parallel = true)
  2. public static Object[][] loginData() {
  3. Object[][] param = new Object[10][2];
  4. param[0][0] = test_user1;
  5. param[0][1] = passUser1;
  6. param[1][0] = test_user2;
  7. param[1][1] = passUser2;
  8. param[2][0] = test_user3;
  9. param[2][1] = passUser3;
  10. // ... 继续添加其他数据
  11. }
英文:

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
  1. @Test(dataProvider = &quot;loginData&quot;, dataProviderClass = DataProviderSource.class)
  2. public void Registration_data(String testcasename, String sUserName, String sPassword) throws Exception {
  3. // Do login
  4. }
  1. Create method as below in dataprovider class:
  1. @DataProvider(parallel = true)
  2. public static Object[][] loginData() {
  3. Object[][] param = new Object[10][2];
  4. param[0][0] = test_user1;
  5. param[0][1] = passUser1;
  6. param[1][0] = test_user2;
  7. param[1][1] = passUser2;
  8. param[2][0] = test_user3;
  9. param[2][1] = passUser3;
  10. }

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:

确定