Keyword Driven Framework in Selenium WebDriver Example

What is Keyword Driven Framework in Selenium


Keyword-driven automation framework is the most popular testing framework that is easy to design in selenium. It is a technique in which we represent the test scenario in terms of keywords and corresponding parameters. 

A keyword driven framework in Selenium is a collection of keywords that are used to write test scripts step-wise in the form of table and functions are called based on keywords to complete an end-to-end flow.

Each keyword represents a user action or function that we would like to test in the application. We write keywords one after another in the table (generally in excel sheet or database) in a simple English style to automate a test case.

For each keyword, we write a particular code that will be executed when the specified keyword is called to perform a particular action.

Let us take a look at the block diagram of keyword driven framework that uses keyword library to perform a particular action of application.

Keyword driven framework in Selenium WebDriver

In keyword driven automation framework, we mainly focus on actions/functions, not on test data. Therefore, if you want to test multiple functionalities of application under test, you can go for the best option keyword driven framework in Selenium. 

This framework is a type of functional automation testing framework which is also known as table-driven testing or action based testing because a sequence of test steps and their corresponding inputs are implemented by using a set of keywords in tabular format.

In the keyword driven framework, we keep separate all the operations and instructions to be performed from the actual test case. Let’s take an example where we will try to understand the functionalities and related keywords of an application.

Suppose we have a banking application that provides various services such as 
1. Account balance check
2. Transfer Money
3. Bill Pay
4. Mobile & DTH Recharge
5. Payments for due bills and so on.

These features are considered as basic functionalities of a banking application that can be understood as keywords. To automate this kind of application, we can implement keyword driven automation framework. We will write keywords for each functionality of the application like Login, Account_Balance_Check, Transfer_Money, Bill_Pay, Mobile_DTH_Recharge, Pay_Due_Bills, and Logout.


For each keyword, we will create a function/method in java for each functionality of the application and then pass test data and test object details to these functions to perform operations.

Main Components of Keyword Driven Framework in Selenium


There are some common components for each keyword driven automation framework that you need in order to create a Selenium keyword driven framework. They are as follows:
1. Excel Sheet: It is used to store keywords and data required for test cases, test steps, test objects, and test actions

2. Object Repository: These are property files that are used to store locator values of web elements present in the application. A property file is a text file where data is stored in the form of key-value pairs. These property files act as an object repository in Selenium WebDriver.

3. Function Library: Functions library plays an important role that is used to perform actions. These functions that we create, should be tied up with keywords.

4. Test Data Sheet (Generally in Excel format): This is an excel file that is used to store data values within objects to perform actions on it.

5. Test Scripts: Test scripts perform operations on AUT (Application under test) with the help of Library functions and Test Data.

6. Driver Script: Driver script is heart of the keyword driven framework in selenium. This script is also known as execution engine. This is the main script that interacts with all test scripts. The driver script does so with the help of Library functions.

The main task that is accomplished by driver script, is to read data from environment variables, read excel file, call functions mapped to keywords, and log the result.
7. Selenium – Setting and Environment variables.

Test Data Sheet in Keyword Driven Framework in Selenium


Generally, automated test cases are stored in an excel spreadsheet. In Selenium keyword driven framework, we read excel sheet and then row by row we execute functions in the test case. Each test case is implemented as a set of keywords that represents the functionalities of an application.

List of common columns in Data Sheet are as follows:

1. Test case ID: ID of test case. A test case is basically a set of cases to test, steps, expected results and actual results to ensure quality of an application.
2. Test case Name: Name of test cases/scenario.

3. Execute Flag: If it is marked Y then test case will be executed.
4. Test step ID: Steps in a test case which is going to perform on the test objects.
5 Action: Name of the action that has to be performed on any Object such as open browser, enter username, enter password, click, etc.

6. Keyword: A set of user actions or functions that we would like to test in the application.
7. Object Name: Name of WebElement like username, password present on a web page.
8. Object Type: Name of locators like XPath, CSS id, name, etc.
9. Object Value: Actual test data that has to be entered in objects.
10. Parameter: It is used to control the execution flow in the function.


Let’s take a look at a sample data sheet that can be used in keyword driven framework. You can also customize your data sheet for each project as per your requirements and design.

Test Data Sheet in Keyword Driven Framework in Selenium
In the above sample data sheet, we have created one test case which contains various keywords that represent functionality of an application in step by step.

Why do we perform Keyword-driven testing?


There are the following reasons to perform keyword-driven testing. 
1. Keyword-driven framework helps to separate test scrip and test data individually which minimizes script modification and maintenance efforts.

2. By separating it, a non-technical person or manual tester can also easily understand automation process and write automation script to automate functionality of the application.

3. Test execution can be performed automatically by non-technical person or manual tester based on the keyword lists.

4. Using this approach, we can make all the common components files individually which help to handle easily and increase the reusability of common components.

5. In keyword driven testing approach, the bug report and its graphical representation are generated automatically which reduces the effort and time of testers.
So, these are some common reasons for performing keyword driven testing framework.

Steps to Set up Keyword Driven Framework in Selenium


Let’s create a simple design of keyword driven framework with the help of a scenario. In this scenario, we will automate an end to end flow of a web application.

Scenario to Automate:
1. Open a Browser
2. Navigate to URL “https://www.linkedin.com/login”
3. Enter username
4. Enter password
5. Click on the login button
6. Click on the profile image
7. Click on the Logout button
8. Close the browser

