Home Guide Keyword Driven Testing Framework in Selenium

Keyword Driven Testing Framework in Selenium

Neha Vaidya, Community Contributor -

Frameworks in Selenium help separate the code and the data to help maintain test cases. Frameworks help make the code reusable, readable and reduces cost of maintenance. This article will explore a Keyword Driven Testing Framework using Selenium Webdriver.

What is a Keyword Driven Framework in Selenium?

A Keyword Driven Framework has all operations and instructions written in an external Excel file. Let’s take a look at the process flow to understand how it works.

Keyword Driven Testing Framework in Selenium

  1. The automation script will read the instructions or test input data from the Excel sheet
  2. The input data is entered in the application under test
  3. The test cases are performed and results are returned
  4. The test output data is stored in the Excel sheet

Even though the process is simple, a structured approach is followed in order to perform Keyword Driven Testing in Selenium. But before exploring that, let’s talk about how the Keyword Driven approach is different from the Data Driven approach.

Difference Between Data Driven And Keyword Driven Framework

Keyword Driven Framework Data Driven Framework 
Easy to maintain due to more abstraction layers between test scripts, test data, keywords, etc.Comparatively harder to maintain since the abstraction is only between test data and test scripts
Planning has to be extensive and precise for keyword driven frameworks Planning is easy since it is only needed for test data and test scripts
It is possible to write test scripts even before the product has finished development stageOne has to wait for development to finish before writing test cases

Components of a Keyword Driven Framework

  1. Excel Sheet – It is used to store the data for test cases such as keywords
  2. Object Repository – This stores the locator values for web elements
  3. Function Library – It is used to create functions that perform actions
  4. Test Data Sheet – This is an Excel file storing the data values within objects
  5. Test Scripts – These are the scripts to interact with the application under test
  6. Driver Script – This is the driver engine that interacts with all test scripts
  7. Selenium – It is used for environment set up. Other tools can also be used for this purpose

Keyword Driven Framework Using Selenium WebDriver : Example

Let’s create a simple design of a Keyword Driven Framework with the help of a scenario. The scenario will automate the end to end flow of a web application.

Scenario to be automated:

1. Open a Browser
2. Navigate to URL https://www.browserstack.com/
3. Enter username
4. Enter password
5. Click on the Login button
6. Click on the Logout button
7. Close the browser

Step1: To design a Keyword Driven Framework, first identify all the actions to be performed for automated testing of an application. In the above scenario, there are seven actions that have to be performed.

Step 2: Once all actions are identified, the next step is to create a keyword map table in the Excel sheet. Keyword map table is a table in an Excel sheet that defines all the keywords available for test automation projects.

The table below shows the keyword map table for the above scenarioKeyword Driven Testing Framework

Step 3: Once all keywords have been identified, the next step is to place the Excel sheet in a package of the project. Create a package named ‘excelData’ by right clicking on the Project. For this, go to New > Package.

Keyword Driven Framework in Selenium

Place the newly created Excel file in the package directory locally on the computer. The package structure of the keyword-driven framework can be seen in the screenshot below.

Keyword driven framework in selenium webdriver code with example
Using this structure is highly recommended as it is easy to understand, use, and maintain.

Step 4: Create a new package named “utility”. Now, create a new class file named “Constants” in which the tester will define constants like URL, filePath, and excelData. Source code looks as below:

package utility;
public class Constants {
public static final String URL = "https://www.browserstack.com/users/sign_in/"; 
//Mention the path to your excel file
public static final String filePath = "D:\\DemoFile.xlsx"; 
public static final String excelData = "DemoFile.xlsx"; 
public static final String username = "Your_Username"; 
public static final String password = "Your_password"; 

Step 5: Now write code for each action keyword. Create a new package named “keywordDriven”. Create a new class file named “Action_Keyword”. In this class, create methods for each Action Keyword that has been identified in Excel.

package keywordDriven;
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import utility.Constants; 
public class Action_keyword 
public static WebDriver driver; 
public void openBrowser() 

WebDriver driver = new ChromeDriver(); 
public void navigate() 
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
public void enterEmail() 
public void enterPassword() 
public void clickSignIn() 

public void logout() 
public void closeBrowser() 

Step 6: In this step, write code to read data from an Excel sheet. For this, use an Apache POI library which allows testers to read, create and edit Microsoft Office documents using Java.

Run Selenium Tests on Real Devices for Free

Create a new package named “excelUtility” and then create a new class file named “ReadExcelSheet”. Look at the following source code to read all keywords from the step table.

package excelUtility;
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Iterator; 
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 utility.Constants;

public class ReadExcelSheet 
public ArrayList readExcelData(int colNo) throws IOException 
String filePath = Constants.filePath; 
File file = new File(filePath); 
// Create an object of FileInputStream class and pass file as parameter to its constructor. 
FileInputStream fis = new FileInputStream(file); 
XSSFWorkbook wb = new XSSFWorkbook(fis); 
XSSFSheet sheet = wb.getSheet("Sheet1"); 

Iterator row = sheet.rowIterator(); 
ArrayList<String> a = new ArrayList(); 
// Checking the next element availability using the reference variable row. 
Row r = (Row) row.next(); 
// Moving cursor to the cell by getting a cell number. 
Cell c = r.getCell(colNo); 
String data = c.getStringCellValue(); 
a.add(((Row) row.next()).getCell(colNo).getStringCellValue()); 
System.out.println("List: " +a);
// Return the data to the Arraylist method. 
return a; 

public void DemoFile(int i) {
// TODO Auto-generated method stub


Step 7: Next step is to write code for calling readExcelData() method of ReadExcelSheet class and methods of ActionKeywords class. Create a new package named “executionEngine” and write the following source code by creating a class named “ExecutionTest”.

package executionEngine;

import java.io.IOException; 
import java.lang.reflect.InvocationTargetException; 
import excelUtility.ReadExcelSheet; 
import keywordDriven.Action_keyword;

public class ExecutionTest 
public static void main(String[] args) throws IOException, Exception, IllegalArgumentException, InvocationTargetException 
ReadExcelSheet rs = new ReadExcelSheet(); 
Action_keyword k = new Action_keyword(); 
System.out.println("Test executed successfully"); 

Observe the output by executing the code:

Keyword driven testing

Advantages And Disadvantages of Keyword Driven Testing Framework

Advantages Disadvantages
Code Reusability Keyword Management
Platform independent Test ScriptsExtensive Planning 
Enhanced Productivity Expertise required to write test scripts
Application abstraction Time taken to write test scripts is higher than usual 

The benefits are evident enough to prove the mettle of Keyword Driven Framework in Selenium. Testers can overcome many of the challenges faced while using other framework approaches by using Keyword Driven Framework in Selenium.

Automation Testing Selenium Selenium Webdriver

Featured Articles

Data Driven Framework in Selenium

How to Read and Write an Excel File using Selenium WebDriver

Testing Tactics for Faster Release Cycles

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free