Building A Data Driven, Keyword Driven & Hybrid Selenium Framework

Last updated on Sep 11,2024 68.2K Views
Vardhan is a technology enthusiast working as a Sr. Research Analyst at... Vardhan is a technology enthusiast working as a Sr. Research Analyst at Edureka. He has expertise in domains like Big data, Cloud computing and...

Building A Data Driven, Keyword Driven & Hybrid Selenium Framework

edureka.co

The previous Selenium blogs in this series have given you exposure to basic concepts in Selenium testing. However, in this blog, I will tell you how to use a Selenium framework to optimize your code structure and this will move you closer to getting certified with the Selenium Certification.

What is a Selenium framework?

Selenium framework is a code structure for making code maintenance simpler, and code readability better. A framework involves breaking the entire code into smaller pieces of code, which test a particular functionality.

The code is structured such that, the “data set” is separated from the actual “test case” which will test the functionality of the web application. It can also be structured in a way wherein, the test cases which need to be executed are called (invoked) from an external application (like a .csv).

There are a number of frameworks out there, but 3 commonly used Selenium framework (s) are:

These frameworks will be discussed with a demo in this blog. But before going any further, let me tell you why a Selenium framework needs to be in place, and what benefits you will get out of using them.

Why do we need a Selenium framework? 

Without a framework in place, there will be one test case which will comprise the entire test functionality. The scary part is, this single test case has the capability to rise up to a million lines of code. So its pretty obvious that a test case so huge will be tough to read. Even if you want to modify any functionality later, then you will have a tough time modifying the code.

Since the implementation of a framework, will result in smaller but multiple code pieces, there are various benefits. 

Find out our Selenium Training in Top Cities/Countries

IndiaOther Cities/Countries
BangaloreUS
HyderabadUK
PuneCanada
ChennaiAustralia
MumbaiSingapore
KolkataEdinburgh

Benefits of Selenium framework

Now that you know the basics of frameworks, let me explain each of them in detail. 

Data Driven Framework

A Data Driven framework in Selenium is the technique of separating the “data set” from the actual “test case” (code). This framework completely depends on the input test data. The test data is fed from external sources such as an excel file, .CSV file or any database.

Since the test case is separated from the data set, we can easily modify the test case of a particular functionality without making wholesale changes to your code. For example, if you want to modify the code for login functionality, then you can modify just that instead of having to also modify any other dependent portion in the same code.

Besides this, you can also easily control how much data needs to be tested. You can easily increase the number of test parameters by adding more username and password fields to the excel file (or other sources).

For example, if I have to check the login to a web page, then I can keep the set of username and password credentials in an excel file and pass the credentials to the code to perform automation on the browser in a separate Java class file.

Using Apache POI With Selenium WebDriver 

WebDriver does not directly support reading of excel files. Hence we use Apache POI for reading/ writing to any Microsoft office document. You can download Apache POI (set of JAR files) from here. Download the zip file or tar file as per your requirement and place them along with the set of Selenium JARs.

The co-ordination between the main code and data set will be taken care by TestNG Data Providers, which is a library that comes as a part of the Apache POI JAR files. For demo purpose, I have created an excel file called “LoginCredentials” in which the usernames and passwords have been stored in different columns.

Take a look at the below code to understand the test case. It is a simple code for testing the login functionality of a flight booking application.

package DataDriven;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DDTExcel
{
 ChromeDriver driver;
 
 @Test(dataProvider="testdata")
 public void DemoProject(String username, String password) throws InterruptedException
 {
 System.setProperty("webdriver.chrome.driver", "C:UsersVardhanDownloadschromedriver.exe");
 driver = new ChromeDriver();
 
 driver.get("http://newtours.demoaut.com/");
 
 driver.findElement(By.name("userName")).sendKeys(username);
 driver.findElement(By.name("password")).sendKeys(password);
 driver.findElement(By.name("login")).click();
 
 Thread.sleep(5000);
 
 Assert.assertTrue(driver.getTitle().matches("Find a Flight: Mercury Tours:"), "Invalid credentials");
 System.out.println("Login successful");
 }
 
 @AfterMethod
 void ProgramTermination()
 {
 driver.quit();
 }

@DataProvider(name="testdata")
 public Object[][] TestDataFeed()
 {
 
 ReadExcelFile config = new ReadExcelFile("C:UsersVardhanworkspaceSeleniumLoginCredentials.xlsx");
 
 int rows = config.getRowCount(0);
 
 Object[][] credentials = new Object[rows][2];

for(int i=0;i<rows;i++)
 {
 credentials[i][0] = config.getData(0, i, 0);
 credentials[i][1] = config.getData(0, i, 1);
 }
 
 return credentials;
 }
}

If you noticed from above, we have a method named “TestDataFeed()”. In this method, I have created an object instance of another class named “ReadExcelFile”. While instantiating this object, I have fed the path of my excel file containing the data. I have further defined a for loop to retrieve the text from the excel workbook.

But, for reading the data from a given sheet number, column number and row number, the calls are made to the “ReadExcelFile” class. The code of my “ReadExcelFile” is below.

