A Keyword Driven Framework for Selenium allows testers to separate test case logic from the code, making it easier to automate and maintain tests.
Overview
Keyword Driven Framework allows testers to write tests using a set of predefined keywords, enabling them to express test cases in a natural language format. This framework enhances reusability, scalability, and readability, making it ideal for large-scale testing projects.
Components of Keyword Driven Framework:
- Keywords: Predefined actions (e.g., click, type, verify) used to represent test steps.
- Test Data: External data sources (e.g., Excel, CSV files) containing inputs for test cases.
- Test Scripts: Automation scripts that use keywords to execute the actual test steps.
- Keyword Repository: A centralized location that stores all the predefined keywords and their associated actions.
- Driver Script: The main script that reads keywords from test data and triggers the execution of corresponding actions in Selenium.
The article explores the concept, benefits, components, and implementation of a Keyword Driven Framework in Selenium, along with its differences from Data Driven Framework.
What is Keyword Driven Framework?
The keyword-driven framework is based on keywords that form the basis of the functionality, take in parameters, and throw the relevant output. When the code has to cover a lot of functionality, it might need repetitive writing for some sections. Thus, keywords are written to abstract the complexity of the code.
- The operations or methods are written separately from the script as keywords.
- These keywords, placed in an external file (Excel sheet), are called using the code. This enables testers to keep each of the functionalities separate.
- The steps of each test case are represented using keywords, and each keyword is associated with relevant parameters.
- Testers can create and modify test cases without dealing with the underlying automation code, making the process more user-friendly.
Read More: Selenium WebElement Commands
What is a Keyword Driven Framework in Selenium?
A Keyword Driven Framework in Selenium is an automation framework where test cases are created using a set of predefined keywords, representing actions like “click,” “enter text,” or “verify.”
These keywords are mapped to corresponding Selenium commands, allowing testers to design tests in a more readable and understandable format. This approach separates the test logic from the code, making test creation and maintenance more efficient.
Here’s a process flow to understand how it works.
- The automation script will read the instructions or test input data from the Excel sheet
- The input data is entered in the application under test
- The test cases are performed and results are returned
- The test output data is stored in the Excel sheet
What is Keyword Driven Testing?
Keyword-Driven Testing, or Keyword-Driven Testing Framework or simply Keyword Testing is a test automation framework that abstracts the technical details of test implementation and allows testers to create and manage tests using a more user-friendly and business-oriented language.
Tests are represented using a sequence of keywords, where each keyword corresponds to a specific action or operation to be performed on the application under test. These keywords are defined in a library or repository, and testers use them to create test cases by combining keywords in a meaningful sequence.
Why perform Keyword Driven Testing?
Keyword driven testing addresses common challenges in test automation by providing a structured approach that separates test design from automation implementation. The following are the benefits:
- It empowers technical and non-technical stakeholders to collaborate effectively, ensures efficient test case maintenance, and enhances the overall quality of the testing process.
- With Keyword-Driven Testing, modifying or updating tests often involves changing the keyword definitions or parameters rather than rewriting entire test scripts.
- Test cases can be developed more quickly using predefined keywords, as they encapsulate complex automation logic.
- High-level keywords make conveying testing progress and results easier for management and other teams.
- Using standardized keywords promotes consistency in test case design and execution.
- Test cases can be easily customized by rearranging, adding, or modifying keywords to create new testing scenarios.
Tools Used for Keyword Driven Testing
- Selenium with TestNG or JUnit: Selenium can be combined with testing frameworks like TestNG or JUnit to create a Keyword-Driven Testing framework. You can define keywords as methods and use annotations or configuration files to map keywords to actual test actions.
- Robot Framework: Robot Framework is a powerful and user-friendly test automation framework that supports Keyword-Driven and Data-Driven Testing. It has built-in keywords for various actions; you can create custom keywords. It has a simple tabular syntax for test case design and supports various test libraries and extensions.
To further enhance your Keyword-Driven Testing process, consider using BrowserStack Automate. It allows seamless cross-browser testing on real devices and browsers, ensuring that your keyword-driven tests work flawlessly across various environments. Automate your Selenium scripts with BrowserStack to achieve faster test execution, greater reliability, and comprehensive test coverage.
Advantages And Disadvantages of Keyword Driven Testing Framework
The following outlines the advantages and disadvantages of using a Keyword Driven Testing Framework, providing insight into its strengths and potential limitations.
| Advantages | Disadvantages |
| Code Reusability | Keyword Management |
| Platform independent Test Scripts | Extensive 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 challenges while using other framework approaches using Keyword Driven Framework in Selenium.
Components of a Keyword Driven Framework
The components of a Keyword Driven Framework include essential elements that work together to enable efficient and structured test automation.
- Excel Sheet – It is used to store the data for test cases, such as keywords
- Object Repository – This stores the locator values for web elements
- Function Library – It is used to create functions that perform actions
- Test Data Sheet – This is an Excel file storing the data values within objects
- Test Scripts – These are the scripts to interact with the application under test
- Driver Script – This is the driver engine that interacts with all test scripts
- Selenium – It is used for environment setup. Other tools can also be used for this purpose
Keyword Driven Framework Using Selenium WebDriver
Here is a simple design of a keyword-driven framework using a scenario that automates the end-to-end flow of a web application.
Frameworks in Selenium help separate the code and the data to help maintain test cases. Frameworks help make the code reusable, readable, and reduce the cost of maintenance. This section will explore a Keyword Driven Testing Framework using Selenium Webdriver.
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.
Scenarios to be automated:
- Open a Browser
- Navigate to URL https://bstackdemo.com/
- Click the Sign In button
- Enter Username
- Enter password
- Click on the Login button
- Validate username on homePage
- Click on the Logout button
- Close the browser
Prerequisites:
- First configure Eclipse with Selenium
- Create a Maven project and add dependencies in the pom.xml file below, which contains dependencies related to the Selenium project and excelSheethandling.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.keyword_driven_framework</groupId> <artifactId>keyword_driven_framework</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- Other dependencies, if any --> <!-- Selenium WebDriver --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.0.0</version> <!-- Replace with the latest version available --> </dependency> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.3.1</version> <!-- Use the latest version available --> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.11</version> </dependency> </dependencies> </project>
Step 1: To design a Keyword Driven Framework, first identify all the actions to be performed for automated testing of an application. In the above scenario, seven steps have to be completed.
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 scenario
Step 3: Once all keywords have been identified, the next step is to place the Excel sheet in the project package. Create a package named ‘excelData’ by right-clicking on the Project. For this, go to New > Package.
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.
This structure is highly recommended as it is easy to understand, use, and maintain.
Read More: How to Launch Browser in Selenium
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 com.keyword_driven_framework.utility;
public class Constants {
public static final String URL = "https://bstackdemo.com/";
//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 identified in Excel.
package com.keyword_driven_framework.keywordDriven;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import com.keyword_driven_framework.utility.Constants;
public class Action_keyword
{
public static WebDriver driver;
public void openBrowser()
{
System.setProperty("webdriver.gecko.driver","path_to_driver");
FirefoxOptions options = new FirefoxOptions();
WebDriver driver = new FirefoxDriver(options);
driver.manage().window().maximize();
}
public void navigate()
{
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get(Constants.URL);
}
public void clickSignIn_h()
{
driver.findElement(By.xpath("//a[@id='signin']")).click();
}
public void enterEmail()
{
driver.findElement(By.xpath("(//div[@class=' css-1wa3eu0-placeholder'])[1]")).sendKeys(Constants.username);
}
public void enterPassword()
{
driver.findElement(By.xpath("(//div[@class=' css-1wa3eu0-placeholder'])[2]")).sendKeys(Constants.password);
}
public void clickSignIn()
{
driver.findElement(By.xpath("//button[@id='login-btn']")).click();
}
public void verifyUserName()
{
String username =new String ("your_username");
username.equals(driver.findElement(By.xpath("//span[@class='username']")));
}
public void logout()
{
driver.findElement(By.xpath("//a[@id='logout']")).click();
}
public void verifySignBtn()
{
String signIn =new String ("Sign In");
signIn.equals(driver.findElement(By.xpath("//a[@id='signin']")));
}
public void closeBrowser()
{
driver.quit();
}
}Step 6: Write code to read data from an Excel sheet in this step. For this, use an Apache POI library, which allows testers 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.
package com.keyword_driven_framework.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 com.keyword_driven_framework.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();
row.next();
ArrayList<String> a = new ArrayList();
// Checking the next element availability using the reference variable row.
while(row.hasNext())
{
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(data);
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 com.keyword_driven_framework.executionEngine;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import com.keyword_driven_framework.excelUtility.ReadExcelSheet;
import com.keyword_driven_framework.keywordDriven.Action_keyword;
public class ExecutionTest
{
public static void main(String[] args) throws IOException, Exception, IllegalArgumentException, InvocationTargetException
{
ReadExcelSheet rs = new ReadExcelSheet();
rs.DemoFile(4);
Action_keyword k = new Action_keyword();
k.openBrowser();
k.navigate();
k.clickSignIn_h();
k.enterEmail();
k.enterPassword();
k.clickSignIn();
k.verifyUserName();
k.logout();
k.verifySignBtn();
k.closeBrowser();
System.out.println("Test executed successfully");
}
}Observe the output by executing the code:
A more advanced keyword-driven framework would involve data-driven capabilities, better error handling, and more modular test scenarios and keyword organization.
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 stage | One has to wait for development to finish before writing test cases |
Overall, we can conclude that Keyword-Driven Testing aims to improve the efficiency and effectiveness of software testing by providing a structured, reusable, and maintainable approach to test automation.
Conclusion
A Keyword Driven Framework for Selenium offers a structured and efficient approach to test automation by separating test logic from code. It enhances readability, reusability, and scalability, making it suitable for both technical and non-technical teams.
While it comes with its own set of challenges, such as maintaining a large set of keywords, its benefits in improving collaboration and simplifying test creation make it a powerful tool for automating web application testing.







