2

I am trying to run my Tests in parallel , everything works fine if my dataProvider is present in the same class where my @Test are present.

However parallel run sometimes runs fine and sometimes fails in case i place my DataProvider in a separate class.

Below is my common DataProvider Code

@DataProvider(name = "dp",parallel=true)
public static Object[][] getData(Method m) {

    String sheetName = m.getName();

    int rows = excel.getRowCount(sheetName);

    int cols = excel.getColumnCount(sheetName);

    Object[][] data = new Object[rows - 1][1];

    Hashtable<String, String> table = null;

    for (int rowNum = 2; rowNum <= rows; rowNum++) { // 2

        table = new Hashtable<String, String>();

        for (int colNum = 0; colNum < cols; colNum++) {

            // data[0][0]
            table.put(excel.getCellData(sheetName, colNum, 1), excel.getCellData(sheetName, colNum, rowNum));
            data[rowNum - 2][0] = table;
        }

    }

    return data;

}

And below is one of my @Test

@Test(dataProviderclass = TestUtil.class,name="dp")
public void doLogin1(HashTable<String,String>data) throws InterruptedException {

    openBrowserLaunchURL(data.get("browser"));

    HomePage homePage = new HomePage();
    homePage.gotoLogin().doSignIn(data.get("username"), data.get("password")).doLogout();

    quitBrowser();

}
James Z
  • 12,209
  • 10
  • 24
  • 44
Soumyansh Gupta
  • 121
  • 1
  • 2
  • 10

1 Answers1

0

TestNG need all integrated annotation in same @Test itself, the class which contains @Test annotation.

What you can do is, To use common DataProvider code :

You need to define DataProvider declaration in each Class and the logic/code behind it you can define in Separate method which you call publicly in any class.

Sample Example:

@DataProvider(name = "dp",parallel=true)
public static Object[][] getData(Method m) {
   return readerUtility.demogetData();    
}

Common Reader class:

public class readerUtility {

  public Object[][] demogetData(){

  String sheetName = m.getName();

    int rows = excel.getRowCount(sheetName);
    int cols = excel.getColumnCount(sheetName);
    Object[][] data = new Object[rows - 1][1];

    Hashtable<String, String> table = null;
    for (int rowNum = 2; rowNum <= rows; rowNum++) { 

        table = new Hashtable<String, String>();
        for (int colNum = 0; colNum < cols; colNum++) {
            table.put(excel.getCellData(sheetName, colNum, 1),excel.getCellData(sheetName, colNum, rowNum));
            data[rowNum - 2][0] = table;
        }
    }
  return data;
  }
} 
Ishita Shah
  • 3,955
  • 2
  • 27
  • 51