App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide Hybrid Framework in Selenium

Hybrid Framework in Selenium

By Neha Vaidya, Community Contributor -

The Selenium Framework is basically code structure that makes code maintenance easy and efficient. Without frameworks, users may place the code at some location which is not reusable or readable. Using frameworks leads to increased code reusability, higher portability, reduced cost of script maintenance, better code readability, etc. The Hybrid framework in Selenium offers these advantages by functioning as a combination of both Keyword-Driven Framework and Data-Driven framework.

This article will discuss how to implement the Hybrid framework in Selenium.

What is Hybrid Framework in Selenium?

Hybrid Driven Framework in Selenium is a mix of both the Data-Driven and Keyword Driven frameworks.

In this case, the keywords as well as the test data, are externalized. Keywords are stored in a separate Java class file and test data can be maintained in a Properties file or an Excel file.

Execution Flow of Hybrid Framework in Selenium

The diagram below depicts the architectural design of the Hybrid framework:

Hybrid framework in Selenium

  1. Execution starts with reading the configuration file. This file will reveal the location and name of the input file, as well as the location of the output file. This is the test suite
  2. The input file is an Excel sheet. It contains the first sheet as the Test Suite. The Test Suite sheet has four columns:
    1. Name of the test case
    2. Run Flag (whether to execute respective test cases or not)
    3. Comments
    4. Run Status
  3. Name of the test case in the Test Suite sheet is the same as the name of the Test Case sheet
  4. Each test case has 6 columns:
    1. Keyword
    2. Locator
    3. Argument
    4. Comment
    5. Run Status
    6. Return value
  5. Each row in a test case sheet represents a test step
  6. Comments are updated in the comment column if any test step fails or is skipped with the proper reason
  7. If a keyword returns some value, then that is updated in the Return value column
  8. Each step is executed in the sequential column. If one or more steps fail in a test case, the corresponding test case is also marked FAIL in the test suite
  9. Once all the test cases of a test suite are executed, and the corresponding status is updated in both sheets, a result file with a new name (timestamp appended) is saved in the Result folder
  10. Logs and screenshots are saved in the Output folder

Executing Hybrid Framework in Selenium

Components of the Hybrid Framework in Selenium are similar to those of the Keyword Driven Framework wherein all Test Data and Keywords are externalized, thus creating scripts in a more generalized form.

If you compare test case execution, the Hybrid framework in Selenium is almost the same as its Keyword-Driven counterpart with only a couple of changes required.

Components of Hybrid Framework in Selenium are as follows:

  1. Function Library
  2. Excel Sheet to store Keywords
  3. Design Test Case Template
  4. Object Repository for Elements/Locators
  5. Test Scripts or Driver Script

1. Function Library

User-defined methods are created for each user action. In other words, keywords are created in the library file.

As an example let’s automate the test cases below:

Test Case NoDescriptionTest StepsExpected Result
1Verify Browserstack logo 1. Enter URL – https://www.browserstack.comBrowserStack logo should be displayed in home page
2Verify valid signIn1. Enter URL – https://www.browserstack.com

2. Click on ‘SignIn’ link

3. Enter valid Email

4. Enter Valid Password

5. Click on SignInButton

Login should be successful
3Invalid login1. Enter URL – https://www.browserstack.com

2. Click on ‘SignIn’ link

3. Enter invalid Email

4. Click on continue

This error message should contain ‘cannot find an account’

2. Excel Sheet to store Keywords

Keywords that we created in the library file are stored in an Excel sheet with descriptions. This helps users to understand the framework and its components.

3. Test Case Design Template

For the framework to function, one must create a Test Case template. Both Test Data and Keywords should be externalized in Hybrid Framework, and the template needs to be created accordingly.

4. Object Repository for Elements

For all the web elements, users must maintain a separate Repository. Each WebElement is referred to with a name followed by its value. The Test Case template will hold the Object Name and the Repository value.

First, let’s create a library file for Keywords.

package Keywords;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Keywords {
public void click(WebDriver driver, String ObjectName, String typeLocator) 
throws IOException{
driver.findElement(this.getObject(ObjectName, typeLocator)).click();
}

By getObject(String ObjectName, String typeLocator) throws
IOException{
//Object Repository is opened 
File file = new File(path+"\\Externals\\Object Repository.properties");
FileInputStream fileInput = new FileInputStream(file);
//Properties file is read 
Properties prop = new Properties();
if(typeLocator.equalsIgnoreCase("XPATH")){
return By.xpath(prop.getProperty(ObjectName)); 
// ObjectName is read and its value is returned
}
else if(typeLocator.equalsIgnoreCase("ID")){
return By.id(prop.getProperty(ObjectName));
// ObjectName is read and its value is returned
}
else if(typeLocator.equalsIgnoreCase("NAME")){
return By.name(prop.getProperty(ObjectName));
// ObjectName is read and its value is returned
}
return null;
}
}

