Keyword Driven Framework in Selenium Webdriver (Hybrid : KeywordDriven + TestNG + DataDriven)


Framework: It is a set of Guidelines designed by an Expert in a generic way to accomplish a task in an effective and efficient manner.If you are using selenium webdriver and wants to create  Keyword Driven Framework from scratch then follow bellow given steps.It will guide how to create hybrid framework .

Keyword Driven Framework is one of well known automation framework and extensively used in various service offering companies. Keyword Driven Framework also known as table-driven Framework or action word based framework.Here we’ll use some keywords as the driving parameters in data driven testing the data sheet. The keywords will be defined by the user, and let’s call them user defined keywords.The highlighted text in the below screenshot are the keywords.

KeywordTestcases

A Keyword or Action Word is a defined combination of actions on a test object which describes how test lines must be executed. An action word contains arguments and is defined by a test analyst.

In simple words In Keyword Driven Framework, Keywords are assigned to Reusable components for an example “Login” keyword assigned to Login method which has automated script to perform login operation.

How it Works?:Action Library contains the keywords and keywords has been used to create the test cases. for data driven, keyword fetches the data from data reader script and after execution it pushes the results to result files.
in many keyword driven automation frameworks we have found that test cases are written in excel file along with test data and then automation framework execute the test steps by reading the excel row one by one.You can maintain an Object Repository file.OR_Keyword

Even though Keyword driven frameworks might take more time to create, there are many advantages in the framework.

Automation tool independent
Application independent
Programming language independent
Easy to read test scripts
Easy to add new test scripts & increases user productivity.
Very easy to maintain
Test script can be created even when the application is not ready for test.

Here i’m sharing the sample code.The Testcases for Yahoo mail and google home page.RunnerClass

Click here to get the sample source code.

Create a class ‘Driver_yahoo’.This is the Test Runner Class.This class helps you to read the data from excel and executes the desired testcases.If you notice the Excel sheet contains a keyword ‘yes’ and ‘no’.This means, It will execute the testcase which contains a keyword ‘yes’.

Please copy and paste the code in your ide.

import java.io.FileInputStream;

import java.lang.reflect.Method;

import jxl.Sheet;
import jxl.Workbook;

import org.testng.annotations.Test;

public class Driver_yahoo {
static Workbook wb;
static Sheet ws, ws1, ws2, ws3;
static FileInputStream fs;
static String testr, classname, func;

@Test(priority = 1)
public static void fn_executetests() throws Exception {

fs = new FileInputStream(“F:\\Workspace\\Keyword\\testdata\\data.xls”);
wb = Workbook.getWorkbook(fs);
// get TestCases sheet
ws1 = wb.getSheet(1);
for (int r = 1; r < ws1.getRows(); r++) {
testr = ws1.getCell(7, r).getContents();

if (testr.matches(“yes”)) {
classname = ws1.getCell(5, r).getContents();
Class c = Class.forName(classname);
func = ws1.getCell(6, r).getContents();
Method m = c.getMethod(func, null); // //datatype for
// arguments,,,, if no
// arguments null
Object obj = c.newInstance();
m.invoke(obj, null); // //////pass the values for the method

}
}
}

}

Now Create a class yahoo.(Never mind: Class name must always starts with UpperCase letter). 

Here i’m declaring methods and calling the OR locators using JXL.

import java.io.FileInputStream;

import jxl.Workbook;

import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Reporter;