Now, follow all the below steps to design a simple keyword driven framework:
Step1To design a keyword-driven framework, the first step is to identify all the actions that have to be performed for test automation of an application. In the above scenario, there are eight 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 project. Table shows keyword map table for the above scenario.

Excel data sheet in keyword driven framework


As you can observe that action keywords have defined one after another in a table to automate a test case. The table so formed is called step table. Now, save this excel sheet as “ExcelSheet” on your computer.

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

Place newly created excel file in the package directory locally on your computer. Now, refresh your project folder in Eclipse. The package structure of the keyword-driven framework can be seen in the below screenshot.

Project folder structure of keyword driven framework in Selenium


It is always recommended to use this structure, 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 we will define constants like URL, filePath, and excelData. Look at the following source code.
Constants class code:

    package Utility; public class Constants { public static final String URL = “https://www.linkedin.com/login”; public static final String filePath = “C:\selenium\Keyword Framework\src\excelData\ExcelSheet.xlsx”; public static final String excelData = “ReadExcelSheet.xlsx”; public static final String username = “xyz@gmail.com”; public static final String password = “dxyz986”; }

Step 5: Next step is to write a code for each action keyword. Create a new package named “keywordDriven”. Create a new class file named “ActionKeywords”. In this class, create methods for each Action Keyword that we had identified in the Excel. See the source code.
ActionKeywords class code:

    package keywordDriven; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import Utility.Constants; public class ActionKeywords { public static WebDriver driver; public void openBrowser() { driver = new FirefoxDriver(); driver.manage().window().maximize(); } public void navigate() { driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get(Constants.URL); } public void enterEmail() { driver.findElement(By.xpath(“//input[@id = ‘username’]”)).sendKeys(Constants.username); } public void enterPassword() { driver.findElement(By.xpath(“//input[@id=’password’]”)).sendKeys(Constants.password); } public void clickSignIn() { driver.findElement(By.xpath(“//button[text() = ‘Sign in’]”)).click(); } public void profileImage() { driver.findElement(By.xpath(“//img[@id = ’ember26′]”)).click(); } public void logout() { driver.findElement(By.xpath(“//a[text() = ‘Sign out’]”)).click(); } public void closeBrowser() { driver.quit(); } }

Step 6: In this step, we have to write code to read data from excel sheet. For this, we use Apache POI library which allows us to read, create and edit Microsoft Office-documents using Java.

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.
ReadExcelSheet class code:

    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 { // Call filePath variable using class name. String filePath = Constants.filePath; // Create an object of file class and pass filePath as an input parameter to its constructor. File file = new File(filePath); // Create an object of FileInputStream class and pass file as parameter to its constructor. FileInputStream fis = new FileInputStream(file); // Create an object of XSSFWorkbook class and pass fis as parameter to its. XSSFWorkbook wb = new XSSFWorkbook(fis); XSSFSheet sheet = wb.getSheet(“Sheet1”); Iterator row = sheet.rowIterator(); row.next(); // Create an ArrayList object of String type. It will accept String value only. ArrayList ar = new ArrayList(); // Checking the next element availability using reference variable row. while(row.hasNext()) { // Moving cursor to next Row using reference variable row. Row r = row.next(); // Moving cursor to the cell by getting cell number. Cell c = r.getCell(colNo); // Read the value of the cell using getStringCellValue() method. String data = c.getStringCellValue(); // Adding the value of the cells in the Arraylist by passing ‘data’. ar.add(data); // OR One line code: ar.add(row.next().getCell(colNo).getStringCellValue()); } System.out.println(“List: ” +ar); // Return the data to the Arraylist method. return ar; } }

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

    package executionEngine; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import ExcelUtility.ReadExcelSheet; import keywordDriven.ActionKeywords; public class ExecutionTest { public static void main(String[] args) throws IOException, Exception, IllegalArgumentException, InvocationTargetException { ReadExcelSheet rs = new ReadExcelSheet(); rs.readExcelData(4); ActionKeywords ak = new ActionKeywords(); ak.openBrowser(); ak.navigate(); ak.enterEmail(); ak.enterPassword(); ak.clickSignIn(); ak.profileImage(); ak.logout(); ak.closeBrowser(); System.out.println(“Test executed successfully”); } }

Now run ExecuteTest class and observe the test automation. You will get the following output.

    Output: List: [openBrowser, navigate, enterEmail, enterPassword, clickSignIn, profileImage, logout, closeBrowser] Test executed successfully

Here, we have prepared a simple set up of Keyword Driven Framework to give the basic idea behind Keyword Driven framework. But this Framework is just a draft version. There are plenty of things that can be done to make it more robust and effective. In the further tutorial, we will learn how to design keyword driven framework at an advanced level.

Advantage of Keyword Driven Framework


Some of advantages of keyword driven framework are as follows:
1. We don’t need expertise in automation to maintain or create new test scripts. Once the keyword functionalities of application are ready, minimal specific scripting skills are needed for automating test cases.

2. Keywords can be reused across multiple test scripts or even different software.
3. Easy maintenance of test scripts.
4. Keyword-driven framework is not dependent on a specific programming language or tool
5. It is compatible with any automation tools available in the market.

Disadvantages of Keyword Driven Framework


Some of disadvantages of keyword driven framework are as follows:
1. To create keyword driven automation framework, we need a higher knowledge of scripting knowledge.
2. Test scripts in keyword driven framework are longer and more complex, taking more time to create and maintain.

Final words
Hope that this tutorial has covered important points to develop keyword driven framework in Selenium WebDriver code with example. I hope that you will have understood how to design this framework and enjoyed it.

Leave a Comment