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.

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.

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.

Java

1

2

3

4

5

6

7

8

9

10

11

// Method to remove alerts from the web page

publicvoidpurgeAllAlerts(){

try{

Thread.sleep(purgeInterval);

Alert alert=driver.switchTo().alert();

if(alert!=null)

alert.dismiss();

}catch(Exception ex){

// Intentionally left blank.

}

}

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.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

// Method to start background thread for removing alerts

publicvoidstartMonitor(){

System.out.println("enter into AlertMonitor().");

keepAlive=true;

Threadt=newThread(newRunnable(){

publicvoidrun(){

for(;;){

purgeAllAlerts();

if(!keepAlive)

break;

}

System.out.println("exit from AlertMonitor() thread.");

}

});

t.start();

System.out.println("exit from AlertMonitor().");

}

// Method to stop alert monitor thread

publicvoidstopMonitor(){

keepAlive=false;

}

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.

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 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.

Java

1

2

3

4

// Insert data into Flying from text box

driver.findElement(By.id("intlDeptCode")).click();

driver.findElement(By.id("intlDeptCode")).sendKeys(SourcePattern);

driver.findElement(By.id("intlDeptCode")).click();

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.

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.

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.

Hi Sebastian – No, the project isn’t using the log4j. Instead, it seems you are using a beta version of Selenium 3 (Gecko) driver which is causing those messages. Get the latest version of Gecko driver and then try to run the project.