Guide On The Keyword Driven Testing Framework – Devstringx
Keyword Driven Framework is a functional automation testing framework that divides test cases into four different parts(test steps, objects of test steps, actions on test steps, and data for test objects) for better automation.
These type of categorization can be maintained with the help of an Excel spreadsheet:-
- Test Step: It is a very small description of the Action going to perform on the Test Object.
- Test Object: It is the name of the Web Page object/element, like Username & Password, Login button.
- Action: It is the name of the action, which is going to perform on any Object such as click, input, mouse hover, etc.
- Test Data: Test Data can be any value that is needed by the Object to perform any action, like the Password value for the Password field.
The idea behind the Keyword Driven approach in automation is to separate the coding from the test case & test step for a better understanding of the script.
Components Used In Keyword-Driven Testing Framework
Below is the list of components that are used in the Any Keyword Driven Framework.
- Function Library/Function class
- Excel Sheet to store Keywords
- Design Test Case Templates
- Object Repository for Elements/Locators
- Test Scripts
#1) Function Library
This is a Java Class file where All the Keywords are defined. In this class, all the actions that are going to perform on the application are defined as user-defined methods in the library class file.
For Example
Let’s say our application has to perform the following actions in one or more test cases:
- Enter the URL
- Click on the login button
So Now we need to create user-defined methods for each individual action. the name which we will be providing is known as Keyword.
public void enter_URL(WebDriver driver,String TestDataValue){driver.get(TestData);}public void clickOnLoginBtn(WebDriver driver,WebElement ele){ele.click();}
Keywords.Java
package Keywords.Defined; import java.io.File; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; public class Keywords { String path = System.getProperty("user.dir"); WebDriver driver; public void enter_URL(WebDriver driver, String TestDataValue) { driver.get(TestDataValue); } //method for click public void click(WebDriver driver,String ObjectName, String locatorType) { driver.findElement(this.getObject(ObjectName,locatorType)).click(); } }
Read Also:- Hybrid Driven Framework In Selenium
#2) Excel Sheet (To Store Keywords)
All the user-defined methods along with their functionality details mention in the excel sheet so that the user can understand what Keywords the library file holds.
The Excel sheet acts as a summary file for the library file and becomes useful while creating the test case template, where the user looks at the excel sheet keyword list and can pick the corresponding Keyword for each action in the test case.
For Example:
Object | Description | Keywords |
URL | This will enter the URL | Enter_URL |
Element | Click on element | Click |
#3) Test Case Template
It is designing depends on how much the project needs the framework to externalize. The externalization can only be for Keywords, Test Data, and UI Elements.
How to Create a Test Case Template:
- From the manual test case sheet, read each test case and its corresponding test steps. For each test step, find the action and its corresponding keyword from the library file.
- Once the action is matched with the keyword, fill that in the test case template in the test case order
TestSteps | LocatorType | LocatorValue | TestData |
enter_URL | www.test.com | ||
click | //button[text()=’Login’] |
#4) Object Repository For Locators
Locators/Elements can be identified and their value can be either defined in the test case template or can mention in a separate Object Repository.
- Locator Type– it can be id, Xpath, etc
- Locator Value– The value of that locator
- Locator Name– the name of the Object/Locator from the Object repository file
Test steps | LocatorType | LocatorName | TestData |
enter_URL | www.test.com | ||
Click | XPath | Login |
Read Also:- Guide On Behavior Driven Development Testing
#5) Test Scripts
This is the main Step that reads all the data from the excel sheet and performs the required action.
In each row, a keyword is read and its corresponding method in the library file is executed, and so on.
package Automation.KeywordFramework; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.LinkedList; import java.util.Properties; 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 org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Reporter; import org.testng.annotations.BeforeTest; import org.testng.annotations.Parameters; import org.testng.annotations.Test; import org.testng.asserts.Assertion; import Keywords.Defined.Assertions; import Keywords.Defined.Keywords; public class IrctcLogic { WebDriver driver; String path = System.getProperty("user.dir"); Keywords keyword = new Keywords(); Assertions assertion = new Assertions(); @Test public void readExcelandexecute() throws IOException, InterruptedException{ //From excel file String excelFilePath = path+"\\Externals\\Test Cases.xlsx"; FileInputStream fileInputStream = new FileInputStream(excelFilePath); XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream); int testcasescount = workbook.getNumberOfSheets()-1; System.out.println("Total test cases :"+testcasescount); for (int testcase=0;testcase<testcasescount;testcase++){ System.setProperty("webdriver.chrome.driver", path+"\\Drivers\\chromedriver.exe"); driver = new ChromeDriver(); XSSFSheet worksheet = workbook.getSheetAt(testcase); System.out.println("worksheet Number "+testcase+":"+worksheet.getSheetName()); int row = worksheet.getLastRowNum(); int column = worksheet.getRow(1).getLastCellNum(); for(int i=1;i<=row;i++){ LinkedList<String> Testexecution = new LinkedList<>(); System.out.println('Row value :"+i+"It has first cell value as : "+worksheet.getRow(i).getCell(0)); for(int j=0;j<column-1;j++){ System.out.println("Column index :"+j); Cell Criteria = worksheet.getRow(i).getCell(j); String CriteriaText; if(Criteria==null){ CriteriaText = null; }else{ CriteriaText = Criteria.getStringCellValue(); } Testexecution.add(CriteriaText); } System.out.println("List :"+Testexecution); String TestStep = Testexecution.get(0); String ObjectName = Testexecution.get(1); String LocatorType = Testexecution.get(2); String Testdata = Testexecution.get(3); String AssertionType = Testexecution.get(4); String ExpectedValue = Testexecution.get(5); String ActualValue = Testexecution.get(6); perform(TestStep,ObjectName,LocatorType,Testdata,AssertionType,ExpectedValue,ActualValue); System.out.println("Row"+i+" is read and action performed"); } driver.close(); System.out.println("************************TEST CASE "+worksheet.getSheetName()+" is executed*******************"); } }
Read Also:- Robot Automation Framework Features
public void perform(String operation, String objectName, String locator type, String test data, String assertion type, String expected value, String actual value) throws IOException, InterruptedException { switch (operation) { case "enter_URL": keyword.enter_URL(driver,testdata); break; case "click": keyword.click(driver, objectName, locatorType); default: break; } } }
If you are interested in even more software testing-related articles and information from us here at Devstringx, then we have a lot to choose from for you.