Data Driven Framework with Selenium WebDriver

Data Driven Framework with Selenium WebDriver provides complete real time knowledge on how to prepare data driven framework in selenium webdriver with jxl jar in Selenium.In order to prepare Data Driven framework i am using jxl jar for xls files to read the data rather than using any other files,why because most of the testers writing test case in Microsoft excel files so testers easily understand the test case in order to execute or prepare the test scripts in Selenium WebDriver.

Data Driven Framework with Selenium WebDriver

Data Driven Framework with Selenium WebDriver


How to Do it

In order to prepare Data Driven Framework with Selenium WebDriver you can use test data for xls file,jxl jars,configure build path for Selenium WebDriver i.e all the selenium-java client jars,and i am using Test NG test framework with @DataProvider annotation ,String array[] to read the data from xls file.Please find the below screenshot of Test Data in xls file.


In Above excel i am using Firstname,Mobile_No to pass in registration screen in order to enter the details one by one,first iteration it will enter Firstname,Mobile number and click on Submit.In the second iteration it will enter Firstname,Mobile_No and click on submit..like this it will run the script until completion of test data last row.Let's write the script now.

I am using two classes to create data driven framework with Selenium WebDriver using extends keyword in order to access driver objects in all the classes.

Selenium Xpath Tutorial

Read Data from properties file

SelWebDriver Class

SelWebDriver class contains objects of drivers to call in all the classes to run the webdriver instead of declaring in each and every class,let's write the code as below.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;

public class SelWebDriver {
 //Declare driver objects
 public static WebDriver driver;
 public static String baseUrl;
  
 @BeforeTest
 public void setUp(){
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  baseUrl = "http://www.amazon.in";
  driver.get(baseUrl);   
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
   
  }
}

In Above code just initialized Selenium WebDriver objects i.e driver and storing the URL into String i.e baseUrl, Here i am using static keyword it means this values will not change it is final.

For Test case preparation we require another class as below.


package com.java.seleniumdatadriven;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class CustomerRegistration extends ExcelOperations {
 
 @Test(dataProvider="Cust_Reg")
 public void Login(String firstname,String mobile_no){
  
  System.out.println("Test method");
  String actual = driver.getTitle();
  driver.findElement(By.xpath("//a[@id='nav-link-yourAccount']")).click();
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
  driver.findElement(By.id("createAccountSubmit")).click();
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  driver.findElement(By.xpath("//input[@id='ap_customer_name']")).sendKeys(firstname);
  driver.findElement(By.id("ap_phone_number")).sendKeys(mobile_no);
  driver.findElement(By.xpath("//input[@class='a-button-input']")).click();
  String Expected = "Expected Title";
  try{
   Assert.assertEquals(actual, Expected);
   System.out.println("Test Case is pass");
  }catch(Exception e){
   System.out.println("Test Case is Fail "+e.getMessage());
   
  }
  

}
 
 @DataProvider(name = "Cust_Reg")
 public static String[][] readingExcel() throws BiffException, IOException{
  
  //Read File first
  FileInputStream testFiles = new FileInputStream("E:\\Automation_Testing\\Customer_Registration_1.0.xls");
  //Now read orkbook
  Workbook workbk =Workbook.getWorkbook(testFiles);
  
  //Now get Sheet
  Sheet sh = workbk.getSheet(0);
  //Now Count number of rows
  int rows = sh.getRows();
  System.out.println("Total Number of rows: " +rows);
  //Now count number of Columns
  int columns = sh.getColumns();
  System.out.println("Total Number of columns: " +columns);
  
  //Now save the excel data into string array because it is a two dimensional array
  String inputData[][] = new String[rows-1][columns];
  int count =0;
  //Now use For loop
  for(int i=1;i<rows;i++){
   for(int j=0;j<columns;j++){
    //Now read Cells 
    Cell cells = sh.getCell(j,i);
    inputData[count][i] = cells.getContents();
    System.out.println(inputData[count][i]);
   }
   count ++;
  }
  
  return inputData;
  
 }
 
}

As per above code i have used @DataProvider annotation which is TestNG framework annotation used to read the data from different input files.

Let's see the explanation in details


@Test(dataProvider="Cust_Reg")
 public void Login(String firstname,String mobile_no){
  
  System.out.println("Test method");
  String actual = driver.getTitle();
  driver.findElement(By.xpath("//a[@id='nav-link-yourAccount']")).click();
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
  driver.findElement(By.xpath("//input[@id='ap_customer_name']")).sendKeys(firstname);
  driver.findElement(By.id("ap_phone_number")).sendKeys(mobile_no);
  driver.findElement(By.xpath("//input[@class='a-button-input']")).click();
  String Expected = "Expected Title";
  try{
   Assert.assertEquals(actual, Expected);
   System.out.println("Test Case is pass");
  }catch(Exception e){
   System.out.println("Test Case is Fail "+e.getMessage());
   
  }


In Above code we are using (dataprovider="Cust_Reg") where Cust_Reg name is declared in @DataProvider annotation we have to mention name of provider in @Test method then

1.Mention Parameters of excel in @Test Method to pass into sendKeys();
i.e String Firstname,String Mobile_No
2.Just you need to pass those String parameter names to elements as  a test data as below
driver.findElement(By.xpath("//input[@id='ap_customer_name']")).sendKeys(firstname);
driver.findElement(By.id("ap_phone_number")).sendKeys(mobile_no);


Now run @Test method class as Run as --Test NG Test.It will run the test script properly and pass the parameters into test data i.e First Name,Mobile number into registration form.

Please watch Data Driven Framework with Selenium WebDriver video



Test Results:

Total Number of rows: 2
Total Number of columns: 2
Rajesh kuchana
8765678909
Test method
Test Case is pass
PASSED: Login("Rajesh Kuchana", "8765678909")

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Please provide your valuable comments on this post.Thank you for reading.


Post a Comment

0 Comments