package DataDriven;

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelFile
{
 XSSFWorkbook wb;
 XSSFSheet sheet;
 
 public ReadExcelFile(String excelPath)
 {
 try
 {
 File src = new File(excelPath);
 FileInputStream fis = new FileInputStream(src);
 wb = new XSSFWorkbook(fis);
 }
 
 catch(Exception e)
 {
 System.out.println(e.getMessage());
 }
 }
 
 public String getData(int sheetnumber, int row, int column)
 {
 sheet = wb.getSheetAt(sheetnumber);
 String data = sheet.getRow(row).getCell(column).getStringCellValue();
 return data;
 }
 
 public int getRowCount(int sheetIndex)
 {
 int row = wb.getSheetAt(sheetIndex).getLastRowNum();
 row = row + 1;
 return row;
 }
}

First note the libraries I have imported. I have imported Apache POI XSSF libraries which are used to read/ write data to excel files. Here, I have created a constructor (object of the same method) to pass the values: sheet number, row number and column number. To understand this framework better, I request you to go through the below video, where I have explained this in a structured manner.

Data Driven Framework In Selenium WebDriver | Selenium Tutorial

 

Now let’s move on to the framework, i.e Keyword Driven framework.

Keyword Driven Framework

Keyword Driven framework is a technique in which all the operations & instructions to be performed are written separately from the actual test case. The similarity it has with Data Driven framework is that, the operations to be performed is again stored in an external file like Excel sheet.

The operations I’m talking about is nothing but the methods that need to be executed as part of a test case. The benefit with Keyword Driven framework is that you can easily control the functionalities you want to test. You can specify the methods which test the functionality of the application in the excel file. Thus, only those method names which are specified in the excel will be tested.

For example, for logging into the web application, we can write multiple methods in the main test case, in which each test case will test certain functionality. For instantiating the browser driver there could be one method, for finding the username & password fields, there could be methods, for navigating to a web page there could be another method, etc.

Take a look at the below code for understanding how the framework looks. The lines which are commented out in the below code serve as explanation if you don’t understand.

package KeywordDriven;

import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class Actions 
{
 public static WebDriver driver;
 
 public static void openBrowser()
 { 
 System.setProperty("webdriver.chrome.driver", "C:UsersVardhanDownloadschromedriver.exe");
 driver=new ChromeDriver();
 }
 
 public static void navigate()
 { 
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 driver.get("http://newtours.demoaut.com");
 }
 
 public static void input_Username()
 {
 driver.findElement(By.name("userName")).sendKeys("mercury"); 
 }
 
 public static void input_Password()
 {
 driver.findElement(By.name("password")).sendKeys("mercury");
 }
 
 public static void click_Login()
 {
 driver.findElement(By.name("login")).click();
 }

@Test
 public static void verify_login()
 {
 String pageTitle = driver.getTitle();
 Assert.assertEquals(pageTitle, "Find a Flight: Mercury Tours:");
 }

 public static void closeBrowser()
 {
 driver.quit();
 }
}

As you can see, the different functionalities which need to be tested are present in separate methods waiting to be called. Now, these methods will be called from another Class, based on the presence of the method name in the excel file. And similarly, to read the excel file, and send back the results, I have written another Class. Both of them are displayed below.

The class file invoking the methods, is this.

package KeywordDriven;

public class DriverScript
{
 public static void main(String[] args) throws Exception 
 {
 //Declaring the path of the Excel file with the name of the Excel file
 String sPath = "C:UsersVardhanworkspaceSelenium Frameworks DemodataEngine.xlsx"; 

 //Here we are passing the Excel path and SheetName as arguments to connect with Excel file
 ReadExcelData.setExcelFile(sPath, "Sheet1");

&nbsp;//Hard coded values are used for Excel row & columns for now&nbsp; &nbsp;&nbsp; 
 //Hard coded values are used for Excel row & columns for now&nbsp; &nbsp; 
 //In later chapters we will replace these hard coded values with varibales&nbsp; &nbsp; //This is the loop for reading the values of the column 3 (Action Keyword) row by row
 for (int iRow=1;iRow<=7;iRow++)
 {
 String sActions = ReadExcelData.getCellData(iRow, 1); 

 //Comparing the value of Excel cell with all the keywords in the "Actions" class
 if(sActions.equals("openBrowser"))
 { 
 //This will execute if the excel cell value is 'openBrowser'&nbsp; &nbsp; 
 //Action Keyword is called here to perform action
 Actions.openBrowser();
 }
 else if(sActions.equals("navigate"))
 {
 Actions.navigate();
 }
 else if(sActions.equals("input_Username"))
 {
 Actions.input_Username();
 }
 else if(sActions.equals("input_Password"))
 {
 Actions.input_Password();
 }
 else if(sActions.equals("click_Login"))
 {
 Actions.click_Login();
 } 
 else if(sActions.equals("verify_Login"))
 {
 Actions.verify_login();
 } 
 else if(sActions.equals("closeBrowser"))
 {
 Actions.closeBrowser();
 } 
 }
 }
}

And the class file which reads the Excel values is this.

package KeywordDriven;

import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;

