Data-driven testing (DDT) is taking a test, parameterizing it and then running that test with varying data. This allows you to run the same test case with many varying inputs, therefore increasing coverage from a single test. In addition to increasing test coverage, data driven testing allows the ability to build both positive and negative test cases into a single test. Data-driven testing allows you to test the form with a different set of input values to be sure that the application works as expected.
It is convenient to keep data for automated tests in special storages that support sequential access to a set of data, for example, Excel sheets, database tables, arrays, and so on. Often data is stored either in a text file and are separated by commas or in Excel files and are presented as a table. If you need to add more data, you simply modify the file either in any text editor or in Microsoft Excel (in case of hard-coded values, you should modify both data and code).
Data-driven test includes the following operations performed in a loop:
• Retrieving input data from storage
• Entering data in an application form
• Verifying the results
• Continuing with the next set of input data
Data Driven Testing can be understood by the following diagram:
Pre-requisites:
- Java JDK 1.5 or above
- Apache POI library v3.8 or above
- Eclipse 3.2 above
- Selenium server-standalone-2.47.x.jar
- TestNG-6.9
Data Excel
Scenario -Open the application and login with different username and password. This data will be coming from excel sheet
Step 1: The first and the foremost step is to create the test data with which we would be executing the test scripts. Download JAR files of Apache POI and Add Jars to your project library. Let us create an excel file and save it as “Credentials.xlsx” and place it in the created package location.
We will use the data excel file and the file looks as below
Step 2: Create a POM class file under com.coe.pom name it as “Loginpage.java”. Inside a login page we write code to identify the webelements of login page using @FindBy annotation. To initialize the web elements we use initelements of page factory class. We utilize the elements by writing a method to it.
Step 3: Create a ‘New Class‘file, by right click on the package com.coe.script and select New > Class and name it as “SuperClass.java”, and create a new class file with a name “ValidLoginLogout.java”.
Step 4: Create some test data in excel that we will pass to script. For demo purpose I have taken username and password in excel.
Step 5: Copy and paste the below mentioned code under com.coe.pom package class.
Superclass: SuperClass.java
package com.coe.scripts;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
public class SuperClass {
public WebDriver driver;
@BeforeClass
public void preCondition()
{
//initialize the Firefox driver
driver = new FirefoxDriver();
// code is to open the URL in browser
driver.get("http://XYZ:3241/");
//code is to maximize the Firefox Driver instance
driver.manage().window().maximize();
//code is to wait for 30 seconds
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@AfterClass
public void postCondition() throws InterruptedException
{
Thread.sleep(5000);
//code is to close the browser
driver.quit();
}
}
Step 6: Copy and paste the below mentioned code under Generic package class and this code is to read the data from excel sheet.
Excelclass: Excel.java
package Generics;
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class Excel
{
public static String getCellValue(String xl, String Sheet, int r, int c)
{
try
{
FileInputStream fis = new FileInputStream(xl);
Workbook wb = WorkbookFactory.create(fis);
Cell cell = wb.getSheet(Sheet).getRow(r).getCell(c);
return cell.getStringCellValue();
}
catch (Exception e)
{
return "";
}
}
public static int getRowCount (String xl, String Sheet)
{
try
{
FileInputStream fis = new FileInputStream(xl);
Workbook wb = WorkbookFactory.create(fis);
return wb.getSheet(Sheet).getLastRowNum();
}
catch (Exception e)
{
return 0;
}
}
}
Classname: LoginPage.java
package com.coe.pom;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import com.coe.scripts.SuperClass;
public class LoginPage extends SuperClass {
//Locating Login button
@FindBy (xpath="//a[contains(text(),'Login')]")
private WebElement loginBtn;
//Locating Email id Text box
@FindBy (id="MemberEmail")
private WebElement emailIdField;
//Locating password Text box
@FindBy (id ="Password")
private WebElement psswd;
//Locating Login button
@FindBy (id="btnSubmit")
private WebElement loginBtn;
//Initializing the Objects
public LoginPage(WebDriver driver)
{
PageFactory.initElements(driver, this);
}
//Clicking on Login button
public void clickLoginBtn()
{
loginBtn.click();
}
//Specifiying email and psswd
public void Login (String UN,String PWD)
{
emailIdField.sendKeys(UN);
psswd.sendKeys(PWD);
}
//Clicking on Login button
public void clickLoginBtn()
{
loginBtn.click();
}
}
Step 7: Copy and paste the below mentioned code under com.coe.scripts packge class.
Note- In below program we have 2 rows so only test case will execute 2 time with different data
Classname: ValidLoginLogout.java
package com.coe.scripts;
import org.testng.annotations.Test;
import Generics.Excel;
import com.coe.pom.HomePage;
import com.coe.pom.LoginPage;
public class ValidLoginLogout extends SuperClass{
@Test
public void verifyLoginWithValidCred () throws InterruptedException
{
// Clicking on Login button
LoginPage clickLoginBtn = new LoginPage(driver);
clickLoginBtn.clickLoginBtn();
// Reading the data from excel file by the specified path
String xl = "./excelFiles/Credentials.xlsx";
String Sheet = "Sheet3";
int rowCount = Excel.getRowCount(xl, Sheet);
for (int i=1;i<=rowCount;i++)
{
String UserName=Excel.getCellValue(xl, Sheet, i, 0);
String Pwd=Excel.getCellValue(xl, Sheet, i, 1);
//Passing Username and password as parameters
LoginPage login = new LoginPage(driver);
login.Login(UserName,Pwd);
//Submitting the data by clicking on login button
LoginPage clckLoginBtn = new LoginPage(driver);
clckLoginBtn.clickLoginBtn();
//Clicking on setting icon in home page
HomePage clckSettingsIcon = new HomePage(driver);
clckSettingsIcon.clickSettingIcon();
//Clicking on Logout link in home page
HomePage clckLogoutLnk = new HomePage(driver);
clckLogoutLnk.clickLogoutLink();
Thread.sleep(1000);
// Again clicking on Login button to continue the loop
clickLoginBtn.clickLoginBtn();
}
}
Result: