Create a Mini Data-Driven Test Suite Using Selenium 3.0

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 demo website to book a flight using Selenium Webdriver and TestNG. Also, we’ve now upgraded this tutorial to use the Selenium 3.0 libraries and the Gecko driver for Firefox.

In addition to the data-driven automation, we’ve dealt with some quite important features of Selenium Webdriver in this post. To name a few of these concepts are as follows.

  • Handling alerts in Selenium 3.0 using a background thread.
  • Simulating Autocomplete feature without using Actions class.
  • Selecting a date from the date picker calendar.
  • Use of TestNG annotation like @DataProvider, @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest, @BeforeMethod, @AfterMethod, and @Test.
  • Waiting for the next page to load and process its elements. 

Truly say, working on this tutorial was a challenging and great learning experience.

Guide to create a mini data-driven test suite in Selenium 3.0

Follow the below steps for writing data-driven tests using the Gecko driver in Selenium 3.0.

Writing Data-Driven Tests in Selenium 3.0

Writing Data-Driven Tests in Selenium 3.0

Understand the use case for writing data-driven tests.

We’ll write data-driven tests to simulate flight booking using a demo website. Here is the complete use case which we’ll automate using Selenium 3.0.

  • Open the demo website to book the flight tickets.
  • Read the user details from the external date source (XLS sheet).
  • Parse details like source, destination, date of departure and arrival, class, and the no. of people traveling.
  • Fill out the details to initiate the flight booking.
  • Simulate the flight booking procedure for each row read from the XLS sheet.
  • After successfully completing all operations, the web page should get closed.

 

Create a Selenium TestNG project in Eclipse.

If you are already familiar with Eclipse and Selenium, then you can simply follow the below steps.

  • Create a Java project in Eclipse and name it as <DataDrivenTestSuite>. Browse it and make sure it contains the <src> folder. If it’s not there, then create the one.

Note – Make sure the correct JDK version is set as the default JRE.

  • Add following two packages under the <src> folder and create empty Java files as mentioned.
    • <com.techbeamers.seleniumtestng.datadriventest>
      • Create the blank <DataDrivenTest.java> file.
    • <com.techbeamers.seleniumtestng.datamanager>
      • Create the blank <DataManager.java> file.

Note – You can check out the source code of these files towards the end of this post.

  • Create a <Resources> folder inside your project. We’ll use it later to store the data file for flight booking.
  • Now, add the following libraries as external Jar files.
    • Selenium 3.0 standalone jar
    • poi-3.14-20160307.jar
    • poi-ooxml-3.14-20160307.jar
    • poi-ooxml-schemas-3.14-20160307
    • xmlbeans-2.6.0.jar.
  • Also, right click on your project, then go to <Build Path >> Add Libraries> and select TestNG. In Selenium 3.0 projects, you need to explicitly add the TestNG reference.
  • You can create a <testng.xml> file again by right clicking your project and using the <TestNG >> Convert to TestNG> option.

Note – TestNG option will only be available if you’ve installed the TestNG plugin in Eclipse.

So these were a few basic steps to get the skeleton ready for writing the data-driven tests.

If you are a newbie and want a detailed overview of creating a simple Webdriver project in Eclipse, then please refer these six steps to learn it quickly.

Also, if you are interested to know the detailed steps for creating a TestNG project, then have a look at here.

 

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.

Writing Data-Driven Tests - Prepare Data Source

Writing Data-Driven Tests – Prepare Data Source.

You need to get the data values shown in the image and put them in an excel file. Then, save the excel sheet as <test_input.xlsx> on your system. And finally, move it to the <Resources> folder inside your project.

 

Add code to enable Selenium 3.0 support.

You’ll need to add the following lines of code to enable Selenium 3.0 support. Make sure to set the path of the Gecko driver for Firefox.

 

How to handle alerts in Selenium 3.0 on a background thread?

You might be wondering why should we be bothered about the alert. It is because the demo website which we are using keeps on generating authentication alerts. And we need to suppress them all to process the flight bookings placidly.

First of all, you’ll need to write a simple method to purge any alert that appears on the web page. So here is the code for the <purgeAllAlerts()> method.

Now, here is the tricky part, we want to spawn a worker thread to silently purge any alert that comes out. Please check out the code below to see the thread which calls the <purgeAllAlerts()> method at specified intervals.

The <startMonitor()> method creates a background thread to dismiss the authentication alerts. And the thread depends on the state of a variable named as <keepAlive> to decide its lifetime. Above code also have a <stopMonitor()> method which sets the <keepAlive> to false and stops the thread.

 

What is Autocomplete and how to handle it in Selenium 3.0?

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 are using a demo travel portal to automate the flight booking procedure using Selenium Webdriver.

Note – Selenium 3.0 has a known issue with the Actions class. Its methods (like moveToElement()) fails to work in Selenium 3.0. So we’ve used an alternative method to support autocomplete feature.

The Old method which doesn’t work in Selenium 3.0.

The above method is still applicable for users that are using Selenium 2.0 for automation testing.

The Newer method which works in both Selenium 2.0 and Selenium 3.0.

 

How to select the dates from the calendar?

In this tutorial, we are also covering how to use date pickers present on the WebPages. The date picker is just like a table with some set of rows and columns. To select a date, you have to choose the desired month and year from their drop-downs. And for the day, you’ll first navigate to the cell displaying the target day and then click to select it.

Writing Data-Driven Tests - Sample Date Picker Calendar

Writing Data-Driven Tests – Sample Date Picker Calendar.

Here is the piece of code which selects the day of departure and arrival from the (date picker) calendar control.

 

How to wait for the next page to load?

When the user fills the flight details and clicks to submit, then he has to wait for the next page to load for viewing the flight options. We need this code to verify the various flight options available on the next screen.

Below is the code which demonstrates one of the best ways to address this case.

 

Configurable waits.

You can set the Wait values as per your convenience. The implicit wait is the max response time for any Webdriver command and the purge interval is the delay in executing the thread.

 

Full source code of the project.

In this flight booking sample, first of all, we’ll fill the source and destination fields. Both of these are Autocomplete-enabled. So as we enter a partial text, a list will emerge suggesting options. We’ll iterate through the list and choose the desired option.

Probably now you want to see the code of Java class that we created for flight booking. Please copy and paste the below code inside the <DataDrivenTest.Java> file left as blank in the beginning.

Sample Code of <DataDrivenTest.java>.

 

Sample Code for <DataManager.java>.

Here is the sample code for the <DataManager.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.

Copy and paste the below code inside the <DataManager.Java> file of your project.

 

Summary – Mini Data-Driven Test Suite in Selenium 3.0.

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.

5 Comments

  1. Sebastian Reply
    • Meenakshi Agarwal Reply
  2. Anandyita Reply
    • Meenakshi Agarwal Reply
  3. Florence Reply

Leave a Reply

*