Guide On The Keyword Driven Testing Framework – Devstringx

Back to Blog

Guide On The Keyword Driven Testing Framework – Devstringx

Keyword Driven Framework is a functional automation testing framework that divides test cases into four different parts(test steps, objects of test steps, actions on test steps, and data for test objects) for better automation.

These type of categorization can be maintained with the help of an Excel spreadsheet:- 

Test Step: It is a very small description of the Action going to perform on the Test Object.

Test Object: It is the name of the Web Page object/element, like Username & Password, Login button.

Action: It is the name of the action, which is going to perform on any Object such as click, input, mouse hover, etc.

Test Data: Test Data can be any value that is needed by the Object to perform any action, like the Password value for the Password field.

The idea behind the Keyword Driven approach in automation is to separate the coding from the test case & test step for a better understanding of the script.

Components Used In Keyword Driven Testing Framework

Below is the list of components that are used in the Any Keyword Driven Framework.

  1. Function Library/ Function class
  2. Excel Sheet to store Keywords
  3. Design Test Case Templates
  4. Object Repository for Elements/Locators
  5. Test Scripts

#1) Function Library

This is a Java Class file where All the Keywords are defined. In this class, all the actions that are going to perform on the application are defined as user-defined methods in the library class file.

For Example:

Let’s say our application has to perform the following actions in one or more test cases:

  1. Enter the URL
  2. Click on the login button

So Now we need to create user defined methods for each individual action.the name which we will be providing is known as KEYWORD.

public void enter_URL(WebDriver driver,String TestDataValue){driver.get(TestData);}public void clickOnLoginBtn(WebDriver driver,WebElement ele){ele.click();}

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;

 

public void enter_URL(WebDriver driver, String TestDataValue)

{

driver.get(TestDataValue);

}

 

//method for click

public void click(WebDriver driver,String ObjectName, String locatorType)

{

driver.findElement(this.getObject(ObjectName,locatorType)).click();

}

 

}

Read Also:- Hybrid Driven Framework In Selenium

#2) Excel Sheet (To Store Keywords)

All the user-defined methods along with their functionality details are mentioned in the excel sheet so that the user can understand what Keywords the library file holds.

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

For Example:

ObjectDescriptionKeywords
URLThis will enters URLEnter_URL
ElementClick on elementClick

#3) Test Case Template

It is designing depends on how much the project needs the framework to externalize. The externalization can only be for Keywords, or Test Data and UI Elements.

How to  create a Test Case Template:

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

#2) Once the action is matched with the keyword, fill that in the test case template in the test case order

 

TestStepsLocatorTypeLocatorValueTestData
enter_URL  www.test.com
click //button[text()=’Login’] 

#4) Object Repository For Locators

Locators/Elements can be identified and their value can be either defined in the test case template or can be mentioned in a separate Object Repository.

  1. Locator Type– it can be id, Xpath, etc
  2. Locator Value– Value of that locator
  3. Locator Name– the name of the Object/Locator from the Object repository file
Test stepsLocatorTypeLocatorNameTestData
enter_URL  www.test.com
clickXPathLogin 

Read Also:- Guide On Behavior Driven Development Testing

#5) Test Scripts

This is the main Step that reads all the data from the excel sheet and performs the required action.

In each row, a keyword is read and its corresponding method in the library file is executed, and so on.

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 excel file

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*******************”);

}

}

Read Also:- Robot Automation Framework Features

public void perform(String operation, String objectName, String locator type, String test data,

String assertion type, String expected value, String actual value) throws IOException, InterruptedException {

 

switch (operation) {

case “enter_URL”:

keyword.enter_URL(driver,testdata);

break;

 

case “click”:

keyword.click(driver, objectName, locatorType);

 

default:

break;

}

}

 

}

 

Share this post

Back to Blog

Hire A Resource