Thursday 10 October 2013

WebDriver TestNG Parameterization with DataProvider


For Parameterization in WebDriver, you can have different way..I have listed 2 ways with TestNG + DataProviderYou can take input from xls and store it in DataProvider and then you can pass that to your test.In other case, you can directly put the data in DataProvider, and can access them. You can find the details below...

1)Take Input From Excel

public class TS_Login
{
   WebDriver driver = new FirefoxDriver();

@Test(priority=1)
public void testing()
 {
   driver.get("URL of the website");
  }

@Test(dataProvider="login", priority=2)
public void TS1_LoginFunctionality(String Email,String Pass)throws Exception
 {
   driver.get("Sign In URL");
   driver.findElement(By.id("signin-link")).click();
   driver.findElement(By.id("signin-email")).sendKeys(Email);
   driver.findElement(By.id("signin-pw")).sendKeys(Pass);
   driver.findElement(By.id("signin-submit")).click();
}

@DataProvider
public Object[][] login() throws Exception
 {
   Object[][] testObjArray = getTableArray("Path Of Excel File","Sheet1","");
   return (testObjArray);
 }

//Get the data from excel and store in Array

public static Object[][] getTableArray(String xlFilePath, String sheetName, String tableName)    throws Exception
     {   
        String[][] tabArray = null;
        try
        {
            Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
            Sheet sheet = workbook.getSheet(sheetName);
            int ci,cj, totalRows = 0, totalCols =  0;
            totalRows = sheet.getRows();
            totalCols = sheet.getColumns();
            System.out.println("total cols = " + totalCols);
            System.out.println("total rows = " + totalRows);
            tabArray=new String[totalRows][totalCols];
            ci=0;
          for (int i=1;i (less than) totalRows;i++,ci++)
   {
cj=0;
for (int j=0;j(less than) totalCols;j++, cj++)
                {
                    tabArray[ci][cj]=sheet.getCell(j,i).getContents();
                    System.out.println(tabArray[ci][cj]);
                }
            }
         }
         catch (FileNotFoundException e)
         {
             System.out.println("Could not read the Excel sheet");
             e.printStackTrace();
         }
         catch (IOException e)
         {
             System.out.println("Could not read the Excel sheet");
             e.printStackTrace();
         }
         return(tabArray);
     }
}
Note : Please replace (less than) with  < 

2)Input with DataProvider

public class dataprovider {

WebDriver driver = new FirefoxDriver();

@Test(priority=1)
public void testing()
 {
   driver.get("URL of the website");
  }

@Test(dataProvider = "excelData", priority = 2)
public void executeTest(String name, String numbn) throws InterruptedException
 {
   driver.findElement(By.id("signin-link")).click();
   driver.findElement(By.id("signin-email")).sendKeys(name);
   driver.findElement(By.id("signin-pw")).sendKeys(numbn);
   driver.findElement(By.id("signin-submit")).click();
 }

@DataProvider(name = "excelData")
public Object[][] data(){
return new Object[][]{
                       {"Test","asd"},
                       {"More Testing","asd"},
                       {"Last Test","asd"}};
 }
}



6 comments:

  1. Hi
    Your code is very helpful, I'm stuck at

    Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));

    What is the xlFilepath we need to use
    what is the Tablename variable
    please post the excel file you have used

    ReplyDelete
  2. public static Object[][] getTableArray(String xlFilePath, String sheetName, String tableName)
    String Table is not required

    ReplyDelete
  3. Hi.. when I am using DataProvider and priority under same annotation, the order is not proper for the methods I am calling in the TestNG.xml file.. Need your suggestion

    ReplyDelete