Data Driven Testing Framework

Explanation: While automating or testing any application, at times it may be required to test the same functionality multiple times with the different set of input data. Thus, in such cases, we can’t let the test data embedded in the test script. Hence it is advised to retain test data into some external data base outside the test scripts. Data Driven Testing Framework helps the user segregate the test script logic and the test data from each other. It lets the user store the test data into an external database. The external databases can be property files, xml files, excel files, text files, CSV files, ODBC repositories etc. The data is conventionally stored in “Key-Value” pairs for properties files. Thus, the key can be used to access and populate the data within the test scripts.

Login Data Sheet of Facebook.xls file

Locators Sheet of Facebook.xls file

View Facebook_login.xls File


package com.datadriven.test;

import java.io.FileInputStream;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import jxl.Sheet;
import jxl.Workbook;
import utility.Helper;

public class DataDrivenFramework {
    WebDriver driver;

    public List getElements(String locatorName, String locatorValue) {
        List elements = null;
        switch (locatorName) {
        case "name":
            elements = driver.findElements(By.name(locatorValue));
            break;
        case "linkText":
            elements = driver.findElements(By.linkText(locatorValue));
            break;
        case "partialLinkText":
            elements = driver.findElements(By.partialLinkText(locatorValue));
            break;
        case "className":
            elements = driver.findElements(By.className(locatorValue));
            break;
        case "xpath":
            elements = driver.findElements(By.xpath(locatorValue));
            break;
        case "css":
            elements = driver.findElements(By.cssSelector(locatorValue));
            break;
        case "tagName":
            elements = driver.findElements(By.tagName(locatorValue));
            break;
        }
        return elements;
    }

    public WebElement getElement(String locatorName, String locatorValue) {
        WebElement element = null;
        switch (locatorName) {
        case "id":
            element = driver.findElement(By.id(locatorValue));
            break;
        case "name":
            element = driver.findElement(By.name(locatorValue));
            break;
        case "linkText":
            element = driver.findElement(By.linkText(locatorValue));
            break;
        case "partialLinkText":
            element = driver.findElement(By.partialLinkText(locatorValue));
            break;
        case "className":
            element = driver.findElement(By.className(locatorValue));
            break;
        case "xpath":
            element = driver.findElement(By.xpath(locatorValue));
            break;
        case "css":
            element = driver.findElement(By.cssSelector(locatorValue));
            break;
        case "tagName":
            element = driver.findElement(By.tagName(locatorValue));
            break;
        }
        return element;
    }

    public void performTask(WebElement element, String type, String... text) {
        switch (type) {
        case "text":
            element.sendKeys(text[0]);
            break;
        case "select":
            Select select = new Select(element);
            select.selectByVisibleText(text[0]);
            break;
        case "button":
            element.click();
            break;
        case "link":
            element.click();
            break;
        case "checkbox":
            element.click();
            break;
        case "radiobutton":
            element.click();
            break;
        }
    }

    @Test(dataProvider = "testData")
    public void facebookLogin(String userID, String pass) throws Exception {
        System.out.println(userID + "\t" + pass);
        FileInputStream fis = new FileInputStream("Facebook_Login.xls");
        Workbook wb = Workbook.getWorkbook(fis);
        Sheet sht = wb.getSheet("Locators");

        driver = Helper.launchBrowser("ff");
        driver.get("http://facebook.com");

        String locatorName, locatorValue;
        locatorName = sht.getCell(0, 0).getContents();
        locatorValue = sht.getCell(0, 1).getContents();
        performTask(getElement(locatorName, locatorValue), "text", userID);
        
        locatorName = sht.getCell(1, 0).getContents();
        locatorValue = sht.getCell(1, 1).getContents();
        performTask(getElement(locatorName, locatorValue), "text", pass);
    
        locatorName = sht.getCell(2, 0).getContents();
        locatorValue = sht.getCell(2, 1).getContents();
        performTask(getElement(locatorName, locatorValue), "button");
    }

    @DataProvider
    public Object[][] testData() throws Exception {
        Object[][] data = null;
        FileInputStream fis = new FileInputStream("Facebook_Login.xls");
        Workbook wb = Workbook.getWorkbook(fis);
        Sheet sht = wb.getSheet("Login Data");

        int rows = sht.getRows(),
              col = sht.getColumns();
        System.out.println("No of Rows: " + rows + "\tNo of Columns: " + col);

        data = new Object[rows-1][col];

        for (int i = 0; i < rows - 1; i++) {
            for (int j = 0; j < col; j++) {
                data[i][j] = sht.getCell(j, i + 1).getContents();
                System.out.print(data[i][j] + " ");
            }
            System.out.println();
        }
        return data;
    }
}

No comments:

Post a Comment