• Selenium Video Tutorials

Selenium - Keyword Driven Framework



Selenium Webdriver can be used to develop test scripts which are based on the keyword driven framework. A keyword driven framework is mostly used to create functional test cases where there is a clear demarcation between the design of the test cases and development.

A keyword driven framework comprises a group of keywords and actions that performs a set of operations. These keywords can be reused multiple times in the same test case. The keywords are given names such that they are self-explanatory, and describe what the actions they are intended to perform on the application under test. Thus a keyword driven can be adopted very easily to automate test cases.

Keyword Driven Testing Framework

A keyword testing or a keyword driven framework is a set of guidelines followed while automating test cases, in which the implementation logic and the technical information are hidden. The usage and maintenance of a keyword driven framework is very easy.

A group of keywords in a particular and purposeful order has to be incorporated as a part of test case preparation to perform a particular task or tasks on the application. All these keywords are described in the common repository or a reusable library.

Need of Keyword Driven Testing Framework

In the keyword driven testing framework, there is a clear partition between the test case and the coding logic of test automation. It has the below usages −

  • A keyword driven framework can be used effectively by anyone without requiring technical expertise. Every member of the team can participate in the testing which helps to ensure a quality product and development of test cases at a faster rate.

  • In case an update is required, the changes are made at the implementation logic and there is no need to rewrite or modify the test cases.

  • In the keyword driven framework, meaningful and consistent keywords are used in the entire test case, thus ensuring uniformity in the test cases.

  • A new test case can be created in the keyword driven framework easily by simply updating, incorporating or changing the order of the keywords.

Tools to Create Keyword Driven Testing Framework

The below tools can be used to create a keyword driven framework −

  • Robot Framework

  • Selenium

  • Playwright

Advantages of Keyword Driven Testing Framework

The below are the advantages of keyword driven testing framework −

  • No technical knowledge required to create, execute, and maintain the test cases built on a keyword driven testing framework.

  • The test cases can be reused to a large extent in a keyword driven testing framework.

  • There is clear separation among the test cases, data, and the implementation logic and functions, so any change required in any one of the sections does not impact others.

  • The test cases developed on the keyword driven testing framework does not have any implementation logic available, hence it gives readability to the test cases.

Disadvantages of Keyword Driven Testing Framework

The below are the disadvantages of keyword driven testing framework −

  • Implementation of the keyword driven testing framework and its logic require high technical skills.

  • The unnecessary keywords created in the keyword driven testing framework may cause confusion.

  • Extensive planning and preparation are required to be done in the initial stages.

  • Periodic maintenance is required to be done both at the test case level and implementation layers in a keyword driven testing framework.

What Comprises a Keyword Driven Testing Framework?

A keyword driven framework comprises of the below items −

  • Excel Sheet which contains the keywords.

  • Object Repository which contains the element locators.

  • Keyword Library which contains the implementation logic of the keywords.

  • Libraries which contain the common functions in the framework.

  • Test Data files which contain the data required for the test cases.

  • Driver Engine which controls and communicates with the test cases.

  • Any tool, for example Selenium which supports creation of a keyword driven framework.

  • Test Cases which are designed to test the application.

Example

Let us take an example of the below page where we would retrieve and verify the message - You have checked Yes obtained after clicking the radio button beside Yes using a keyword driven testing framework.

Selenium Keyword Driven Framework 1

Step 1 − First of all, we would prepare an excel called TestCase.xlsx as shown in the below image with all the keywords like launchBrowser, open, click, verifyTest, and quit in the form of a test case for the example. We had placed this file under the excel package in our project.

Selenium Keyword Driven Framework 2

Step 2 − We would store the path of the TestCase.xlsx and other environment values like the application URL within the class file StaticDatas.java under the Utils package.

Code Implementation in StaticDatas.java

package Utils;

public class StaticDatas {
   public static final String exeData = "TestCase.xlsx";
   public static final String URLToOpen = "https://www.tutorialspoint.com/selenium/practice/radio-button.php";
}

Step 3 − We would create the methods for the keywords identified within the TestCase.xlsx to perform the actions to be performed for that we would create the class file ActionsToPerform.java under the Action package.

Code Implementation in ActionsToPerform.java

package Action;

import Utils.StaticDatas;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import static org.testng.Assert.assertEquals;

