Keyword Driven Framework Development…

Keyword Driven is more or less similar to Data Driven approach however in this case we will create test steps in an Excel sheet. Our test case will use the test steps and keywords like ‘openBrowser’, ‘navigate’ etc., from the Excel sheet and perform set of actions in a web page. A separate class file will have the definitions for these keywords. Now let’s create a sample framework based on what we discussed. We will use the same project from our last post but will rename the project to ‘FrameworkDesign’. You can easily a rename a project in Eclipse by clicking ‘F2’ to edit.

  1. The first step is to create our test steps in an Excel sheet and name it as ‘KeywordAction.xlsx’. We will place this Excel sheet inside the ‘com.selenium.data’ package. We can either do it by drag or drop or else you can go to the project location and directly paste it in ‘src/com/selenium/actions’ folder. ‘com.selenium.data’ package was already created in our last post.

    Test Steps
  2. Right click on the ‘src‘ folder and click New -> Package. Name the package as ‘com.selenium.actions’ and click Finish to close the window.
  3. Right click on ‘com.selenium.actions’ package and click New -> Class. Name the class as ‘Actions’. This class will contain definition of all keywords (Actions column) that we used in ‘KeywordAction.xlsx’ Excel. Copy the below code and paste into ‘Actions’ class. All the keywords (for example openBrowser, navigateURL, enterText etc) used in our Excel sheet are static methods in ‘Actions’ class.
    
    package com.selenium.actions;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class Actions {
    
    	public static WebDriver driver;
    
    	// Open Browser
    	public static void openBrowser() {
    		System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/Chrome/chromedriver");
    		driver = new ChromeDriver();
    
    	}
    
    	// Navigate to URL
    	public static void navigateURL() {
    		driver.get("http://www.google.com");
    		driver.manage().window().maximize();
    
    	}
    
    	// Enter text in text box
    	public static void enterText(String inputText) {
    		// Relative XPath for the text box
    		driver.findElement(By.xpath("//input[@id='lst-ib']")).sendKeys(inputText);
    
    	}
    
    	// Click Search icon
    	public static void clickButton() {
    		/* Find the Web Element Search icon. After finding, click the search icon */
    		driver.findElement(By.id("_fZl")).click();
    
    	}
    
    	// Click URL
    	public static void clickLink(String linkText) throws InterruptedException {
    		// Wait for page to load
    		Thread.sleep(5000);
    		// Enter text from test data Excel row 1 column 4
    		driver.findElement(By.partialLinkText(linkText)).click();
    	}
    
    	// Title verification
    	public static boolean verifyTitle(String titleText) throws InterruptedException {
    		// Wait for page to load
    		Thread.sleep(5000);
    		String title = driver.getTitle();
    		// Title verification. The third link is Wikipedia link
    		if (title.equals(titleText)) {
    			System.out.println("TITLE VERIFIED.");
    			System.out.println("TEST CASE PASSED");
    			return true;
    		} else {
    			System.out.println("TITLE NOT VERIFIED.");
    			return false;
    		}
    	}
    
    	// Close the browser
    	public static void closeBrowser() {
    		// Close and quit the driver to close the Browser
    		driver.close();
    	      driver.quit();
    	}
    }
    
  4. Next step is to create the test case. We will use the test steps from ‘KeywordAction.xlsx’ Excel. Right click on ‘com.selenium.testcase’ package, that we created in our last project and click New -> Class to add a new class file. Name the class file as ‘TC_KeySeleniumSearch’. Copy the below code into ‘TC_KeySeleniumSearch’.
    package com.selenium.testcase;
    
    import com.selenium.actions.Actions;
    import com.selenium.utils.Constants;
    import com.selenium.utils.ExcelUtil;
    
    public class TC_KeySeleniumSearch {
    
    	public static void main(String[] args) throws Exception {
    		// Set the path to KeywordAction.xlsx Excel sheet
    		String sPath = System.getProperty("user.dir") + "/src/com/selenium/data/KeywordAction.xlsx";
    
    		/* Set the Excel path with the sheet name mentioned in Constants class */
    		ExcelUtil.setExcelFile(sPath, Constants.SHEETNAME);
    
    		/*
    		 * We are looping all the 8 rows in Excel starting from row 1. Remember row 0 is the header in Excel sheet
    		 */
    		for (int rowNumber = 1; rowNumber < 8; rowNumber++) {
    
    			// This is to take the keywords from 3rd column
    			String sActionKeyword = ExcelUtil.getCellData(rowNumber, 3);
    
    			if (sActionKeyword.equals("openBrowser")) {
    				/* This will execute if Excel cell has 'openBrowser' */
    				Actions.openBrowser();
    			} else if (sActionKeyword.equals("navigateURL")) {
    				/* This will execute if Excel cell has 'navigateURL' */
    				Actions.navigateURL("http://www.google.com");
    			} else if (sActionKeyword.equals("enterText")) {
    				/* This will execute if Excel cell has 'enterText' */
    				Actions.enterText("Selenium");
    			} else if (sActionKeyword.equals("clickButton")) {
    				/* This will execute if Excel cell has 'clickButton' */
    				Actions.clickButton();
    			} else if (sActionKeyword.equals("clickLink")) {
    				/* This will execute if Excel cell has 'clickLink' */
    				Actions.clickLink("Wikipedia");
    			} else if (sActionKeyword.equals("verifyTitle")) {
    				/* This will execute if Excel cell has 'verifyTitle' */
    				Actions.verifyTitle("Selenium (software) - Wikipedia");
    			} else if (sActionKeyword.equals("closeBrowser")) {
    				/* This will execute if Excel cell has 'closeBrowser' */
    				Actions.closeBrowser();
    			}
    		}
    	}
    }
    
  5. The idea behind the Keyword Driven Framework is to have the test steps created in Excel. Each test step will have keywords to perform an action on our test website. This way the test cases becomes easily readable and maintainable.
  6. Below screenshot shows the folder structure of our project.

    Folder Structure
  7. The next step is to execute our test case. Right click on ‘TC_KeySeleniumSearch’ and click Run As -> Java Application. Once the execution is successfuly completed, we can see below output in the console.

    Console output

With this we came to end on the basics of Keyword Driven approach. This framework is similar to Data Driven one as both uses Apache POI jars. Please post your comments and let me know your thoughts…

Happy Learning!