Open In App

Hybrid Framework in Selenium

Last Updated : 21 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. Test Data and Keywords Externalization: Test data and keywords are stored in external files, making the script more generalized and adaptable.
  2. 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.
  3. 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.
  4. 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.
  5. 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-Hybrid-Selenium
Workflow of Hybrid Framework in Selenium

Executing 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.

  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:

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 -

gfg

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 -

excel-sheet

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.

design-test-case

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.


Next Article
Article Tags :

Similar Reads