Data Driven Framework Part 2

Data driven example with excel sheet - Please refer below source codes to understand the implementation of Data Driven Testing/Framework using excel sheet.

In this example we are passing the username and password using excel sheet to test the login functionality.

Excel Sheet with user credentials:

Base Class: In this base class we have created following methods

setup() - to launch the browser and navigate to application
tearDown() - quit the browser
getExcelData() - to get the excel test data and store using 2-D Array.
and we have created the object of NewExcelLibrary to use the different methods to access the excel sheet. Complete excel library you can find here.
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import com.utility.NewExcelLibrary;

public class BaseClass {
 public WebDriver driver;
 NewExcelLibrary obj= new NewExcelLibrary("D:\\Workspace_Eclipse\\DataDriven\\TestData\\User.xlsx");
 public void setup() {
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32 (2)\\chromedriver.exe");
  driver = new ChromeDriver();

 public void tearDown() {
 @DataProvider(name ="Credentials1")
 public Object[][] getExcelData() {
  //Totals rows count
  int rows=obj.getRowCount("Data");
  //Total Columns
  int column=obj.getColumnCount("Data");
  int actRows=rows-1;
  Object[][] data= new Object[actRows][column];
  for(int i=0;i<actRows;i++) {
   for(int j=0; j<column;j++) {
    data[i][j]=obj.getCellData("Data", j, i+2);
  return data;

TestClass: Supply the dataProvider name in test method

import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.base.BaseClass;

public class DataDrivenTest extends BaseClass {

 @Test(dataProvider = "Credentials1")
 public void loginTest(String username,String password) {
  String actualURL=driver.getCurrentUrl();
  String expectedURL="";
  Assert.assertEquals(actualURL, expectedURL);

Output: Refer the below output, you can see our test executed with different set of test data

Please refer below YouTube video to understand better.

No comments:

Post a Comment