public class ReadExcelData
{
 private static XSSFSheet ExcelWSheet;
 private static XSSFWorkbook ExcelWBook;
 private static XSSFCell Cell; 
 
 //This method is to set the File path and to open the Excel file
 //Pass Excel Path and SheetName as Arguments to this method
 public static void setExcelFile(String Path,String SheetName) throws Exception 
 {
 FileInputStream ExcelFile = new FileInputStream(Path);
 ExcelWBook = new XSSFWorkbook(ExcelFile);
 ExcelWSheet = ExcelWBook.getSheet(SheetName);
 }
 
 //This method is to read the test data from the Excel cell
 //In this we are passing parameters/arguments as Row Num and Col Num
 public static String getCellData(int RowNum, int ColNum) throws Exception
 {
 Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
 String CellData = Cell.getStringCellValue();
 return CellData;
 }
}

Now, let’s move onto the final part of this Selenium framework blog, where I will show you how to build a Hybrid framework.

For details, You can even check out test automation strategies and methodology concepts with the Automation testing online course.

Hybrid framework 

Hybrid framework is a technique wherein we can make the best use of both Data Driven & Keyword Driven Selenium framework (s). Using the examples shown above in this blog, we can build a Hybrid framework by storing the methods to execute in an excel file (keyword driven approach) and passing these method names to the Java Reflection Class (data driven approach) instead of creating an If/Else loop in the “DriverScript” class.

Take a look at the modified “DriverScript” class in the below code snippet. Here, instead of using multiple If/ Else loops, data driven approach is used to read the method names from the excel file.

package HybridFramework;

import java.lang.reflect.Method;

public class DriverScriptJava
{
 //This is a class object, declared as 'public static'
 //So that it can be used outside the scope of main[] method
 public static Actions actionKeywords;
 
 public static String sActions;

 //This is reflection class object, declared as 'public static' 
 //So that it can be used outside the scope of main[] method
 public static Method method[];
 
 public static void main(String[] args) throws Exception 
 {
 //Declaring the path of the Excel file with the name of the Excel file
 String sPath = "C:UsersVardhanworkspaceSelenium Frameworks DemodataEngine.xlsx";

&nbsp;//Here we are passing the Excel path and SheetName to connect with the Excel file&nbsp; &nbsp;&nbsp; 
 //This method was created previously
 ReadExcelData.setExcelFile(sPath, "Sheet1");

 //Hard coded values are used for Excel row & columns for now&nbsp; &nbsp;&nbsp; 
 //Later on, we will use these hard coded value much more efficiently&nbsp; &nbsp; 
 //This is the loop for reading the values of the column (Action Keyword) row by row 
 //It means this loop will execute all the steps mentioned for the test case in Test Steps sheet
 for (int iRow=1;iRow<=7;iRow++)
 {
 sActions = ReadExcelData.getCellData(iRow, 1);
 //A new separate method is created with the name 'execute_Actions'
 //You will find this method below of the this test 
 //So this statement is doing nothing but calling that piece of code to execute
 execute_Actions(); 
 }
 }

//This method contains the code to perform some action 
//As it is completely different set of logic, which revolves around the action only, it makes sense to keep it separate from the main driver script 
//This is to execute test step (Action)
private static void execute_Actions() throws Exception 
 {
 //Here we are instantiating a new object of class 'Actions'
 actionKeywords = new Actions();

 //This will load all the methods of the class 'Actions' in it. 
 //It will be like array of method, use the break point here and do the watch 
 method = actionKeywords.getClass().getMethods();

 //This is a loop which will run for the number of actions in the Action Keyword class 
 //method variable contain all the method and method.length returns the total number of methods
 for(int i = 0;i<method.length;i++)
 {
  //This is now comparing the method name with the ActionKeyword value received from the excel
  if(method[i].getName().equals(sActions))
 { //In case of match found, it will execute the matched method 
  method[i].invoke(actionKeywords);
   //Once any method is executed, this break statement will take the flow outside of for loop
  break;
 }
 }
 }
}

Learn more about testing methodologies and their concepts from the Manual testing course online.

To understand this concept of Data Driven, Keyword Driven & Hybrid Driven frameworks better, I request you to watch the below video.

Selenium Framework using Java | Selenium Tutorial | Selenium Training Online

 

I hope this blog was useful to you and gave you a clear understanding of what a Selenium framework is, how it is beneficial and how to build your code structure using these 3 Selenium frameworks. Stay tuned to more blogs in this series.

If you wish to learn Selenium and build a career in the testing domain, then check out our interactive, live-online training on selenium, which comes with 24*7 support to guide you throughout your learning period. The concepts related to “Selenium Framework” has an in-depth coverage in Edureka’s course.

Got a question for us? Please mention it in the comments section and we will get back to you.

Upcoming Batches For Selenium Course
Course NameDateDetails
Selenium Course

Class Starts on 25th November,2024

25th November

MON-FRI (Weekday Batch)
View Details
Selenium Course

Class Starts on 30th November,2024

30th November

SAT&SUN (Weekend Batch)
View Details
Selenium Course

Class Starts on 21st December,2024

21st December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES