In this tutorial we are going to discuss about the implementation of Keyword Driven Framework. In Keyword Driven framework, you can create various keywords and associate function with each of these keywords. Then you create a Function Library that contains the logic to read the keywords and call the associated action.
Advantages:
The keyword and function libraries are completely generic and thus can be reused easily for different applications
All the complexity is added in the function libraries. Once the libraries are ready, it becomes very easy to write the actual test script steps in excel sheets.
Disadvantages:
Lot of time and effort needs to be spent initially to create the function libraries.
The benefits of the keyword driven framework are realized only after it has been used for longer periods of time.
Lets get into the implementation of the framework.
This framework is mainly implemented using the Reflection API where the method name is invoked based on the keyword passed in the XLSX file.
Pre-Requisites
Dependencies:
- Selenium WebDriver
- TestNG
- Apache POI
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<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>SeleniumTutorial</groupId> <artifactId>KeywordDrivenFwk</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>KeywordDrivenFwk</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.14.3</version> </dependency> <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager --> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>3.8.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.11</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.11</version> </dependency> </dependencies> </project> |
Logic :DriverScript.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package keyworddrivenfwk.tests; 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.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Parameters; import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; import keyworddrivenfwk.utils.ActionClass; public class DriverScript { private WebDriver driver; @Parameters({"browserType"}) @BeforeClass public void invokeBrowser(String browserType) { if(browserType.equals("FF")) { WebDriverManager.firefoxdriver().setup(); driver = new FirefoxDriver(); } else if(browserType.equals("IE")) { WebDriverManager.iedriver().setup(); driver = new InternetExplorerDriver(); } else { WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); } } @Test public void validateLogin() throws Exception { /* * param1 : driver * param2 : xlsx filename * param3 : sheet name */ ActionClass actionClass = new ActionClass(); boolean result = actionClass.run(driver,"inputData.xlsx","E2E_001"); Assert.assertTrue(result); } @AfterClass public void closeBrowser() { driver.quit(); } } |
Logic :ActionClass.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
package keyworddrivenfwk.utils; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; public class ActionClass { static WebDriver driver; static int row; public static void invokeMethod(String className, String methodName, Object... inputArgs) { Class<?> params[] = new Class[inputArgs.length]; for (int i = 0; i < inputArgs.length; i++) { if (inputArgs[i] instanceof String) { params[i] = String.class; } } try { Class<?> actionClass = Class.forName(className); Object _instance = actionClass.newInstance(); Method myMethod = actionClass.getDeclaredMethod(methodName, params); myMethod.invoke(_instance, inputArgs); } catch (Exception e) { e.printStackTrace(); } } public boolean run(WebDriver driver,String fileName,String sheetName) throws Exception{ boolean result = false; this.driver = driver; ExcelUtils.setExcelFile(fileName, sheetName); int rowCount = ExcelUtils.getNoOfRows(); System.out.println("Number of rows" +rowCount); for (row=1; row <=rowCount; row++) { List<Object> myParamList = new ArrayList<Object>(); String methodName = ExcelUtils.getCellData(row,2); for (int col = 3; col < 7; col++) { if (!ExcelUtils.getCellData(row, col).isEmpty() & !ExcelUtils.getCellData(row, col).equals("null")) { myParamList.add(ExcelUtils.getCellData(row,col)); } } Object[] paramsArray = new String[myParamList.size()]; paramsArray = myParamList.toArray(paramsArray); invokeMethod("keyworddrivenfwk.utils.ActionClass", methodName, paramsArray); } result = true; return result; } public void launchApplication(String url) { driver.get(url); } public static void dropDownSelect(String elementReference, String element, WebDriver driver,String data) throws Exception { try { WebElement select = findElement(elementReference, element); List<WebElement> dropDownSelection = select.findElements(By.tagName("option")); for (WebElement option : dropDownSelection) { if (data.trim().equals(option.getText().trim())) { option.click(); Thread.sleep(1000L); break; } Thread.sleep(1000L); } } catch (Exception e) { System.out.println(e); } } public void verifyText(String elementReference, String referenceValue,String data) { try{ String actual = findElement(elementReference, referenceValue).getText(); if(actual.contains(data)) { System.out.println("Expected is equal to actual"); ExcelUtils.writeCellData(row,6,"Pass"); } else { ExcelUtils.writeCellData(row,6,"Fail"); } Thread.sleep(1000L); } catch (Exception e) { e.printStackTrace(); } } public static WebElement findElement(String elementReference, String referenceValue,String data) throws Exception { WebElement x = null; try { if (elementReference.equalsIgnoreCase("id")) { x= driver.findElement(By.id(referenceValue)); } else if (elementReference.equalsIgnoreCase("class")) { x=driver.findElement(By.className(referenceValue)); } else if (elementReference.equalsIgnoreCase("name")) { x= driver.findElement(By.name(referenceValue)); } else if (elementReference.equalsIgnoreCase("tagname")) { x= driver.findElement(By.tagName(referenceValue)); } else if (elementReference.equalsIgnoreCase("linktext")) { x= driver.findElement(By.linkText(referenceValue)); } else if (elementReference.equalsIgnoreCase("xpath")) { x= driver.findElement(By.xpath(referenceValue)); } else if (elementReference.equalsIgnoreCase("css")) { x= driver.findElement(By.cssSelector(referenceValue)); } } catch (Exception e) { e.printStackTrace(); } x.sendKeys(data); return x; } public static WebElement findElement(String elementReference, String referenceValue) throws Exception { WebElement x = null; try { if (elementReference.equalsIgnoreCase("id")) { x = driver.findElement(By.id(referenceValue)); } else if (elementReference.equalsIgnoreCase("class")) { x = driver.findElement(By.className(referenceValue)); } else if (elementReference.equalsIgnoreCase("name")) { x = driver.findElement(By.name(referenceValue)); } else if (elementReference.equalsIgnoreCase("tagname")) { x = driver.findElement(By.tagName(referenceValue)); } else if (elementReference.equalsIgnoreCase("linktext")) { x = driver.findElement(By.linkText(referenceValue)); } else if (elementReference.equalsIgnoreCase("xpath")) { x = driver.findElement(By.xpath(referenceValue)); } else if (elementReference.equalsIgnoreCase("css")) { x = driver.findElement(By.cssSelector(referenceValue)); } } catch (Exception e) { e.printStackTrace(); } x.click(); return x; } } |
Logic :ExcelUtils.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package keyworddrivenfwk.utils; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelUtils { private static XSSFSheet excelWSheet;//Sheet private static XSSFWorkbook excelWBook;//Workbook private static XSSFCell cell;//Cell public static void setExcelFile(String Path,String SheetName) throws Exception { FileInputStream ExcelFile = new FileInputStream(Path); excelWBook = new XSSFWorkbook(ExcelFile); excelWSheet = excelWBook.getSheet(SheetName); } public static String getCellData(int RowNum, int ColNum) throws Exception{ cell = excelWSheet.getRow(RowNum).getCell(ColNum); String CellData = cell.getStringCellValue(); return CellData; } public static void writeCellData(int RowNum, int ColNum,String result) throws Exception{ cell = excelWSheet.getRow(RowNum).createCell(ColNum); cell.setCellType(cell.CELL_TYPE_STRING); cell.setCellValue(result); FileOutputStream fos=new FileOutputStream("inputData.xlsx"); excelWBook.write(fos); fos.close(); } public static int getNoOfRows() { return excelWSheet.getLastRowNum(); } } |
Logic : testng.xml
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite" parallel="false" verbose="5"> <test name="Test"> <parameter name="browserType" value="CH" /> <classes> <class name="keyworddrivenfwk.tests.DriverScript"/> </classes> </test> <!-- Test --> </suite> <!-- Suite --> |
Logic :inputData.xlsx
Github Link for xlsx file:
https://github.com/totalqa9/KeywordDrivenFwk.git
Conclusion:
Using the Logic mentioned above helps to implement the Keyword Driven Framework successfully in your projects.
Let us know if you have any queries in implementing this framework.
Drop an email to totalqa9@gmail.com