What is Data Driven Framework in Selenium? Explain Hybrid & Keyword Driven Frameworks?

Last updated on : Nov 4, 2023 by Shubham Srivastava

If you want to know- What is Data Driven Framework in Selenium? Also, What is Hybrid Framework in Selenium? What is Keyword Driven Framework in Selenium? then, you are at correct place.

In this excellent guide we will understand, What are these framework models and what are the major difference between the data driven framework, hybrid and keyword driven framework in Selenium?
So, Let’s get started –

What is Data Driven Framework in Selenium - Hybrid-Keyword Driven Frameworks

What is Data Driven Testing?

Data Driven Testing is most popular approach of any Automation Framework development for Smoke and Regression Testing. Data Driven Testing is a simple approach, where we write Test Scripts by keeping our Test Data in an external source, such as – Excel file (.xls , .xlsx), CSV file (.csv), Text file (.txt) etc.

When implementing Data Driven Framework in a Selenium based framework, we need to separate the β€œdata set” from the actual β€œtest case” (code). Since the test case is separated from the data set, any SDET / Automation Test Engineer can easily update the test case of a particular functionality without making changes to the code.

Note πŸ’‘
Data Driven Testing is one of the most popular Automation Testing Interview Question for Freshers as well as Experienced folks. If you are interested to learn latest Selenium Interview Questions and Answers, then Click on the link now.

Example – Implementation of Data Driven Testing

Let’s say, Suppose you have a simple test scenario where you want to test a login page with different sets of usernames and passwords.

An example of Data Driven Framework in Selenium

Step 1: Prepare the Test Data

Create a spreadsheet (e.g., Excel) with columns for test data, such as “Username” and “Password.” Fill in the test data for various scenarios:

| Username          | Password     |
|-------------------|--------------|
| user1@example.com | password1    |
| user2@example.com | password2    |
| user3@example.com | password3    |

Step 2: Create the Selenium Test Script

Once the Test Data is ready, the next step is to write the Test code. Here is an example –

/******************************************************************************

                            LogicalDuniya.com
               Sample Program to implement Data Driven Testing
       Copy this program, and run using atleast Java 8 to get the results.

*******************************************************************************/

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class DataDrivenExampleTest 
{
    public static void main(String[] args) 
    {
        // Set the path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize the WebDriver
        WebDriver driver = new ChromeDriver();

        // Load the login page
        driver.get("https://example.com/login");

        // Assuming you have a method to read data from the spreadsheet
        Object[][] testData = readTestDataFromSpreadsheet();

        for (int i = 0; i < testData.length; i++) {
            String username = testData[i][0].toString();
            String password = testData[i][1].toString();

            // Find the username and password fields
            WebElement usernameField = driver.findElement(By.id("username"));
            WebElement passwordField = driver.findElement(By.id("password"));

            // Enter the data from the test case
            usernameField.sendKeys(username);
            passwordField.sendKeys(password);

            // Submit the form (assuming you have a login button)
            WebElement loginButton = driver.findElement(By.id("login-button"));
            loginButton.click();

            // Add verification or assertion code here

            // Clear the fields for the next iteration
            usernameField.clear();
            passwordField.clear();
        }

        // Close the browser
        driver.quit();
    }

    // Implement a method to read test data from the spreadsheet
    public static Object[][] readTestDataFromSpreadsheet() {
        // In this example, you would read data from the spreadsheet file.
        // You can use libraries like Apache POI or CSV parsers for this.
        // For simplicity, we'll just create some test data here.
        return new Object[][] {
            {"user1@example.com", "password1"},
            {"user2@example.com", "password2"},
            {"user3@example.com", "password3"}
        };
    }
}

Step 3: Execute the Test

You need to replace "path/to/chromedriver" with the actual path to your ChromeDriver executable. Execute this script to perform the login test for each set of data.

With this approach, you can easily add or modify test data in the spreadsheet without changing your test script, making your testing process more efficient and maintainable.

Advantages – Of Data Driven Testing Framework

