Writing Data Driven Tests Using Selenium Webdriver

In this blog post, we’ll present a live example of writing data driven tests using Selenium Webdriver. With the data-driven testing approach, we can create a scalable test framework and run a large no. of test cases. In this tutorial, we’ll automate a website to book a flight using Selenium Webdriver and TestNG. In addition to the data-driven automation, we’ve dealt with some other important concepts or features of Selenium Webdriver in this post like Autocomplete and selecting a date from the date picker calendar. Truly say, it was a challenging and great learning experience.

Probably you’ve seen that most travel websites are using the Autocomplete text boxes for user input. It facilitates users in choosing an option from a pre-populated list of values based on the text that user enters. It mainly concentrates on providing suggestions to users while typing into the field. So we’ll be writing data driven tests to automate this scenario.

Writing Data Driven Tests - Sample Autocomplete Textbox

Writing Data Driven Tests – Sample Autocomplete Textbox.

Let’s now see an example. When we enter any text in the textbox supporting Autocomplete, it displays a no. of options containing the related text. We can then select a value from that pre-populated list. You can see that we’ll use a demo travel portal in this example to book a flight using Selenium Webdriver.

Writing Data Driven Tests Using Selenium Webdriver.

Rather discussing too much, let’s come straight to view the sample code of flight booking using Selenium Webdriver and TestNG. In this sample, first of all, we’ll fill the source and destination fields. Both of these are Autocomplete-enabled. So we’ll enter a partial text and then pick the correct option. Since we have a list as an option, so it can hold multiple values. We’ll iterate through the list and choose the desired option.

Now, let’s go through the use case we’ll take up in this tutorial.

1- The Use Case for Data-Driven Test Automation.

  • Open the Webpage to book the flight tickets.
  • Read the data from the date source.
  • Read details like source, destination, date of departure and arrival, class, and the no. of people travelling.
  • Enter the details to initiate the flight booking.
  • After successfully completing all operations, the web page should get closed.

For your note, we assume that you’ve basic knowledge of creating a simple Webdriver project in Eclipse. If you are a newbie, then please refer these six steps to learn it quickly.

Also, if you want to test run the below data-driven example, then create a Java project in Eclipse and add the latest Selenium Jar as an external library. Consequently, follow the below instructions.

2- Prepare an XLS sheet for data-driven testing.

Since we’ll be writing data driven tests in the next sections, so it’s mandatory to define a data source. We’ll use an XLS sheet to hold the data values. See an example below.

You need to save the below excel file as <test_input.xlsx> on your system. Then, create a <Resource> folder in the project. And add the excel into the <Resource> folder.

Writing Data Driven Tests - Prepare Data Source

Writing Data Driven Tests – Prepare Data Source.

Note: While creating the TestNG classes, we’ll use the @BeforeMethod, @AfterMethod, and @DataProvider annotations.

3- Handling Autocomplete textboxes While Booking a Flight.

In this part of the post, we’ll create a Webdriver class to fill the details for booking a flight. Since all the text fields on the demo site are Autocomplete, so we’ll handle them accordingly. Probably you want to see the code of Java class that we created for flight booking. Please save this file as <BookFlight.java> and add to your project. So that, you can run the same test in your Eclipse IDE.

Sample Code of <BookFlight.java>.

 

4- Handle Date Pickers Present on the WebPages.

In this part, we’ll show how to use date pickers present on WebPages. The date picker is just like a table with some set of rows and columns. To select a date, we have to choose the desired month and year from their drop-downs. And for the day, we have to navigate to the cell where our desired date is present and click on it.

Writing Data Driven Tests - Sample Date Picker Calendar

Writing Data Driven Tests – Sample Date Picker Calendar.

Here is a sample code for the <ReadFlightData.java> file. It’ll read the flight booking details (test data) from the data source. And it’ll then pick a date available in the test data.

Also, add this file to your project.

Sample Code for <ReadFlightData.java>.

 

Final Thought.

It was a comprehensive tutorial on writing data driven tests using Selenium Webdriver and TestNG.

And hopefully, you all would like it and able to use the concept in your projects.

 

All the Best,

TechBeamers.

Leave a Reply


*