Data Driven Framework in Selenium WebDriver | Software Testing Material

Data Driven Testing Framework in Selenium WebDriver

In this post, I will show you how to implement Data Driven Framework in Selenium WebDriver using Apache POI and TestNG data provider. There are different types of test automation frameworks in the market such as Modular, Data Driven, Keyword Driven, Page Object Model (actually it’s a design pattern), Hybrid Framework. Each type of framework has its own features.

Data Driven Testing Framework Architecture

What is Data Driven Framework

Data Driven framework is focused on separating the test scripts logic and the test data from each other. Allows us to create test automation scripts by passing different sets of test data. The test data set is kept in the external files or resources such as MS Excel Sheets, MS Access Tables, SQL Database, XML files etc., The test scripts connect to the external resources to get the test data. By using this framework we could easily make the test scripts work properly for different sets of test data. This framework significantly reduces the number of test scripts compared to a modular based framework.

Why Data Driven Framework

Usually, we place all our test data in excel sheets which we use in our test runs. Assume, we need to run a test script (Say, login test) with multiple test data. If we run the same test with multiple test data sets manually is time-consuming, and error-prone. In the next section, we see a practical example.

In simple words, we adopt Data Driven Framework when we have to execute the same script with multiple sets of test data.

Advantages of using Data Driven Test Framework

  • Re-usability of code
  • Improves test coverage
  • Faster Execution
  • Less maintenance
  • Permits better error handling

What is Apache POI

Apache POI is an open source library developed and distributed by Apache Software Foundation to design or modify Microsoft Office files using Java program. It is a popular API that allows working around excel files using Java Programs. In short, you can read and write MS Excel files using Java. Apache POI is your Java Excel solution.
You’d use HSSF if you needed to read or write an Excel file using Java (XLS). You’d use XSSF if you need to read or write an OOXML Excel file using Java (XLSX). It has many predefined methods, classes, and interfaces.

Learn more on Apache POI

How to work on Data Driven Framework in Selenium Using Apache POI

Selenium automates browsers. It’s a popular tool to automate web-based applications. To handle excel sheets to read and write data using Selenium we do use Apache POI.

Assume, you need to test login form with 50 different sets of test data

  1. As a manual tester, you do log in with all the 50 different sets of test data for 50 times
  2. As an automation tester, you create a test script and run 50 times by changing the test data on each run or you create 50 test scripts to execute the scenario

One of the reasons we take automation is to overcome the time consumption issue. By following the above two ways we don’t achieve anything with automation.

Data Driven testing helps here and save a lot of time of us.

How To Create Data Driven Framework in Selenium Using Apache POI

Here I will take Facebook Application to showcase implementation of Data Driven Framework in Selenium with Java using Apache POI.

Scenario: Open facebook page and do log in and log out.

Must Read: Complete Selenium WebDriver Tutorial

Follow below steps to Implement Data Driven framework.

First, we see how to read test data from excel sheet and then we see how to write the result in the excel sheet.

How To Read Data From Excel Sheet Using Apache POI

Prerequisites to implement Data Driven Framework:

  1. Eclipse IDE
  2. TestNG
  3. Selenium jars
  4. Apache POI jars
  5. Microsoft Excel

The structure of my project (Data Driven Project with Maven) is as follows:

Data Driven Framework Project Structure

Step 1: Open Eclipse and configure Apache POI jar files – Download Apache Jars

Note: If you are using Maven Project then you could include the dependencies in the pom.xml

Must Read: How to create Maven Project

Step 2: Open Excel Sheet and create some test data. Here I have saved my excel sheet on my D Drive.

Step 3: Create a Java Class under your Project

Here I have created a Java Class “DataDrivenTest” under my Project “DataDrivenProject”

Step 4: Here is a sample code to login to facebook.com by getting the user credentials from excel sheet. Copy the below code and paste it in your Java Class.

Step 5: Run your test script

How To Write Data From Excel Sheet Using Apache POI

Step 1: Follow step 1 as mentioned above

Step 2: Here I have created another column as “Result” on my excel sheet which is on my D Drive.

Step 3: Here is a sample code to login to facebook.com by getting the user credentials from excel sheet and write the result in the excel sheet. Copy the below code and paste it in your Java Class.

Step 4: Once it finished open the Excel file and check for the result.

Data Driven Framework in Selenium WebDriver using TestNG Data Provider

Scenario: Open Facebook and type username and password and login

Below test script runs 2 times with different sets of test data.

If you have any queries please comment below in the comments section.

Page Object Model with Page Factory in Selenium – Complete Guide

Here I have hand-picked few posts which will help you to learn some interesting stuff:

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

8 thoughts on “Data Driven Framework in Selenium WebDriver | Software Testing Material”

  1. I am not getting dependency for this XSSFWorkbook anywhere.Can you please help.I have added latest apache poi 3.17 dependency but XSSFWorkbook not present in this.

    Reply
  2. Hello
    I saw your code for data driven testing using excel file in TestNG. You created object array. but If we have 50 sets of data, it is hard to create 5o object. So could please pass the code using excel file in testNG ?I have not found the code in web. I hope you will provide the code
    Thanks

    Reply
  3. Hey Raj, your block is too good for the learners, I really appreciate your help and support.

    Raj, you gave a good example on @Dataprovider and how to use it in Data Driven Framework (by taking ‘Test Data’ in the class level itself (last example)).

    Can please tell me how to grab the details from the external source (Excel) using @dataprovider annotation.for the same FB login page.??

    Reply

Leave a Comment

Share via
Copy link
Powered by Social Snap