Keyword Driven Framework In Selenium

Profile picture for user arilio666

Keyword Driven Framework is a popular test automation framework used in Selenium for web application testing. In this framework, keywords represent the test steps mapped to specific functions or methods in the automation framework.

The Keyword Driven Framework in Selenium consists of the following components:

  • Test data: This includes the input data for the test cases, such as login credentials, search keywords, etc.
  • Keywords: Keywords represent the test steps in the test case. Examples of keywords include "click," "type," "verify," etc.
  • Test scripts: Test scripts are the actual code that performs the actions represented by the keywords. Each keyword is mapped to a specific function or method in the test script.
  • Object repository: The object repository collects all the web elements used in the tested application. These elements are identified using unique identifiers such as IDs, names, classes, etc.
  • Test results: The test results are generated at the end of the test execution and provide information about the pass or fail status of the test cases.
  • The Keyword Driven Framework in Selenium helps to make test automation more structured, modular, and maintainable. 
  • Separating the test data, keywords, and scripts makes it easier to manage the test automation process and change individual components without affecting others.

1.) Xlsx

  • Let us create an xlsx file where we name keywords to map the table and get it.
  • This is called a keyword map table, a table in an excel sheet that defines keywords for all automation suites.
  • Once the xlsx file is done, paste it inside a new package.
  • Here is the package representation of how the keyword-driven framework looks.

2.) Utility

  • Let us a constants class inside a new package. All the constant, like URL, file path, excel data, etc., is stored here.
package utility;

public class Constants {
    public static final String URL = "https://www.programsbuzz.com/user/login";
    public static final String filePath = "C:\\Users\\arili\\Desktop\\keywordDoc.xlsx";
    public static final String excelData = "keywordDoc.xlsx";
    public static final String username = "Naruto";
    public static final String password = "Sas2234";
}

3.) Action Keyword Class

  • Let us create an Action_keyword class with the exact keyword names from excel as method names.
package keywordDriven;

import java.util.concurrent.TimeUnit;

import org.openqa. Selenium.By;
import org.openqa. Selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;
import utility.Constants;

public class Action_Keyword {
    public static ChromeDriver driver;

    public void openBrowser() {

        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();

        driver.manage().window().maximize();

    }

    public void navigate() {
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(Constants.URL);
    }

    public void enterEmail() {
        driver.findElement(By.id("edit-name")).sendKeys(Constants.username);
    }

    public void enterPassword() {
        driver.findElement(By.id("edit-pass")).sendKeys(Constants.password);
    }
}


4.) Excel Read

  • Let us create an excel read utility file where it reads all the keywords from the excel data and feeds us the data that is contained parallel to its cell.
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); 
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(); 
while(row.hasNext()) 
{ 
Row r = (Row) row.next(); 
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 a; 
}

public void DemoFile(int i) {

} 
}
  • Here we feed in the file path for excel from the constants class, and we read the cell values using apache POI.
  • Create an object of FileInputStream class and pass the file as a parameter to its constructor.
  • Check the next element's availability using the variable reference row.
  • Move the cursor to the cell by getting a cell number.
  • Return the data to the Arraylist method.

5.) Execute

  • Let us call excel to read class and methods of action keywords into a class called executeTest.
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();
        rs.DemoFile(3);
        Action_Keyword k = new Action_Keyword();
        k.openBrowser();
        k.navigate();
        k.enterEmail();
        k.enterPassword();

        System.out.println("Done");
    }
}
  • After the excel read, it maps the keywords in the excel file, and the method name runs the test.