Run Selenium Tests on Real Device Cloud for Free

5. Driver Script

Let’s now write the driver script. This contains the main logic from the Test Case template Excel sheet. It will read all the Test Cases and perform corresponding actions by reading from the library file. The script is designed based on the Test Case template created.

package HybridFramework;
import java.lang.reflect.Method;
public class DriverScript{
public static Actions actionKeywords;
public static String sActions;
//This is reflection class object and declared as 'public static' and it can //be used outside the 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 = "Path to the exceldata.xlsx";
ReadExcelData.setExcelFile(sPath, "Sheet1");
for (int iRow=1;iRow<=9;iRow++)
{ sActions = ReadExcelData.getCellData(iRow, 1);
execute_Actions(); 

}
}
private static void execute_Actions() throws Exception {
actionKeywords = new Actions();
//It will load all the methods of the class 'Actions' in it. 
method = actionKeywords.getClass().getMethods();
//It will run for the number of actions in the Action Keyword class 
for(int i = 0;i<method.length;i++) {
if(method[i].getName().equals(sActions))
{
method[i].invoke(actionKeywords);
break;
}
}
}
}

Example: Test Case creation in Hybrid Framework using POM

Step 1: Create a class BrowserFactory

package com.automation.utility;
import org.openqa.selenium.WebDriver;
public class BrowserFactory {
public static WebDriver startApplication(WebDriver driver,String browserName, String appURL){
if(browserName.equals(“chrome”)){
System.setProperty(“webDriver.chrome.driver”,”.Path of chrome driver”);
driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
}
else if(browserName.equals(“Firefox”))
{
System.setProperty(“webDriver.gecko.driver”,”Path of driver”);
driver = new FirefoxDriver();
}
else if(browserName.equals(“IE”))
{
System.setProperty(“webDriver.ie.driver”,”Path of driver”);
driver = new InternetExplorerDriver();
}
else
{
System.out.println(“Does not support this browser”);
}
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(appURL);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
return driver;
}
public static void quitBrowser(WebDriver driver){
driver.quit();
}
}

Step 2: Create a New Class LoginTestCRM

package com.automation.testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
import com.automation.pages.LoginPage;
import com.automation.utility.BrowserFactory;
public class LoginTestCRM extends BaseClass {

@Test
Public void loginApp()
{
LoginPage loginpage = PageFactory.initElements(driver, LoginPage.class);
loginPage.loginToCRM(“Selenium”, “Password”);
}
}

Step 3: Create a New Class LoginPage

package com.automation.pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class LoginPage {
WebDriver driver;
public LoginPage(WebDriver ldriver)
{
this.driver=ldriver;
}
@FindBy(name=”user[login]”) WebElement uname;
@FindBy(name=”user[password]”) WebElement pass
@FindBy(name=”commit”) WebElement loginButton;

Public void loginToCRM(String usernameApplication, String passwordApplication)
{
uname.sendKeys(xxx);
pass.sendKeys(xxxxx);
loginButton.click();

}
}

Step 4: Create a class (BaseClass)

package com.automation.pages;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import com.automation.utility.BrowserFactory;
public class BaseClass {
public WebDriver driver;
@BeforeClass
public void setup()
{
driver= BrowserFactory.startApplication(driver, “Chrome”, “https://www.browserstack.com/users/sign_in”);
}
@AfterClass
public void teardown()
{
BrowserFactory.quitBrowser(driver);
}
}

Right-click on the LoginTestCRM class and run the application.

Try running the code, and utilize the Hybrid framework in Selenium for more sophisticated test creation and execution. Bear in mind that Selenium tests are best run on real browsers and devices, since emulators and simulators simply cannot provide the real user conditions required for 100% accurate test results.

Run manual and automated tests on real browsers and devices. Start running tests on 2000+ real browsers and devices on BrowserStack’s real device cloud. Run parallel tests on a Cloud Selenium Grid to get faster results without compromising on accuracy. Detect bugs before users do by testing software in real world circumstances with BrowserStack.

Run Selenium Tests on Cloud for Free

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

Data Driven Framework in Selenium

6 Things to avoid when writing Selenium Test Scripts

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack