Data Driven Framework | First Test | Part 3

Data Driven Framework Tutorial


At the end of this data driven framework tutorial you would be able to:

  • create a test script and execute it using TestNG
  • write a generic method
  • determine the status of the test using Assert

Our first test is to validate that login should be successful. This could be verified by checking the presence of Log Out link after a user is logged in, as shown in the below screenshot.


Data Driven Framework tutorial



Create Generic Method(s)


Generic methods are the methods that are used across the framework. These common methods are created in TestBase or parent class since parent class is extended by all other classes. In our scenario, we need to check the presence of an element (Log Out link) after the user is logged in. Also, this method could be used in other test classes as well.

Update the class by adding the following method (highlighted in bold):


package com.appliedselenium.base;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

public class TestBase {

    //initialize the WebDriver interface reference variable
    public static WebDriver driver;

    // Create instance of Properties class to refer the .properties file
    public static Properties or = new Properties();
    public static Properties config = new Properties();

    // Create filestream class object to load the file
    public static FileInputStream fis;
    //Generic Method to check the presence of an element
    //This method accepts 1 parameter by of type By and returns a boolean value

        public boolean isElementPresent(By by) {
            try {
                return true;
            }catch (NoSuchElementException e) {
                return false;

    // First method to be called before automation suite is executed 
    public void setUp() {
        //check if driver reference variable is null, i.e. no browser is opened through selenium yet
        if (driver == null) {

            //get the reference of file
            try {
                fis = new FileInputStream(
                        System.getProperty("user.dir") + "\\src\\test\\resources\\properties\\");
            } catch (FileNotFoundException e) {
            //load the file into memory
            try {

            } catch (IOException e) {

            //get the reference of file
            try {
                fis = new FileInputStream(
                        System.getProperty("user.dir") + "\\src\\test\\resources\\properties\\");
            } catch (FileNotFoundException e) {
            //load the file into memory
            try {
            } catch (IOException e) {

            // invoke the browser mentioned in config.prop and assign it to driver reference variable
            if (config.getProperty("browser").equals("chrome")) {
                        System.getProperty("user.dir") + "\\src\\test\\resources\\executables\\chromedriver.exe");
                driver = new ChromeDriver();

            //open the url and assign an implicit wait (as configured in


    // Mehod called after all the tests are executed
    public void tearDown() {
        System.out.println("Closing the browser");





Update file


Update file to include the locator value for Log Out link.


#Login Page

#Home Page
logout_XPATH=//a[contains(text(),'Log Out')]



Create First Test Class


All the test scripts will be created under com.appliedselenium.testscripts package. Also, all the test script class names should end with Test. This is how maven interprets the test classes.

Right-click on package com.appliedselenium.testscripts -> New -> Class and create a new class CheckLoginTest. In this class, we will perform the following:

  • extend the TestBase class
  • create a method testLogin() and use @Test annotation to indicate it is a test method
  • enter username and password from
  • click on the login button and verify (Assert) that the Log Out link is displayed

Paste the following code in the CheckLoginTest class:


package com.appliedselenium.testscripts;

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

import com.appliedselenium.base.TestBase;

//Test class name should end with Test
//All the test classes must extend TestBase
public class CheckLoginTest extends TestBase {

    // @Test annotation is used to create a test method
    public void testLogin() {

        // enter the username and password. These values are stored in
        // file and locators are stored in
        // file



        // create a method isElementPresent() in TestBase to verify the presence of an element
        // if element is found then pass the test else fail the test with message "Login not successful"
        Assert.assertTrue(isElementPresent(By.xpath(or.getProperty("logout_XPATH"))), "Login not successful");




The values of locators and fields are read from and respectively using the getProperty() method. Verification is done using Assert.assertTrue(boolean, message).

The first parameter is the generic method isElementPresent(by) which accepts the locator value of Log Out link. If this link is found then the test is pass, else the test will fail with the message “Login not successful”.


Executing The Test Script


We are using data driven approach for this tutorial, but the underlying framework is TestNG. To execute this test, we should have a testng.xml file (later we will execute this project using maven). To create this XML file, we will convert our project to TestNG.

Right-click on the project -> TestNG -> Convert to TestNG. This will open the below window.


Data driven framework tutorial


Update the following things in the above window:

  • change the Location path to DataDrivenPattern/src/test/resources/testNG/testng.xml
  • remove the entry for TestBase class. testng.xml should contain only test classes and listeners, not the base class.
  • change the suite name to project name, this is optional though

The final window should look like:


Data driven framework tutorial


Click on the Finish button and the testng.xml file should get created inside the testNG folder. You may also change the test name (highlighted below) to match your test method.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "">
<suite name="Data Driven Approach">
  <test thread-count="5" name="testLogin">
            <class name="com.appliedselenium.testscripts.CheckLoginTest"/>
  </test> <!-- Test -->
</suite> <!-- Suite -->


To execute the test, right-click on testng.xml – > Run As -> TestNG suite. The test should get executed successfully and 2 reports should also get generated inside the test-output folder (refresh the project to see the reports).

The first report is index.html, which gives high-level information about the test. Another report is emailable-report.html which can be used to share with stakeholders. However, we will not use these reports, but rather the infamous extent report.

In the next tutorial, we will learn how to integrate logs (log4j) in our framework.


Author: Dhawal Joshi

A post-graduate in MCA, ISTQB & ITIL certified QA with more than 8 years of experience in QA working with a CMMI Level 5 organization as System Analyst. I started my automation journey with HP UFT(formerly known as QTP) and for the past few years, I am using Selenium for automation. I also have experience in Android Application Development, Java, HTML, and VBScript. When I am not working, I like to spend time with my family, cooking and learning new developments in IT.

Leave a Reply

Your email address will not be published. Required fields are marked *