How to Write an efficient Cucumber Test?

cucumber test

Cucumber is integrated with the testing framework majorly used to introduce behavior driven framework. It allows testers to do the testing according to the behavior of the application.

There are different feature files being written which bridge the gap between different stakeholders of the project. Business Analyst, Customer, and Technical people should come on the same page so that the development of the product can meet the customer’s expectations and bug tracking can be faster as compared to conventional testing.
Now, you know how it is to write feature files. Let’s take an example of a feature file which tests an application login flow.

Feature: Login to the test application

It checks whether the login to the application is performed correctly or not.
Scenario: Successful Login to Test Application
Given User is at the login page
When User enters username as “nancy” and password as “pwd”
And User clicks on login button
Then User is able to successfully login to the Test Application.
Now, writing this feature file in a haphazard manner is very easy but have you ever imagined that there are some effective ways in which you can write your Cucumber Tests which we will be looking at in the below section. Let’s have a look at those:
1. You should use background keyword very wisely. If you want some common steps to run before each scenario of a feature file then you can put those steps in a background scenario. Those steps would run before each scenario in a feature file.

Feature: Test Background Feature

Description: This feature is to test the Background keyword
Given User is at the login page
When User enters username as “nancy” and password as “pwd”
And User clicks on login button
Then User is able to successfully login to the Test Application.

Scenario: Search a product and add the first product to the User basket

Given User type Acer Laptop in search box
When Add the first search result laptop to the basket
Then User basket should display the added item

Know More: Selenium Automation Testing using Cucumber Tool

Scenario: Add selected product and add the same to the User basket

Given User select Lenovo Laptop
When Select on the laptop to add it to the basket
Then User basket should display the added item
2. Every scenario should be independent of any other scenario. Any scenario should not depend on any other scenario for execution. Every scenario should be isolated and in this way, the execution of scenarios should not be dependent on any other scenario.

3. You should use a Scenario Outline when you want one scenario to be executed on a number of times. Just assume the case when you want to test the login scenario for different combinations of username and password. So, in that case, you can specify the combination of username and password for which the scenario will run n number of times.

Feature: Login to the test application using Scenario Outline

Description: It checks whether the login to the application is performed correctly or not for all set of specified users.
Scenario: Successful Login to Test Application using Scenario Outline
Given User is at the login page
When User enters Username as <username> and Password as <password>
And User clicks on login button
Then User is able to successfully login to the Test Application.
Example –
|username|password|
|user1|pwd1|
|user2|pwd2|
|user3|pwd3|
This scenario will run for three sets of users and password. It will, in turn, check the login flow for 3 usernames and password.

Know More: Rest API Testing using Cucumber Tools

4. You should make the scenarios more declarative and not imperative. The declarative styles make the scenarios more readable. It improves the readability of the scenario. Let’s take an example which is written in an imperative way and a scenario which is written in a declarative way. You would feel the difference in the readability of the scenarios.
Example: Imperative Scenario

Feature: Login to the test application

It checks whether the login to the application is performed correctly or not.
Scenario: Successful Login to Test Application
Given User is at the login page
When User enters username as “nancy” and password as “pwd”
And User clicks on login button
Then User is able to successfully login to the Test Application.
Example: Declarative

Feature: Login to the test application

It checks whether the login to the application is performed correctly or not.
Scenario: Successful Login to Test Application
Given User is at the login page
When User login In
Then User should be able to successfully login.

  1. Avoid doing hard coding I step definition files. Always try to parameterize the scenarios by passing username and password.

Feature: Login to the test application using Scenario Outline

Description: It checks whether the login to the application is performed correctly or not for all set of specified users.
Scenario: Successful Login to Test Application using Scenario Outline
Given User is at the login page
When User enters Username as <username> and Password as <password>
And User clicks on login button
Then User is able to successfully login to the Test Application.
Example –
|username|password|
|user1|pwd1|
|user2|pwd2|
|user3|pwd3|
You should write step definition files in which you take parameters from feature file.
@When(“^User enters Username as \”([^\”]*)\” and Password as \”([^\”]*)\”$”)
public void I_enter_Username_as_and_Password_as(String arg1, String arg2) {
driver.findElement(By.id(“email”)).sendKeys(user1);
driver.findElement(By.id(“password”)).sendKeys(pwd1);
driver.findElement(By.id(“login”)).click();
}
6. When you want to conjunct too many steps. You should do it by “and” keyword. Make sure that you do it into two steps and then use “and” keyword. One action per step should be used and it makes the steps more modular. It increases the scenario more readable.

Feature: Login to the test application
It checks whether the login to the application is performed correctly or not.
Scenario: Successful Login to Test Application
Given User is at the login page
When User login In with username and password
And User clicks on login button
Then User should be able to successfully login.

Conclusion

You should follow some steps which will make the cucumber tests more efficient. These steps will make the tests more readable. All the best!