Keyword Driven Framework In Selenium With Examples

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 7, 2024

This Comprehensive Tutorial on Keyword Driven Framework Explains Various Components of the Framework & How to Create One in Selenium:

In general, Framework is a set of guidelines, which when followed will give beneficial results.

The Keyword-Driven framework is a technique to externalize keywords/actions that are used in the script in a separate Object Repository (here, it is a Java Class file), which will give positive outcomes like increased code-reusability, reduced script maintenance, and higher portability.

=> Take A Look At The Selenium Beginners Guide Here.

Keyword Driven Framework In Selenium


What Is A Keyword Driven Framework In Selenium?

Here are the Video Tutorials:

Keyword Driven Framework – Plan & Design


Creation of Keyword Driven Framework Part – I


Creation of Keyword Driven Framework Part – II

Components Of Keyword Driven Testing Framework

Given below are the list of components that are involved in the Keyword Driven Framework.

  1. Function Library
  2. Excel Sheet to store Keywords
  3. Design Test Case Template
  4. Object Repository for Elements/Locators
  5. Test Scripts or Driver Script

#1) Function Library

This is usually a Java Class file where the Keywords are defined. In other words, all the actions that are performed on the application are defined as user-defined methods (which are keywords) in the library class file.

For Example:

Let us assume that our application has to perform the following actions in one or more test cases:

  • Enter the URL.
  • Click on an element.
  • Type in a textbox.

Then, the library file is created by defining individual methods for all these actions as shown below:

Here, we are creating a user-defined method for the action – ‘Enter URL’.

The name provided for the user-defined method is called a Keyword.

So, here ‘enter_URL’ is the Keyword

public void enter_URL(WebDriver driver,String TestData) throws IOException
{
driver.get(TestData);
}

Parameters:

driver – The driver is initialized in the Main Class and is passed in here.
TestData – is read from the external source by the Main Class and passed it in here.

Function:

Here, driver.get() – is the function of Selenium that performs the action ‘enter URL’.

Keywords.java

package Keywords.Defined;

import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class Keywords {
 
String path = System.getProperty("user.dir");
WebDriver driver; // driver object is declared
 
//method for entering URL – Keyword is ‘enter_URL’ 
public void enter_URL(WebDriver driver,String TestData) throws IOException
{
driver.get(TestData);
}
//method for typing action – Keyword is ‘type’ 
public void type(WebDriver driver, String ObjectName, String locatorType, String testdata) 
{
driver.findElement(this.getObject(ObjectName,locatorType)).sendKeys(testdata);
}
 
//method for click action – Keyword is ‘click’ 
public void click(WebDriver driver,String ObjectName, String locatorType) 
{
driver.findElement(this.getObject(ObjectName,locatorType)).click();
}

}

#2) Excel Sheet To Store Keywords

All the user-defined methods along with its functionality details should be mentioned in the excel sheet so that the user can understand what Keyword the library file holds.

Excel sheet acts as a summary for the library file and becomes useful while creating the test case template, where the user looks at the excel sheet keyword list and picks the corresponding Keyword for each action in the test case.

For Example:

Excel Sheet to store Keywords


package Keywords.Defined;

import java.io.File;
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.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Keywords {

String path = System.getProperty("user.dir");

WebDriver driver;

public void enter_URL(WebDriver driver,String TestData) throws IOException{
driver.get(TestData);
}

public void type(WebDriver driver, String ObjectName, String locatorType, String testdata) throws IOException{

driver.findElement(this.getObject(ObjectName,locatorType)).sendKeys(testdata);
//driver.findElement(By.xpath("//")).sendKeys(testdata);

}
public void wait(WebDriver driver,String ObjectName, String locatorType) throws IOException{
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(this.getObject(ObjectName,locatorType))));
}
public void click(WebDriver driver,String ObjectName, String locatorType) throws IOException{
driver.findElement(this.getObject(ObjectName,locatorType)).click();
}
public String get_currentURL(WebDriver driver){
String URL = driver.getCurrentUrl();
System.out.println("print URL "+URL);
return URL;
}
By getObject(String ObjectName, String locatorType) throws IOException{

File file = new File(path+"\\Externals\\Object Repository.properties");
FileInputStream fileInput = new FileInputStream(file);

Properties prop = new Properties();
//find by xpath
if(locatorType.equalsIgnoreCase("XPATH")){

return By.xpath(prop.getProperty(ObjectName));
}
//find by class
else if(locatorType.equalsIgnoreCase("CLASSNAME")){

return By.className(prop.getProperty(ObjectName));

}
//find by name
else if(locatorType.equalsIgnoreCase("NAME")){

return By.name(prop.getProperty(ObjectName));

}
//Find by css
else if(locatorType.equalsIgnoreCase("CSS")){

return By.cssSelector(prop.getProperty(ObjectName));

}
//find by link
else if(locatorType.equalsIgnoreCase("LINK")){

return By.linkText(prop.getProperty(ObjectName));

}
//find by partial link
else if(locatorType.equalsIgnoreCase("PARTIALLINK")){

return By.partialLinkText(prop.getProperty(ObjectName));

}
return null;

}

}

#3) Design Test Case Template

Test Case Template can be created according to project convenience. There is no particular rule to create a template. It is designed depending on how much the project needs the framework to externalize. The externalization might be only for Keywords, or sometimes even Test Data and UI Elements are also externalized.

A sample test case template is created:

In the given example, the template is created in such a way that:

  • Each sheet corresponds to a Test Case and the last sheet is the one that holds the ‘Keywords list’.
  • Each row corresponds to the test steps of a TC.
  • Each Column is the parameters that are necessary for each action.

