Selenium Data Driven Framework step by step

Selenium Data Driven Framework step by step
Welcome to Selenium Tutorials, in this Selenium Data Driven Framework step by step giving clear details about Data Driven Framework with selenium with real time examples.Most of the frameworks data driven is most important in order to run the script for positive and negative test cases and handling validation message.Framework can be finalized based upon web application (AUT) you are testing.If you are doing testing of big applications then you can go with Data Driven framework and keyword driven framework.

What is Data driven?

Data driven testing is nothing but testing the application with multiple set of data in order to verify how application behaves with positive and negative scenarios. Data driven testing can be done with the help of Excel,Text files,Csv files and Databases.In this post i am using Excel files to drive the application with multiple set up test data.Please follow below simple steps to create Data Driven Framework in Selenium WebDriver.

Pre-requisites:

Below are the important pre requisite for creating Selenium Data Driven Framework step by step,please make sure you should have below details.

ALSO READ  Run Selenium Script using runnable JAR

1.Selenium Webdriver
2.jxl jar
3.Excel files as a format of xls
4.Dataprovider in TestNG
5.TestNG Library

Excel file:

Here i’m using excel file for multiple set of data to drive the application for positive and negative scenarios and created the Test Scripts with TestNG annotations,those are

1.@Test
2.@DataProvider

Please read TestNG Tutorials for good understanding of TestNG annotations. Please find below Selenium Script for Data driven framework creation.

Selenium Script:

 package simple_package;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.util.concurrent.TimeUnit;
 import org.openqa.selenium.Alert;
 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 import jxl.Cell;
 import jxl.Sheet;
 import jxl.Workbook;
 import jxl.read.biff.BiffException;

 public class Customer_Registration {

  WebDriver driver;

  @Test(priority = 1)
  public void login() throws InterruptedException {

   driver = new FirefoxDriver();
   driver.manage().window().maximize();
   driver.get("http://www.wikishown.com");
   System.out.println("Browser is opened and wikishown url in entered.");

   driver.manage().window().maximize();
   Thread.sleep(2000);

   driver.findElement(By.id("userId")).sendKeys("xxxxxxxxx");
   driver.findElement(By.id("passId")).sendKeys("xxxxxxxx");
   driver.findElement(By.id("submit")).click();

   System.out.println("Wikishown My Account page is loaded");
  }

  //Calling Excel file input column names using String

  @Test(priority = 2, dataProvider = "Cust_Reg")
  public void customer_regis(String Mobileno, String Firstname, String Lastname) throws Exception,
   BiffException, IOException {

    driver.findElement(By.name("searchMobileNo")).clear();
    driver.findElement(By.name("searchMobileNo")).sendKeys(Mobileno);

    Thread.sleep(3000);
    driver.findElement(By.id("searchBtn")).click();

    System.out.println("search button is clicked");
    Thread.sleep(2000);

    driver.findElement(By.id("fname")).sendKeys(Firstname);
    driver.findElement(By.id("lname")).sendKeys(Lastname);

    driver.findElement(By.id("validateBtn")).click();
    System.out.println("Validate Button is clicked");
   }

  //Identification of Excel file
  @DataProvider(name = "Cust_Reg")
  public Object[][] CustRegistration() throws Exception {
   FileInputStream filepath = new FileInputStream("E:\Automation_Testing\TestData.xls");

   Workbook wb = Workbook.getWorkbook(filepath);
   Sheet sheet = wb.getSheet("Cust_Data");

   int row = sheet.getRows();
   System.out.println("number of rows" + row);
   int column = sheet.getColumns();
   System.out.println("number of columns" + column);
   String Testdata[][] = new String[row - 1][column];
   int count = 0;

   for (int i = 1; i < row; i++) {
    for (int j = 0; j < column; j++) {
     Cell cell = sheet.getCell(j, i);
     Testdata[count][j] = cell.getContents();
    }
    count++;
   }
   filepath.close();
   return Testdata;
  }
 }


Must Read

Launch Firefox Browser using GeckoDriver
Selenium Xpath Tutorial
Verify element is enable in Selenium
Read Data From properties file using Selenium
Selenium-Testng examples to execute multiple classes
Selenium WebDriver Methods
Generate HTML Reports using Selenium

In above script i have usesd below parameters

ALSO READ  How to run TestNG test cases using Maven

1.String Mobileno
2.String Firstname
3.String Lastname

these are the column names in the excel file which are using for Test Data as above image and we are passing these parameters in “customer_regis” method with the help of Dataprovider method as used in “public Object[][] CustRegistration()” method where i am calling Test data excel file with the help of FileInputStream filepath = new FileInputStream(“filepath”) and getting particular worksheet where data is stored ,calling that sheet in order to count no of rows ,no of columns to run until the end of the test data in file with the help of for loop as seen in CustRegistration method.

ALSO READ  How to send extent reports in email with screenshots

Now calling CustRegistration method i have declared Dataprovider name as above and using this name in public void customer_regis() method in order to pass the values in place of static data as below.

driver.findElement(By.id("fname")).sendKeys(Firstname);
driver.findElement(By.id("lname")).sendKeys(Lastname);

1.Save above script and replace with your AUT application URL and replace all your elements.
2.Close the excel sheet
3.Run the script with TestNG Test.

You will see application will run with multiple set up test data i.e Data driven Testing.Thank you for reading,Please provide your valuable comments on this topic so that i will modify the details more efficient.Please implement for your web application here i have given the code.

Rajesh K

One thought on “Selenium Data Driven Framework step by step

  1. Thanks for sharing a great article on Selenium!!

    Really it will be helpful for both beginners and experienced professional. Your information is very clear.

    Along with your post, I put my input here.

    To test this you have three different approaches:

    1) Create 100 scripts one for each dataset and execute each test one by one.
    2) Change the data in the script and execute it multiple times.
    3) Import the data from the excel sheet and execute the script multiple times with different data.

Comments are closed.