Data Driven Framework in Selenium

In this tutorial, We will learn the designing concept theData Driven Framework in Selenium.

NoteTo understand this tutorial better, Please start from very first tutorial of Page Object Model. This will give you the better understanding and design concept.


In previous tutorials, we have seen how can we design and implement POM Framework. In this tutorial, we are going to extend the existing project and will apply the concept of Data Driven on the Existing project. So for better understanding, it’s better to start from very first tutorial of Page Object Model.


What is DataDriven ?

In Data Driven Framework , We kept Test case logic resides in Test Scripts, the Test Data is separated and kept outside the Test Scripts.Test Data is read from the external files (Excel Files, CSV Files, Database) and are loaded into the variables inside the Test Script. Variables are used both for Input values and for Verification values. In Data Driven Framework Single Test case iterate over the Test Data.


For example, We have Login test Case and we want to test it with Combination of Username and Password then, in this case, we will keep our Test Data in some external file and read this data and then pass it to our Test Script. Let’s see how can we implement it.


As we know We are extending our existing framework further. First, Let’s see the Project Structure again.

Data Driven Framework


We have already discussed this structure in our last tutorials. But we have some new files here.

In TestData.xlsx file we will keep our Test Data, which later we will pass to our Parameterized Test Cases. Now let’s understand the structure of TestData.xlsx file and see how can we read the data from this file.
(We are using Excel so to run this code on your local machine, First you need to configure the Apache POI in your project

Data Driven Framework


In our TestData.xlsx file we have our test Data. Let’s What it is –

  1. We have separate DataSheet for each TestClass. Like – LoginSheet for loginPageTest.Java Test Class, ProfileSheet for Test Class.

  2. In each sheet, we define the Test Data, specific to that sheet only. In the First Column, we define the TestName, which should be similar to Test Name in the Test Script for which that TestSheet belongs. Like “loginTest” is our Test defined in Test file.
    After defining the Test Name we will define the Test Data specific to that Test only. Each Column Define the Fields (to which we want to pass the Test Data) and Each Row define the Actual Test Data.


Here each sheet can contain the Data for multiple Tests. Like, suppose have 5 Test cases. Similar to above sample sheet we will define the Test Data for all 5 tests in the single ‘loginSheet‘ . We just need to take care testHeading in the test data sheet should be similar to Test Name mentioned in the LoginPageTest. java file.


Now We will see how can we read the Test Data from TestData file which is specific to TestCase only.

For this we are creating an function in file named getData() . As we have seen in the TestNG Tutorials , TestNG Data Provider reads the two-dimensional array. So getData() function returns us two dimensional array.


We are implementing our code in such a way that our Two-dimensional arrays will contain only one column and multiple rows.

  • Each Row will contain the one Hashtable of Java Collection.

  • Each hash table will contain the Test Data for the TestCase in the form of key-value pair. Suppose we have 3 rows for our Test loginTest then getData() function array will contain the three hash tables, which we will pass to our Test Case. So our Test will iterate 3 times over Test Data.


Now we will see, How can we read the Data from Test Datasheet.

  • First, we will find the Row, from which the Test Starts.

  • Next, find a number of columns for this Test Case. (This represents the Number of fields for Test)

  • Next, find Number of Rows for that test. (This represents the number of Data Rows)

  • Now put this Test Data into HashTable and put the HashTable into Two-Dimensional Array.


The above function will return the two-dimensional array and that array will contain the Test Data & TestCase Name.


Now we will see how can we use this with our TestCase. To use this function, We will implement the DataProvider in our Test Class and which should be specific to Test Case and then use that Data Provider. Let’s see understand this with the help of below-mentioned code.


In the above code, we have declared the DataProvider. In the DataProvider, we are reading the Data for LoginTest. We are just passing the SheetName and TestName to getData() function and this will return the HashTable.


This HashTable will return the values in Key-value pair. Here Key always will be same and values will vary according to the data mentioned in the Data Sheet.

(As per the getData() code for loginTest key will be Username and Password, and Values will depend on number of rows mentioned for the TestCase.)


You can see in the above @Test annotation we have declared our TestCase code. We have implemented the Test case loginTest , Which is using the DataProvider named getLoginData. In this TestMenthod, we have passed an HashTable to get the data from dataProvider.


  • First, we are checking the Run mode of the TestCase.

  • Then we are opening the LoginPage.

  • In next step we are trying to login into application using doLogin() function. In doLogin() function we are getting the data from HashTable using get() function, this HashTable is getting the Data from getData() function. The key is username and Password.

  • This Test will iterate over the data i.e If we have 3 rows in our Test Data for username and password then this Test case will run three times.


We are uploading the complete Framework code here in a .doc file. You can down it and run it on your local machine. We have already uploaded the Project structure above. Download the code arranges it as per defined project structure and execute.


Download the Complete Project code form here. (Selenium POM Framework)


You can get the code from here and try it on your local machine. This is complete Framework you can use it to Automate your Project. If you are facing any issue then Please write us on our support email.

0 Comment

Leave a Reply