public class ActionsToPerform {
   public static WebDriver driver;
   public void launchBrowser(){
   
      // Initiate the Webdriver
      driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
   }
   public void open(){
   
      //launch URL
      driver.get(StaticDatas.URLToOpen);
   }
   public void click(){
   
      // identify element then click
      WebElement radioBtn = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/form/div[1]/input"));
      radioBtn.click();
   }
   public void verifyText(){
   
      // identify element
      WebElement chkMessage = driver.findElement(By.xpath("//*[@id='check']"));

      // get element text
      String text = chkMessage.getText();
      System.out.println("Message is: " + text);

      // verify message
      assertEquals("You have checked Yes", text);
   }
   public void quit(){  
   
      // quitting browser
      driver.quit();
   }
}

Step 4 − We would need to read the TestCase.xlsx file(using the Apache POI Library) to get hold of the keywords identified to perform on the application for that we would create the class file GetDataFromExcel.java under the ExcelUtils package.

Code Implementation in GetDataFromExcel.java

package ExcelUtils;

import Utils.StaticDatas;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

public class GetDataFromExcel {
   public ArrayList getExcel(int c) throws IOException {

      // get excel file path
      String f = StaticDatas.exeData;

      // load the excel file
      File fl = new File(f);
      FileInputStream fileInputStream = new FileInputStream(fl);

      // instance of XSSFWorkbook
      XSSFWorkbook w = new XSSFWorkbook(fileInputStream);

      // create sheet in XSSFWorkbook with name TestCase1
      XSSFSheet s = w.getSheet("TestCase1");

      // iterating through rows in sheet
      Iterator r = s.rowIterator();

      // moving to next row
      r.next();
      ArrayList<String> arr = new ArrayList();

      // iterate till next row data exists
      while(r.hasNext()){
         Row rw = (Row) r.next();

         // move to next cell in same row
         Cell cell = rw.getCell(c);

         // get cell data
         String cellValue = cell.getStringCellValue();

         // store cell data in arraylist
         arr.add(cellValue);

         // store all cell data in subsequent rows in arraylist
         arr.add(((Row) r.next()).getCell(c).getStringCellValue());
      }
      System.out.println("Get all cell data in the list : " + arr);
      return arr;
   }
   public void getFile(int j) {
   }
}

Step 5 − We would need to call the method getExcel from the GetDataFromExcel class file to read the TestCase.xlsx file and methods of the ActionsToPerform class file for that we would create the class file ActualTest.java under the DriveEngine package.

Code Implementation in ActualTest.java

package DriverEngine;

import Action.ActionsToPerform;
import ExcelUtils.GetDataFromExcel;

public class ActualTest {
   public static void main(String[] args) {

      GetDataFromExcel  getDataFromExcel = new GetDataFromExcel();

      // get column number of containing keywords in the excel
      getDataFromExcel.getFile(4);

      // get the keyword actions
      ActionsToPerform actionsToPerform = new ActionsToPerform();

      // execute the test steps starting with browser launch
      actionsToPerform.launchBrowser();

      // open URL
      actionsToPerform.open();

      // click radio button
      actionsToPerform.click();

      // verify message
      actionsToPerform.verifyText();

      // quit browser
      actionsToPerform.quit();
      System.out.println("Keyword driven testing framework executed successfully");
   }
}

Step 6 − Dependencies added to pom.xml.

<?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>org.example</groupId>
   <artifactId>SeleniumJava</artifactId>
   <version>1.0-SNAPSHOT</version>
   
   <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   
   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.11.0</version>
      </dependency>
      
      <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
      <dependency>
         <groupId>org.apache.poi</groupId>
         <artifactId>poi</artifactId>
         <version>5.2.5</version>
      </dependency>
      
      <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
      <dependency>
         <groupId>org.apache.poi</groupId>
         <artifactId>poi-ooxml</artifactId>
         <version>5.2.5</version>
      </dependency>
      
      <!-- https://mvnrepository.com/artifact/org.testng/testng -->
      <dependency>
         <groupId>org.testng</groupId>
         <artifactId>testng</artifactId>
         <version>7.9.0</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
</project>

Output

Message is: You have checked Yes
Keyword driven testing framework executed successfully

Process finished with exit code 0

In the above example, we had implemented a keyword driven testing framework to verify and get the message in the console - You have checked Yes.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Selenium Keyword Driven Framework 3

This concludes our comprehensive take on the tutorial on Selenium Webdriver - Keyword Driven Framework. We’ve started with describing what is a keyword driven framework, why is a keyword driven framework used, which tools are used for a keyword driven framework, what are the advantages and disadvantages of a keyword driven framework, what comprises of a keyword driven framework, and walked through an example of how to implement a keyword driven framework along with Selenium Webdriver.

This equips you with in-depth knowledge of the Keyword Driven Framework in Selenium Webdriver. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements