Data-driven testing with Selenium, Apache POI and TestNG

Before discussing Data-driven framework, let’s understand why do we need Framework for Test Automation?

A Test Automation framework is a set of assumptions, concepts, and practices that provide support for automated software testing. It is simply an execution environment for automated tests. Framework empowers testers to write valuable tests that are reusable, maintainable, scalable and resilient across all the browsers. Once the framework is created it can be used across the projects in an organization with few changes in configuration, test data & object repository. It helps in fast execution with minimal human intervention. A nontechnical user can write scripts with proper documentation.

Below are the popular Test Automation frameworks:

In this article, we will see how to automate the test scenario of login page of SenchaTutorials application with the Data-Driven framework.

Prerequisites

  • Eclipse IDE
  • Java
  • TestNG
  • Install Maven in eclipse
  • Firefox browser
  • Selenium Webdriver (supports all major browsers. We will use Firefox, Chrome, and IE)
  • Apache POI to perform operations with excel like read, write and update excel sheet

 

Getting Started

Download the project from https://github.com/walkingtree/Selenium-Automation and import to your Eclipse.

Data Driven framework is used to drive the test cases and suite from an external data feed. The data feed can be data sheets like xls, xlsx, and csv files.

TestNG is a testing framework created in line with the Junit, but with more features that makes it suitable for use in regression test automation projects. Data Provider is one such feature in TestNG. It allows a test method to be executed with multiple sets of data.

In this article, we will look at the process involved in using the Data Provider feature for supplying the test data from an excel sheet to a test method. In the article we will cover the following:

  1. Architecture for Data Driven Framework.
  2. What is a Data Provider?
  3. Test Scenario for login page
  4. How to provide Multiple sets of data to a Test method using DataProvider.
  5. Fetching data from Excel sheet using Data provider.

1) Architecture for Data Driven testing

The above diagram explains the architecture of data-driven framework, where Selenium Web Driver interacts with Application Under Test (AUT), locates the elements specified in automated test scripts and performs the actions. The advantage of TestNG framework with Maven is that it is able to create HTML reports. Data is read from Excel sheets and to read the data from Excel we use Apache POI library.

2) TestNg Data Provider

Data Provider is a method used for supplying the test data to a test method. It is used to test the App with multiple sets of data. The Data-driven concept is achieved by @Data Provider annotation in TestNG.

Data Provider returns a two-dimensional object to a test method. To supply the data for the test method it can be specified with an attribute “name” or if we do not specify the attribute “name” then the Data Provider name will be same as the corresponding method name.

Sample Data Provider with attribute name

Sample Data Provider without attribute name

3) Test Scenario for login page

1.Visit the login form of SenchaTutorials (http://senchatutorials.in/)

2.Find the login form’s username field and input text

3.Find the login form’s password field and input text

4.Find the submit button and click it

Expected Result: User shall login successfully and see the home page.

4) How to provide multiple sets of data to a test method  using DataProvider

The below code explains a test case where we test the “Sencha Tutorials” login Page with multiple sets of data using Data Provider.

We have two functions in this example:

  1. getData()
  2. testSenchaLogin()

getData() is the DataProvider function that is sending out test data in the form of an array of array of objects (object[][]). The test method testSenchaLogin() hooks onto the DataProvider by declaring that its data should be supplied by the DataProvider named “getData”.

testSenchaLogin() is the function, which contains the parameters for fetching data from DataProvider. It can contain conditions, loops, selenium commands, etc. Here we are having the parameters that match with an array in DataProvider and also the required selenium commands that need to be performed on the application.

In the above example, we have declared the test data array in our test script instead of using a spreadsheet. To achieve this we should call a function that fetches data from excel sheet and returns an object to the data provider.      

5) Fetching Data from Excel Sheet

Below is the function that fetches data from an Excel sheet. It uses Apache POI to fetch data from Excel sheet. Update the pom.xml file with POI dependencies as shown below.

 

It has two parameters

  1. path: the path of Excel file/workbook containing the data. This path is relative to the Java project
  2. sheetName: name of the Excel sheet that contains the data

Below code is used to read the data from excel sheet

Below code explains how to fetch the data from excel sheet and provide the data to test method.

Run this test by right clicking on the test script and select Run As > TestNG Test. Once it is finished, results will be shown, as shown below. As test data is provided three times, the test has executed thrice.

Summary

As part of this article, we learned about the Data-Driven framework of Selenium. Also, we learned about how we make use of TestNG’s Data Provider to read data from Excel spreadsheet and pass them to your test methods so that we can test your application with different data without changing the test script.

At WalkingTree, test automation is the core part of our development strategy and we strongly recommend our customers to get rid of the redundancies by making use of the appropriate test automation framework and associated strategy. We will be happy to assist you in taking advantage of our experience.

References

Related Posts

Comments (2)

Hi, How to write back the results to excel while using data provider

Hi Kiran,
I have one quick quesiton regarding Data Driven test using Testng.
I have updated excel with 5 rows of data and written code in the TestNG format where I have used for loop @Test.
When I ran the script through TestNG, all the five records data is captured and executed successfully.
But in the console it shows only one Test runs instead of 5 Test runs as for loop ran 5 times for different data.

Comment to BusyBee Cancel reply

Privacy Preferences
When you visit our website, it may store information through your browser from specific services, usually in form of cookies. Here you can change your privacy preferences. Please note that blocking some types of cookies may impact your experience on our website and the services we offer.