public class yahoo extends Driver_yahoo
{
static String url;
static FirefoxDriver fd=new FirefoxDriver();
public static String subj;
public static void fn_open() throws Exception
{
//get sheet0 having URL
fn_getdata(0);
url=ws.getCell(1,1).getContents();
fd.get(url);
fd.manage().window().maximize();

Thread.sleep(3000);
}
public static void fn_login() throws Exception
{

//get sheet2 and 3 having obj and testdata
fn_getdata(2,3);
fd.findElement(By.name(ws2.getCell(0,1).getContents())).sendKeys(ws3.getCell(0, 1).getContents().trim());
fd.findElement(By.name(ws2.getCell(0,2).getContents())).sendKeys(ws3.getCell(0, 2).getContents().trim());
fd.findElement(By.name(ws2.getCell(0,3).getContents())).click();
Thread.sleep(15000);

}
public static void fn_compose() throws Exception
{
//get sheet2 and 3 having obj and rep
fn_getdata(2,3);
subj=ws3.getCell(1,2).getContents();
fd.findElement(By.xpath(ws2.getCell(1,1).getContents().trim())).click();
Thread.sleep(5000);
fd.findElement(By.id(ws2.getCell(1,2).getContents())).sendKeys(ws3.getCell(1,1).getContents());
fd.findElement(By.id(ws2.getCell(1,3).getContents())).sendKeys(subj);
fd.findElement(By.name(ws2.getCell(1,4).getContents())).sendKeys(ws3.getCell(1,3).getContents());
fd.findElement(By.id(ws2.getCell(1,5).getContents())).click();
Thread.sleep(7000);
fd.findElement(By.id(“inbox_back”)).click();
Thread.sleep(2000);
}
public static void fn_checkmail() throws Exception
{
//get sheet2 having obj
fn_getdata(2);
fd.findElement(By.linkText(ws.getCell(2,1).getContents())).click();
Thread.sleep(8000);
String sub=fd.findElement(By.xpath(ws.getCell(2,2).getContents())).getText().trim();
if (sub.matches(subj))
{
Reporter.log(“<b><font color=’green’>Mail is received</font></b> “+sub);
}
else
{
Reporter.log(“<b><font color=’red’>Mail NOT received</font></b> “+sub);
}
}
public static void fn_logout()
{
fd.findElement(By.linkText(“Sign Out”)).click();
fd.close();
}
private static void fn_getdata(int s1) throws Exception
{
fs=new FileInputStream(“F:\\Workspace\\Keyword\\testdata\\data.xls”);
wb=Workbook.getWorkbook(fs);
ws=wb.getSheet(s1);
}
private static void fn_getdata(int s1,int s2) throws Exception
{
fs=new FileInputStream(“F:\\Workspace\\Keyword\\testdata\\data.xls”);
wb=Workbook.getWorkbook(fs);
ws2=wb.getSheet(s1);
ws3=wb.getSheet(s2);
}

}

Now Create a class google1

import org.openqa.selenium.By;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Reporter;

public class google1 {
static FirefoxDriver fd = new FirefoxDriver();

public static void fn_searchdata() throws Exception {

fd.get(“http://www.google.co.in&#8221;);
Thread.sleep(3000);
fd.findElement(By.name(“q”)).sendKeys(“seleniumbycharan”);
fd.findElement(By.name(“btnG”)).click();
Thread.sleep(3000);
if (fd.findElement(By.id(“resultStats”)).isDisplayed()) {
System.out.println(“Results are displayed”);
Reporter.log(“Results are displayed”);
} else {
System.out.println(“Results are not displayed”);
Reporter.log(“Results are not displayed”);
}
}

public static void fn_check() throws Exception {
Thread.sleep(3000);
String t = fd.getTitle();
if (t.contains(“Google”)) {
Reporter.log(“Google page is displayed correct”);
} else {
Reporter.log(“Google page is not displayed correct”);
}
}

}

The above methods will be sync with the column name ‘methodname‘ present in excel sheet.

When the keyword matches with ‘yes’ then that respective method will be called.We can also integrate this with page object model.Its always good to have Page objects.

Note : This is just a sample framework, which helps you to understand and let design your own UserDefiined Framework.You can find the source code here. Download

Summary :

  • User is responsible for creating the test scripts in the keyword driven format in a spreadsheet as shown above.
  • Object repository file is updated once the application is ready.
  • Keyword driven tests refer to both Object repository file & translation files to execute a step.
Advertisements

8 thoughts on “Keyword Driven Framework in Selenium Webdriver (Hybrid : KeywordDriven + TestNG + DataDriven)

  1. Abhijit September 14, 2015 / 2:20 pm

    Hi,

    thanks for this article…i was looking this one only.

    but hey i have one question. i have below situation i don’t know what approach i should follow.

    scenario :

    I want to create three excel sheet namely “Master Test sheet”, Test Suit”, “Test Cases”.
    Master Test sheet will contain list of all test suit name, Test Suit excel sheet contain list of all test cases for that suit & last excel file namely Test cases contains test steps with keywords.

    So, i want my driver script to read that Master Test sheet file & Test Suit file. and then execute test cases mentioned in Test case file.

    So my question should i create three objects for above three excel file use those three objects through out execution or load contents of above two files (Master Test sheet & Test Suit) into an array.

    which approach should i go. please suggest.

    Like

    • charan163 September 14, 2015 / 2:35 pm

      In this case i’d suggest you to create the classes with name of your testSuits and i dont understand why, why you would like to create an object for Master Test Sheet.Anyway you will not run it.You better pass any keyword which will get the Testsuit information.

      Also please use page objects pattern,so that we could reuse it anywhere anytime.My example just tells you, ‘How to call’.

      Like

  2. VINOTHKUMAR March 22, 2017 / 9:17 pm

    How can i create a multiple classes? i want to run test thru testng.xml for parallel testing, also I see it runs only fn_executetests(), i created another class with priority 2 as my application has no.of functionalities, and i can’t keep those in single class so i want to create multiple classes and want to run those as well,how can i make it to run?

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s