Design Test Case Template

How to Fill in the Test Case Template:

#1) From the provided test case sheet, read each test case and its corresponding test steps. For each test step, locate the action and find the corresponding keyword from the library file.

#2) Once the action is matched with the keyword, fill in the test case template in the test case order and also pass the other required parameters as necessary.

Test Case Template

#4) Object Repository For Locators

UI locator can be identified and its value can be either mentioned in the test case template or maintained in a separate Object Repository.

In the below example, the element identification properties

  • Locator Type – The identification technique used is id, Xpath, ClassName, etc.
  • Locator Value – Value of that attribute – For Example: If its Id attribute, then the value of id and so on.

Object Repository for Locators

Another way is to maintain in the Object Repository and mention the Object Name in the Excel Sheet.

  • Locator Type – The identification technique used is id, Xpath, ClassName, etc.
  • Locator Name –The name of the Object from the Object repository where the locator value is stored. For Example, If the repository holds the Xpath value of an element with the ObjectName as ‘Username’, then the LocatorName is mentioned as ‘UserName’.

NOTE: According to the LocatorType mentioned in the excel sheet, its value should be stored in the Object Repository. For Example: If Xpath is specified as LocatorType, then the Repository should store Xpath of that element, if the id is specified as Locatortype, then the id should be stored and so on.

Object Repository

#5) Test Scripts Or Driver Script

This is the main script that reads all the contents of the excel sheet and performs the corresponding action. The script is designed based on how the template is created.

In our case, as each test sheet acts as a test case and each row as a test step, the driver script can be created by iterating across sheets and then rows.

In each particular row, a keyword is read and its corresponding method in the library file is executed and so on. This continues until all the test steps in the test case are executed. Then, it proceeds to the next test case/sheet and executes it.

Test Scripts or Driver Script


package Automation.KeywordFramework;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Properties;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Reporter;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;

import Keywords.Defined.Assertions;
import Keywords.Defined.Keywords;

public class IrctcLogic {

WebDriver driver;
String path = System.getProperty("user.dir");

Keywords keyword = new Keywords();
Assertions assertion = new Assertions();

@Test
public void readExcelandexecute() throws IOException, InterruptedException{

//From excelfile
String excelFilePath = path+"\\Externals\\Test Cases.xlsx";
FileInputStream fileInputStream = new FileInputStream(excelFilePath);

XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);

int testcasescount = workbook.getNumberOfSheets()-1;

System.out.println("Total test cases :"+testcasescount);

for (int testcase=0;testcase<testcasescount;testcase++){
System.setProperty("webdriver.chrome.driver", path+"\\Drivers\\chromedriver.exe");
driver = new ChromeDriver();

XSSFSheet worksheet = workbook.getSheetAt(testcase);

System.out.println("worksheet Number "+testcase+":"+worksheet.getSheetName());

int row = worksheet.getLastRowNum();
int column = worksheet.getRow(1).getLastCellNum();

for(int i=1;i<=row;i++){

LinkedList<String> Testexecution = new LinkedList<>();

System.out.println('Row value :"+i+"It has first cell value as : "+worksheet.getRow(i).getCell(0));

for(int j=0;j<column-1;j++){
System.out.println("Column index :"+j);
Cell Criteria = worksheet.getRow(i).getCell(j);

String CriteriaText;
if(Criteria==null){
CriteriaText = null;
}else{
CriteriaText = Criteria.getStringCellValue();
}
Testexecution.add(CriteriaText);
}
System.out.println("List :"+Testexecution);

String TestStep = Testexecution.get(0);
String ObjectName = Testexecution.get(1);
String LocatorType = Testexecution.get(2);
String Testdata = Testexecution.get(3);
String AssertionType = Testexecution.get(4);
String ExpectedValue = Testexecution.get(5);
String ActualValue = Testexecution.get(6);

perform(TestStep,ObjectName,LocatorType,Testdata,AssertionType,ExpectedValue,ActualValue);

System.out.println("Row"+i+" is read and action performed");
}

driver.close();
System.out.println("************************TEST CASE "+worksheet.getSheetName()+" is executed*******************");
}
}

public void perform(String operation, String objectName, String locatorType, String testdata,
String assertionType, String expectedValue, String actualValue) throws IOException, InterruptedException {

switch (operation) {
case "enter_URL":
//Perform click
keyword.enter_URL(driver,testdata);
break;

case "get_currentURL":
//Set text on control
keyword.get_currentURL(driver);
break;

case "type":
keyword.type(driver, objectName, locatorType, testdata);

case "click":
keyword.click(driver, objectName, locatorType);

/* case "wait":
keyword.wait(driver, objectName, locatorType);*/

case "implicitWait":
Thread.sleep(8000);

default:
break;
}

if(operation.contains("AssertURL")){

switch(assertionType){

case "contains":
assertion.AssertURLContains(driver, keyword.get_currentURL(driver), expectedValue);
case "equals":
assertion.AssertURLEquals(driver, keyword.get_currentURL(driver), expectedValue);

}

}

if(operation.contains("AssertElement"){

assertion.AssertElement(driver, assertionType, objectName, locatorType);

}

}

}

Conclusion

Thus, once a framework is created, it is re-usable for multiple test cases/projects. In this way, it reduces the man-hours of creating an automation script for each project.

In the next chapter, we will discuss the creation of the Hybrid driven framework which is a combination of the Keyword-Driven framework and the Data-Driven framework.

=> Read Through The Easy Selenium Training Series.

Was this helpful?

Thanks for your feedback!

Leave a Comment