Practical examples of Cucumber data driven testing

This article is an extension of our previous one on how to pass a single string parameter in Cucumber. In the previous article, we were more focused on how to create a very basic cucumber data driven test script. In this article, we will go a step further and show you various examples of the data driven setup.

Why is this article useful?

This article is important because it will show you different practical usages of basic cucumber data driven testing. The examples that we will provide here are useful as this is something that you will actually use in your frameworks and projects. So, we suggest that you use this article in the following way –

  • Go through each of the different examples and understand how it works
  • Try to identify the examples which you think are suitable for your automation project
  • Then follow the usual implementation process – add the examples to the feature files, run test runner class to get method definition, add methods to step definition classes and work out the selenium code

Let us now start with the various examples one by one.

Note: This article is a part of our Cucumber Tutorial series. Head over to our Selenium Cucumber Tutorial main page for the step by step cucumber setup guide

Passing more than one parameters in a cucumber step

One of the most common examples of this type of scenario is the login functionality. For your test application, you can write the login feature in ‘expanded form’ in this way:

Scenario: Login to application
Given I open my application
When I enter user id as "admin"
And I enter password as "pass1234"
And I click on login button
Then application homepage is displayed

There are 2 issues with this type of approach –

  • Number of steps in cucumber feature file increases a lot
  • You have to write lot of step definition and selenium methods to cover each cucumber step

A better approach to tackle this type of situation is to write cucumber steps in such a way that multiple logical actions can be clubbed together in a single step. An example for this is given below –

Scenario: Login to application
Given I open my application
And I login with credentials "admin" and "pass1234"

Here, the second step would cover the last four steps given in the first scenario. And this is also the example where you pass more than one data parameter in a single cucumber step.

Add this scenario in a feature file and then run the test runner class to get the missing step definition method. You will see that the method definition for the second step would be displayed like this –

You will notice that Cucumber has added 2 regular expressions and 2 method arguments to identify the string parameters. Provide meaningful name to this method and the arguments and then use it in your test script like this –

@Given("^I login with credentials \"([^\"]*)\" and \"([^\"]*)\"$")
public void i_login_with_credentials(String username, String password) {
    //Add code to enter username, password and click on Login button
    //Add code to verify that login is successful
}

Note: You can extend this same approach to add 3 or more parameters in a single cucumber step. Cucumber will just keep on adding more method arguments in the step definition method.

With string parameters covered, let us now have a look at how you can pass numbers in cucumber feature files.

How to pass integer values in Cucumber step

Passing integer values using Cucumber steps is pretty straightforward. While passing the integer values, just make sure that you don’t write it within double quotes. If you write it in double quotes, then Cucumber by default would treat it as a string. Let’s check the below example to see how it works –

You can clean up this method and add it to your step definition class like this –

@Given("^I want to verify that (\\d+) plus (\\d+) equals (\\d+)$")
public void i_want_to_verify_sum_of_numbers(int firstNum, int secondNum, int sum) {
    if((firstNum + secondNum) == sum) {
    	System.out.println("Sum matches");
    } else {
    	System.out.println("Sum doesn't match");
    }
}

Passing double values in Cucumber step

If you want to pass double values in a Cucumber test, then you will see that it is easy, but not as straightforward as passing integer values. Let us use the same example that we used for integers to see how it works.

The Cucumber scenario is given below. It’s same as the previous example, only difference being that we have replaced integer values with double values.

Scenario: Perform addition of two numbers
Given I want to verify that 2.2 plus 3.3 equals 5.5

When you run this feature, you will see that missing step definition method comes up like this –

In the above image, you can notice that when it comes to regular expression, by default, cucumber treats a double number as two integer numbers with a dot in between. Also, it displays six method arguments of type integer instead of three arguments of type double.

To fix this, you will need to manually update the method definition. To do this –

  • replace the three occurrences of regular expression (\\d+)\\.(\\d+) with (\\d+.\\d+), so that it represents an integer
  • remove the six integer arguments and add three double arguments in its place

The final method definition will look like this –

@Given("^I want to verify that (\\d+.\\d+) plus (\\d+.\\d+) equals (\\d+.\\d+)$")
public void i_want_to_verify_sum_of_numbers(double firstNum, double secondNum, double sum) {
	if((firstNum + secondNum) == sum) {
    	System.out.println("Sum matches");
    } else {
    	System.out.println("Sum doesn't match");
    }
}

Pass string parameters in cucumber step without double quotes

It’s not necessary that you always have to write string parameters in double quotes. You can pass string parameters without double quotes as well, though it impacts the readability of the cucumber scenario a little bit.

Also, cucumber automatically interprets a string in double quotes as a parameter. But when you provide the string parameter without double quotes, you will have to yourself add the regular expression and method argument in the step definition method. Let’s see how this works –

Let’s take the same application login example that we covered in the beginning. But in this case, we will pass the user name and password without the double quotes. The scenario will now look like this –

Scenario: Login to application
Given I open my application
And I login with credentials admin and pass1234

When you run the test runner class to get the step definition method, then it will be displayed like this –

Here, you can see that cucumber didn’t convert admin and pass1234 into regular expressions, because we didn’t add double quotes around it. Also, you will notice that cucumber has considered 1234 from pass1234 as integer, and added integer based regular expression and argument to the method.

Now, we need to manually change the method definition to pass admin and pass1234 as string arguments. You can do this by following the below two steps –

  • Replace admin and pass(\\d+) with the generic regular expression (.*)
  • Remove the int method argument and add two string arguments

The modified step definition will look like this –

@Given("^I login with credentials (.*) and (.*)$")
public void i_login_with(String username, String password) {
    //Add code to login to application
}

Passing a set of limited values instead of (.*)

The regular expression (.*) is a good option when you want to cover free texts, but what if you have only few limited values for the data parameter? Let’s consider the below example where we want to parametrize the browser name –

Scenario: Application Login
When I open application in Chrome browser

Using (.*) as a regular expression in step definition method works perfectly in this scenario, but there is better way where you can provide only a limited set of browser names as well. And this method provides much better readability than by using (.*). To use this method, you need to modify your step definition method like this –

@When("^I open application in (Chrome|Firefox|Safari) browser$")
public void i_open_application_in_browser(String browser) {
    if(browser.equalsIgnoreCase("chrome")) {
    	//code to launch Chrome
    } else if(browser.equalsIgnoreCase("firefox")) {
    	//code to launch Firefox
    } else if(browser.equalsIgnoreCase("safari")) {
    	//code to launch Safari
    }
}

In the above code snippet, you can see that we have used the regular expression (Chrome|Firefox|Safari) instead of (.*). This is somewhat like an OR condition, where you specify multiple values using pipe condition.

In this case, the cucumber feature file step will identify the method only when you use any of these browser names. But if you use any other parameter, say Internet Explorer, then Cucumber would not recognize this step and you will get missing step definition method message when you run the code.

So, use this method for better readability when you have a limited set of values to cover.

With this, we now complete this article on different examples of basic cucumber data driven testing approach. If you have any doubts with any of the concepts above, then please feel free to reach out to us using the comments section.

Next Article: Working with Cucumber data table