Hybrid Framework in Selenium
Last Updated :
21 Aug, 2024
Selenium, an open-source test automation tool, has been a mainstay in web application testing since its release in 2004. Supporting various programming languages such as Node.js, Python, Java, JavaScript, C#, and PHP, Selenium has gained widespread popularity due to its versatility. Today, we'll dive into the Hybrid Framework in Selenium—a robust, flexible approach to test automation.
What is a Hybrid Framework in Selenium?
A Hybrid Framework in Selenium is a combination of multiple frameworks, primarily Data-Driven and Keyword-Driven frameworks. This blend leverages the strengths of both approaches, creating a testing environment that is both powerful and adaptable.
- Data-Driven Framework: This framework uses external data sources like Excel, CSV, XML, or databases to drive test cases. The test data can be modified independently of the code, allowing for easy updates and maintenance.
- Keyword-Driven Framework: This framework speeds up automated testing by separating keywords that represent common functions and instructions. These keywords, along with test data, are stored in external files such as Excel.
Execution Flow of Hybrid Framework in Selenium
The execution flow of a Hybrid Framework in Selenium involves several steps:
- Test Data and Keywords Externalization: Test data and keywords are stored in external files, making the script more generalized and adaptable.
- Function Library: Contains reusable functions that perform common actions on the web application. These functions are called by the test case design template to execute specific actions.
- Test Case Design Template: This template defines the structure of test cases. It reads the test steps from external sources and executes them in sequence. Each test step corresponds to an action (e.g., click, enter text) and is mapped to a function in the function library.
- Object Repository: A centralized storage location where all the web elements (such as buttons, text fields, checkboxes, etc.) are managed and stored. This allows scripts to be more maintainable and easier to update when the application UI changes.
- Driver Script: The entry point for test execution. It initializes the WebDriver, reads the test cases from the external source (like an Excel file), and triggers the execution of each test case.
Here is the depiction of the execution flow -
Workflow of Hybrid Framework in SeleniumExecuting Hybrid Framework in Selenium
Components of hybrid framework such as test data and keywords are externalized .This approach makes the script more generalized and adaptable.
- Function Library
- Excel Sheet to store Keywords
- Design Test Case Template
- Object Repository for Elements/Locators
- Test Scripts or Driver Script
1. Function Library:
The function library contains reusable functions that perform common actions on the web application.These functions are called by the test case design template to execute specific actions.
For Example: Let us take an instance to automate the below test cases -
First, the test cases and their test steps are analyzed and their actions are noted down.
Say,
- In TC 01: Verify gfg logo present- the user actions will be: Enter URL
- In TC 02: Verify Valid SignIn- the user actions are Enter URL, Click, TypeIn
- In TC03: Verify Invalid Login- the user actions are Enter URL, Click, TypeIn
2. Excel Sheet to store Keywords
Stores test data and keywords.
Example -
3. Test Case Design Template
The test case design template defines the structure of the test cases . It reads the test steps from the external source and executes them in sequence.Each test step corresponds to an action (e.g., click, enter text) and is mapped to a function in the function library.
4. Object Repository for Elements
An object repository is a centralized storage location where all the web elements (such as buttons text fields, checkboxes, etc.) used in automation scripts are manged and stored.Object Repository allows you to define these elements in one place, making your scripts more maintainable and easier to manage.
let's create a library file for keywords -
Java
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;
public class Keywords {
private String path; // Define the path variable
public Keywords(String path) {
this.path = path;
}
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\\ObjectRepository.properties");
Properties prop = new Properties();
try (FileInputStream fileInput = new FileInputStream(file)) {
// Properties file is read
prop.load(fileInput);
} catch (IOException e) {
e.printStackTrace();
throw e; // Rethrow the exception to indicate failure in reading the file
}
// Determine the locator type and return the appropriate By object
if (typeLocator.equalsIgnoreCase("XPATH")) {
return By.xpath(prop.getProperty(ObjectName));
} else if (typeLocator.equalsIgnoreCase("ID")) {
return By.id(prop.getProperty(ObjectName));
} else if (typeLocator.equalsIgnoreCase("NAME")) {
return By.name(prop.getProperty(ObjectName));
} else {
throw new IllegalArgumentException("Invalid locator type: " + typeLocator);
}
}
}
Expected Output:
case 1 : When the click method executed
- The getObject method will read the locator for the given ObjectName from the properties file.
- The code then attempts to locate the element using Selenium's By locator (e.g., By.xpath, By.id, or By.name).
- Once the element is found, it will perform a click action on it.
case 2: If the element is not found or there is an issue with reading the properties file
- An exception could be thrown (e.g., NoSuchElementException if the element is not found, or IOException if there’s an issue reading the file)
5. Driver Script
The Driver Script is the entry point for the test execution. It intializes the webdriver, reads the test cases from the external source (like Excel file) and triggers the execution of each test case.
Here's a simple example of a driver script in Java that uses the Selenium WebDriver and the Keywords class you provided earlier -
Java
package TestAutomation;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import Keywords.Keywords;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class DriverScript {
public static void main(String[] args) {
// Set up WebDriver (assuming ChromeDriver is used)
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
// Maximize the browser window
driver.manage().window().maximize();
// Define a base URL
String baseUrl = "https://example.com";
// Open the base URL
driver.get(baseUrl);
// Implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Path to the Object Repository properties file
String objectRepoPath = "path/to/your/objectRepository.properties";
// Create an instance of Keywords class with the path to the Object Repository
Keywords keywords = new Keywords(objectRepoPath);
try {
// Perform actions using keywords
keywords.click(driver, "loginButton", "XPATH");
// You can add more actions as needed
// keywords.type(driver, "usernameField", "USERNAME", "ID");
// keywords.type(driver, "passwordField", "PASSWORD", "NAME");
// keywords.click(driver, "submitButton", "XPATH");
} catch (IOException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}
Expected Output:
Browser Actions -
- The browser will open, navigate to https://example.com, and attempt to click the element specified by the "loginButton" XPath locator.
Console Output -
- If the actions are successful and there are no exceptions, there will be no output in the console.
- If the element with the locator "loginButton" is not found, a NoSuchElementException will be thrown, and the stack trace will be printed in the console.
- If there is an issue reading the properties file (e.g., file not found or incorrect path), an IOException will be caught, and its stack trace will be printed.
Browser Behavior -
- The browser will navigate to the specified URL, attempt the action (clicking the button), and then close.
Conclusion
In conclusion, a hybrid Selenium automation framework combines the strengths of both keyword-driven and data-driven approaches, offering a versatile and scalable solution for test automation. By externalizing test data, keywords, and object repositories, the hybrid framework allows for greater flexibility, maintainability, and reusability of test scripts. This modular design not only simplifies the management of test cases but also facilitates collaboration among team members, making it easier to adapt to changes in the application under test. Overall, a hybrid framework is an effective strategy for achieving robust and efficient test automation, capable of handling complex testing scenarios with ease.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read