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 TestBase.java class by adding the following method (highlighted in bold):

 

package com.appliedselenium.base;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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.openqa.selenium.chrome.ChromeDriver;
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 {
                driver.findElement(by);
                return true;
            }catch (NoSuchElementException e) {
                return false;
            }
            
        }

    // First method to be called before automation suite is executed 
    @BeforeSuite
    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 or.properties file
            try {
            
                fis = new FileInputStream(
                        System.getProperty("user.dir") + "\\src\\test\\resources\\properties\\or.properties");
            } catch (FileNotFoundException e) {
                
                e.printStackTrace();
            }
            
            //load the file into memory
            try {
                or.load(fis);

            } catch (IOException e) {
                
                e.printStackTrace();
            }

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

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

            //open the url and assign an implicit wait (as configured in config.properties)
            driver.get(config.getProperty("url"));
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(Integer.parseInt(config.getProperty("implicit_wait")),
                    TimeUnit.SECONDS);
        }

    }

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

    }

}

 

 

Update or.properties file

 

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

 

#Login Page
username_XPATH=//input[@class='input'][@name='username']
passwd_XPATH=//input[@class='input'][@name='password']
loginBtn_XPATH=//input[@class='button'][@type='submit']

#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 config.properties
  • 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
    @Test
    public void testLogin() {

        // enter the username and password. These values are stored in config.properties
        // file and locators are stored in
        // or.properties file
        driver.findElement(By.xpath(or.getProperty("username_XPATH"))).sendKeys(config.getProperty("user_name"));

        driver.findElement(By.xpath(or.getProperty("passwd_XPATH"))).sendKeys(config.getProperty("password"));

        driver.findElement(By.xpath(or.getProperty("loginBtn_XPATH"))).click();

        // 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 or.properties and config.properties 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 "http://testng.org/testng-1.0.dtd">
<suite name="Data Driven Approach">
  <test thread-count="5" name="testLogin">
    <classes>
            <class name="com.appliedselenium.testscripts.CheckLoginTest"/>
    </classes>
  </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 e-mail address will not be published. Required fields are marked *