简体   繁体   中英

Run tests in parallel using @Dataprovider

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.

在此处输入图像描述

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

在此处输入图像描述

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("https://www.baseURL.com");
}

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

@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) {
    // read excel file
    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> <!-- Test -->
</suite> <!-- Suite -->

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 = "loginData", 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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM