i

Selenium Step By Step Guide

TestNG DataProvider

As we can connect excel sheets using our Excel Reading utility, we can provide the data to the TestNG test cases using the data provider feature of TestNG.

Let us see how the excel file which we take the data from for our Hybrid Automation framework.

Test

Keyword

Object

ObjectType

Value

Login

 

 

 

 

 

OPENURL

 

 

url

 

ENTERTEXT

userid

XPATH

user1

 

ENTERTEXT

password

XPATH

password1

 

CLICK

btnLogin

XPATH

 

 

As this is a Data + Keyword-driven approach, we need to pass all the values from the excel sheet to our test by using the data-provider. Let us see the TestNG test class for the implementation of a data-provider method for the same:

package TestSuite;

import java.io.IOException;

import java.util.Properties;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

public class TestNGTest {

                WebDriver driver;

                @Test(dataProvider="crendentialData")

    public void testLogin(String testcaseName,String keyword,String objectName,String objectType,String value) throws Exception {     

    if(testcaseName!=null&&testcaseName.length()!=0){

                System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir"+"/testData.xlsx"));

                                this.driver= new ChromeDriver();

    }

      ObjectReader object = new ObjectReader();

      Properties allObjects = object.getObjectRepository();

      BaseActions operation = new BaseActions(driver);

    //Use of perform method to perform actions on UI

     operation.perform(allObjects, keyword, objectName,

                objectType, value);

    }

                @DataProvider(name="crendentialData")

    public Object[][] getDataFromDataprovider() throws IOException{

    Object[][] object = null;

    ExcelReader file = new ExcelReader();

    //Reading the sheet for keywords and data

    Sheet sheet = file.readExcel(System.getProperty("user.dir")+"\\","testData.xlsx" , "TestData");

    // Number of rows in excel sheet

    int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();

    object = new Object[rowCount][5];

    for (int i = 0; i < rowCount; i++) {

        //Loop to read all the rows

        Row row = sheet.getRow(i+1);

        //Create a loop to print cell values in a row

        for (int j = 0; j < row.getLastCellNum(); j++) {

            //Print excel data in console

            object[i][j] = row.getCell(j).toString();

        }

    }

    System.out.println("");

     return object;   

    }

                @AfterMethod

                void cleanup()

                {

                                driver.quit();

                }

}

Here we can see that the data provider method is providing the data to the TestNG test method with the help of “readExcel” method of the Excel reading utility.