Blog

Data-Driven Testing via Selenium WebDriver

Data-Driven Testing

What is Data-Driven Testing?

Data-driven testing (DDT) is a term that is mainly used in Automation testing. Data that include input parameters and values is stored in any external storage platform like database, spreadsheet or xml-files etc and then it is used iteratively in scripted tests.

Test scripts are created in Data Driven testing so that the tests can be run together with related data sets. Generic logic is created that can be reused for time saving and maintenance purposes. Besides these, it also improves the test coverage. These test inputs and outputs are separated from code. Data driven testing is mostly performed during Automation testing process.

Selenium Data-Driven Testing Using WebDriver

It is the responsibility of test developer to create and script data driven infrastructure so that the environment is capable of supplying different set of test data repeatedly and workflows that need to be tested against a particular application.

Practical Scenario:

First of all; let’s suppose we have a scenario to test the Gmail login functionality using various set of test data. So as per Automation testing standards and data driven approach used in automation testing, automation engineer will write a generic script to test the application with varying data values.

Automation engineer will create test scripts against this scenario:

  • Using valid credentials, test sign in functionality
  • Using username and blank password to test sign in functionality
  • Using blank username and password to test sign in functionality
  • And finally using no username and no password means send both fields blank to check the behavior of the sign in functionality

Now let’s implement this scenario in our practical environment.

To implement this scenario; first of all the automation engineer will need Eclipse IDE. TestNG can be installed in eclipse following a very simple method described below:

Adding TestNG in Eclipse

Open eclipse IDE and go to ‘Help’ from the menu bar, click on help to list down the options, now click ‘Install New Software…’ right on top of the last option ‘About Eclipse’. On clicking ‘Install New Software…’ a new window will open that contains field with label ‘Work with:’, write the following URL in that field and hit enter.”

URL: http://beust.com/eclipse

TestNG will be displayed in below section, tick the checkbox for this and click Next. Select the radio button option to accept the terms and conditions of license, click finish to complete wizard.

On successfully completing this wizard, TestNG is ready to be used in Eclipse IDE.

Create Java Project:

Now simply create new java project from file menu options, name the project as Gmail or whatever you want to and click finish and the java project will be created in Eclipse.

Add Package

Click on your project in package explorer and add package from new section to your project. Here I need to give package name so I name it as com.gmail to keep things simple.

Add TestNG to Project Creating Class

Now right click on the project title and click other in new section, here you will see the complete list in popup window, select TestNG as we added this in previous step. Select TestNG and name the class as gmail.login and finish wizard to add TestNG class successfully in your project.

Adding JARS

To successfully implement data driven testing using selenium, we need to add Selenium Server that can be downloaded from the following URL:
• http://docs.seleniumhq.org/download/

After this we need to add Excel JAR, excel JAR is needed to add because in our final step we are using data source in excel form
•http://sourceforge.net/projects/jexcelapi/files/jexcelapi/2.6.12/

and lastly we need to add TestNG JAR which can be downloaded from the following URL
•http://testng.org/doc/download.html

Right click on the project in package explorer in eclipse and go to ‘Build Path’ to click ‘Configure Build Path’. In new pop window open the tab named ‘Libraries’ and add the three downloaded JARs and click OK to finish.

Adding Data Source

It’s time to add the data source to our project, create an excel sheet carrying three columns i.e. Login, Username and Password. Fill the data source sheet as per the testing data and requirements.

Save data source i.e. excel sheet to your drive and include the file to the project created in eclipse.

Now automation engineer need to script the scenario in eclipse as per the required scenario.

The complete code is mentioned below:


package com.gmail;
import java.io.File;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;


public class gmailLogin {

//Excel API to read test data from excel workbook
private String[][] getExcelData(String xlPath, String shtName, String tbName) throws Exception{
String[][] tabArray=null;
Workbook workbk = Workbook.getWorkbook(new File(xlPath));
Sheet sht = workbk.getSheet(shtName);
int sRow,sCol, eRow, eCol,ci,cj;
Cell tableStart=sht.findCell(tbName);
sRow=tableStart.getRow();
sCol=tableStart.getColumn();
Cell tableEnd= sht.findCell(tbName, sCol+1,sRow+1, 100, 64000, false);
eRow=tableEnd.getRow();
eCol=tableEnd.getColumn();
System.out.println(“startRow=”+sRow+”, endRow=”+eRow+”, ” + “startCol=”+sCol+”, endCol=”+eCol);
tabArray=new String[eRow-sRow-1][eCol-sCol-1];
ci=0;
for (int i=sRow+1;i<eRow;i++,ci++){
cj=0;
for (int j=sCol+1;j<ecol;j++,cj++){
tabArray[ci][cj]=sht.getCell(j,i).getContents();
}
}
return(tabArray);
}

//Data provider definition
@DataProvider(name = “DP”)

public Object[][] createData() throws Exception{
Object[][] retObjArr=getExcelData(Excel workbook exact file path enclosed by “”, Sheet name of excel workbook enclosed by “”,”Login”);
return(retObjArr);
}

//Actual test functionality
@Test(dataProvider = “DP”)
public void loginGmail(String Usrname, String Pwd){

//Firefox browser instantiation
WebDriver driver = new FirefoxDriver();

//Loading the gmail URL

driver.get(“http://www.gmail.com”);

//User name input field identification and data entered
WebElement usernametext = driver.findElement(By.name(“Email”));
usernametext.sendKeys(Usrname)

//Password input field identification and data entered
WebElement passwordtext = driver.findElement(By.name(“Passwd”));
passwordtext.sendKeys(Pwd);

//Sign in button identification and click it
WebElement signinbutton = driver.findElement(By.name(“signIn”));
signinbutton.click();

//Closing the browser
driver.close();
}
}

Final look of the project will be as following image:

 

Run Project: Execute data driven testing

Simply right click on the project and run as TestNG Test. Firefox browser will be launched performing Gmail login functionality as per each data row available in the attached data source. Project will take data set from attached data source and execute in Firefox browser one by one.

Conclusion:

So that was all about implementing data driven testing using selenium web driver. I hope this proves to be helpful for all the automation engineers who are heading up to implementing data driven testing using selenium web driver. It is easy to modify data source according to the requirements and implement testing using same script for different scenarios.