Cucumber Series Tutorial 2- Make your Automation Data Driven by Parametrising Feature File



We as automation engineers always require to design automation test script that runs for multiple datasets. Similarly, in Cucumber also, we can achieve data-driven testing.

There are multiple ways to achieve data-driven testing, we will discuss each approach in detail. In this tutorial, we will discuss how to achieve Data-driven Testing by the Parametrising feature file.

Approach 1: Parametrising Feature File


In the Tutorial-1, we have hardcoded the username and password in the step definition file.
Instead of hardcoding the username and password, we can parametrize them in Step definition file using regular expressions.

Previous method:

@Then("^User enters Username and password$") public void user_enters_Username_and_password() {  driver.findElement(By.xpath("//input[@name='email']")).sendKeys("username");         driver.findElement(By.xpath("//input[@name='password']")).sendKeys("password"); }

Updated method with regular expression:


@Then("^user enters \"(.*)\" and \"(.*)\"$")
public void user_enters_Username_and_password(String username, String password) {
    driver.findElement(By.xpath("//input[@name='email']")).sendKeys(username);
    driver.findElement(By.xpath("//input[@name='password']")).sendKeys(password);
}


Please note that here we have used regular expression in Step statement in @Then annotation. Also, don't remove the ^ and $ symbols in step definition.
Now, we also have to pass the username and password from the feature file as follows:

Feature: Free CRM Login Feature
Scenario: Free CRM Login Test Scenario

Given User is already on Login Page
When Title of login page is Free CRM
Then user enters "user@gmail.com" and "userPassword"
And  User clicks on login button
And user is on home page

Now execute the TestRunner class and you will see, the script is using the username and password from Feature file.
We can verify it by running the script in dry run as follows, please note down the options used in @CucumberOptions annotation.



The console o/p for dry run says everything is fine:



I hope you like this tutorial, please share it and like and comment. Also let me know, if you want me to write a tutorial on some other topic as well. Till then, Happy Learning !!!!

Comments

Popular posts from this blog

Automate Mobile OTP Number with Selenium

Cucumber Series Tutorial 3- Make your Automation Data Driven by Parametrising Feature File with Scenario Outline and Examples keyword.