Tuesday 4 August 2015

Keyword driven framework example :

Step 1: We will define a class called KeyWordExample, which will have all the resuable methods, driver invocation, taking the screen shot and reporting mechanism.

package com.keyword.sample;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

public class KeyWordExample {

    static WebDriver driver;
    static WebDriverWait wait;

    public void open_Browser(String browserName) {
        try {
            if (browserName.equalsIgnoreCase("Firefox")) {
                driver = new FirefoxDriver();
            } else if (browserName.equalsIgnoreCase("chrome")) {
                System.setProperty("webdriver.chrome.driver",
                        "D:/Jars/chromedriver.exe");
                driver = new ChromeDriver();
            } else if (browserName.equalsIgnoreCase("IE")) {
                System.setProperty("webdriver.ie.driver",
                        "D:/Jars/IEDriverServer.exe");
                driver = new InternetExplorerDriver();
            }
        } catch (WebDriverException e) {
            System.out.println(e.getMessage());
        }
    }

    public void enter_URL(String URL) {
        driver.navigate().to(URL);
    }

    public By locatorValue(String locatorTpye, String value) {
        By by;
        switch (locatorTpye) {
        case "id":
            by = By.id(value);
            break;
        case "name":
            by = By.name(value);
            break;
        case "xpath":
            by = By.xpath(value);
            break;
        case "css":
            by = By.cssSelector(value);
            break;
        case "linkText":
            by = By.linkText(value);
            break;
        case "partialLinkText":
            by = By.partialLinkText(value);
            break;
        default:
            by = null;
            break;
        }
        return by;
    }

    public void enter_Text(String locatorType, String value, String text) {
        try {
            By locator;
            locator = locatorValue(locatorType, value);
            WebElement element = driver.findElement(locator);
            element.sendKeys(text);
        } catch (NoSuchElementException e) {
            System.err.format("No Element Found to enter text" + e);
        }
    }

    public void click_On_Link(String locatorType, String value) {
        try {
            By locator;
            locator = locatorValue(locatorType, value);
            WebElement element = driver.findElement(locator);
            element.click();
        } catch (NoSuchElementException e) {
            System.err.format("No Element Found to enter text" + e);
        }
    }

    public void click_On_Button(String locatorType, String value) {
        try {
            By locator;
            locator = locatorValue(locatorType, value);
            WebElement element = driver.findElement(locator);
            element.click();
        } catch (NoSuchElementException e) {
            System.err.format("No Element Found to perform click" + e);
        }
    }
   
    public void close_Browser() {
        driver.quit();
    }
}
Step 2: We will define other class called KeyWordExecution, which takes the responsibility of retrieving the data from excel sheet, identify the locators and parameters and invoke the respective methods in the 'KeyWordExample' class.

package com.keyword.sample;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class KeyWordExecution {

    public void runReflectionMethod(String strClassName, String strMethodName,
            Object... inputArgs) {

        Class<?> params[] = new Class[inputArgs.length];

        for (int i = 0; i < inputArgs.length; i++) {
            if (inputArgs[i] instanceof String) {
                params[i] = String.class;
            }
        }
        try {
            Class<?> cls = Class.forName(strClassName);
            Object _instance = cls.newInstance();
            Method myMethod = cls.getDeclaredMethod(strMethodName, params);
            myMethod.invoke(_instance, inputArgs);

        } catch (ClassNotFoundException e) {
            System.err.format(strClassName + ":- Class not found%n");
        } catch (IllegalArgumentException e) {
            System.err
                    .format("Method invoked with wrong number of arguments%n");
        } catch (NoSuchMethodException e) {
            System.err.format("In Class " + strClassName + "::" + strMethodName
                    + ":- method does not exists%n");
        } catch (InvocationTargetException e) {
            System.err.format("Exception thrown by an invoked method%n");
        } catch (IllegalAccessException e) {
            System.err
                    .format("Can not access a member of class with modifiers private%n");
            e.printStackTrace();
        } catch (InstantiationException e) {
            System.err
                    .format("Object cannot be instantiated for the specified class using the newInstance method%n");
        }
    }

    public static void main(String[] args) {
        KeyWordExecution exeKey = new KeyWordExecution();
        ReadExcel excelSheet = new ReadExcel();
        excelSheet.openSheet("D:/testCaseSheet.xls");
        for (int row = 1; row < excelSheet.getRowCount(); row++) {
            List<Object> myParamList = new ArrayList<Object>();
            String methodName = excelSheet.getValueFromCell(0, row);
            for (int col = 1; col < excelSheet.getColumnCount(); col++) {
                if (!excelSheet.getValueFromCell(col, row).isEmpty()
                        & !excelSheet.getValueFromCell(col, row).equals("null")) {
                    myParamList.add(excelSheet.getValueFromCell(col, row));
                }
            }

            Object[] paramListObject = new String[myParamList.size()];
            paramListObject = myParamList.toArray(paramListObject);

            exeKey.runReflectionMethod("com.keyword.sample.KeyWordExample",
                    methodName, paramListObject);
        }
    }
}

Step 3: We will create an other class to read the excel sheet. We have used jxl library to read the data from excel. You can also use 'Apache POI' to do the same.



package com.keyword.sample;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ReadExcel {

    Workbook wbWorkbook;
    Sheet shSheet;

    public void openSheet(String filePath) {
        FileInputStream fs;
        try {
            fs = new FileInputStream(filePath);
            wbWorkbook = Workbook.getWorkbook(fs);
            shSheet = wbWorkbook.getSheet(0);

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

    public String getValueFromCell(int iColNumber, int iRowNumber) {
        return shSheet.getCell(iColNumber, iRowNumber).getContents();
    }

    public int getRowCount() {
        return shSheet.getRows();
    }

    public int getColumnCount() {
        return shSheet.getColumns();
    }
}
The below is the excel file which has provided with four columns :
Project Structure

The main advantage going for keyword driven framework is 'Re-usability', we can re-use the same methods for number of test cases. We can extend the framework by increasing flexibility with minimum effort.

Hope this helps you in understanding about keyword driven framework.

No comments:

Post a Comment