There are so many advantages of Data Driven Testing with Selenium or any Automation Library. Here are some of them –

  1. It allows to test the application with multiple sets of data values during Smoke, Sanity and Regression Testing.
  2. It allows to efficiently separate the test case data from the test script code.
  3. It helps to generate test data automatically. This is helpful when large volumes of random test data are required.
  4. As a result, we develop a framework which is far more easier to maintain.
  5. It allows developers and automation test engineers to separate the logic of their test cases/ test scripts from the test data.
  6. It also help to execute test cases multiple times which helps to reduce test cases and scripts.
  7. When using Data Driven Testing, we do not need to make changes in test scripts for test data.

By implementing Data-Driven Testing using Selenium, testers can define their test cases for more efficient execution. This reduces the development time, makes lives a lot easier and results in more deeper testing and better quality software.


Keyword Driven Framework & Testing Approach

The Keyword-Driven Framework in automation testing simplifies the process of testing by using keywords to represent specific actions or operations. These keywords take input parameters and provide output. Here’s how it works in simple terms:

Keyword Driven Framework Flow Example
  1. Keywords as Actions:
    Instead of writing complex code for each functionality, testers define keywords to represent specific actions. For example, a keyword like “login” could represent the action of logging into a system.
  2. Separate Operations:
    These keywords are written as separate operations or methods, away from the main testing script. It’s like having a library of actions that can be reused.
  3. External Keyword File:
    The keywords are stored in an external file, often in an Excel sheet. The testing script (code) then calls these keywords as needed.
  4. Mapping Keywords to Actions:
    Each test case is constructed by using keywords. Testers map these keywords to the relevant actions, and they can pass parameters to the keywords if needed.
  5. User-Friendly Test Cases:
    Testers can create and modify test cases without having to deal with the complex underlying automation code. This makes the testing process more user-friendly and less dependent on coding skills.

Overall, a Keyword-Driven Framework allows testers to create and manage test cases using simple language keywords, making the automation testing process more efficient and accessible to a broader range of team members.


Hybrid Framework & Testing Approach

The Hybrid Driven Framework in automation testing is like the best of both worlds, combining the strengths of both the Data-Driven and Keyword-Driven frameworks.

Here’s what it contains and how it works :

  1. Mix of Data and Keywords:
    The Hybrid Framework brings together two key elements – keywords and test data. Keywords represent the actions to be taken, while test data contains the specific values for those actions.
  2. Externalized Keywords:
    Instead of embedding keywords directly into the test script, they are organized in a separate Java class file. This separation keeps the code clean and makes it more reusable.
  3. External Test Data:
    The test data, which specifies what inputs should be used for each test, is stored in a Properties file or an Excel file. This allows easy management and updates without altering the code.
  4. Advantages of Framework:
    The whole purpose of any framework is to make code maintenance easier. Without a framework, code might end up scattered and hard to reuse. A framework increases code reusability, makes the code more portable, reduces script maintenance costs, and improves code readability.
  5. Combining the Best:
    The Hybrid Framework combines the strengths of both the Keyword-Driven Framework (user-friendly, plain language keywords) and the Data-Driven Framework (multiple sets of test data). This combination helps achieve these advantages efficiently.

In a nutshell, the Hybrid Framework with Selenium takes the best features from different frameworks to make automation testing more efficient, organized, and easier to manage.


Conclusion

In this Testing Frameworks guide, I hope you have understood – What is Data Driven Framework? What is Keyword-Driven Framework, and What is Hybrid Framework? We have also learnt core advantages of these Testing Frameworks along with simple examples.

Check these super helpful useful guides –

Keep Thinking Logical..
Thanks from Team LogicalDuniya.com
Cheers, Shubham !!

Shubham

Shubham Srivastava

Hey there, my name is Shubham and I'm working as an SDET at a US based FinTech company. I've more than 7 years of working experience and using my knowledge - I want make this platform super helpful for those, who want to solve any software development and testing related problems. Comment your query / request or suggestion in the below section, and I'll try to respond within 24 hours. Keep improving, Keep coding !!

Recommended Posts :


5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Press ESC to close