Java, NoSQL, SQL, REST API and other scary words, QA (eng)

Cucumber – bridge or another one abyss between BA and QA

Cucumber Introduction:

Cucumber is a tool based on Behavior Driven Development (BDD) framework which is used to write acceptance tests for web application. It allows automation of functional validation in easily readable and understandable format (like plain English) to Business Analysts, Developers, Testers, etc.

Cucumber feature files can serve as a good document for all. There are many other tools like JBehave which also support BDD framework. Initially Cucumber was implemented in Ruby and then extended to Java framework. Both the tools support native JUnit.

Behavior Driven Development is extension of Test Driven Development and it is used to test the system rather than testing the particular piece of code. We will discuss more about the BDD and style of writing BDD tests.

Cucumber could be used along with Selenium and Java and this is more than enough to make me happy.

Cucumber Basics:

In order to understand cucumber we need to know all the features of cucumber and its usage.

#1) Feature Files:

Feature files are essential part of cucumber which is used to write test automation steps or acceptance tests. This can be used as live document. The steps are the application specification. All the feature files ends with .feature extension.

Sample feature file:

Feature: Login Functionality Feature
In order to ensure Login Functionality works,
I want to run the cucumber test to verify it is working
Scenario: Login Functionality
Given user navigates to SOFTWARETETINGHELP.COM
When user logs in using Username as “USER” and Password “PASSWORD”
Then login should be successful
Scenario: Login Functionality
Given user navigates to SOFTWARETETINGHELP.COM
When user logs in using Username as “USER1” and Password “PASSWORD1”
Then error message should be thrown

#2) Feature:

This gives information about the high level business functionality (Refer to previous example) and the purpose of Application under test. Everybody should be able to understand the intent of feature file by reading the first Feature step. This part is basically kept brief.

#3) Scenario:

Basically a scenario represents a particular functionality which is under test. By seeing the scenario user should be able to understand the intent behind the scenario and what the test is all about. Each scenario should follow given, when and then format, this is pretty structured thing and should be common for SA and BA skilled team members.

  1. Given: As mentioned above, given specifies the pre-conditions. It is basically a known state.
  2. When: This is used when some action is to be performed. As in above example we have seen when the user tries to login using username and password, it becomes an action.
  3. Then: The expected outcome or result should be placed here. For Instance: verify the login is successful, successful page navigation.
  4. Background: Whenever any step is required to perform in each scenario then those steps needs to be placed in Background. For Instance: If user needs to clear database before each scenario then those steps can be put in background.
  5. And: And is used to combine two or more same type of action.

Example:

Feature: Login Functionality Feature
Scenario: Login Functionality
Given user navigates to SOFTWARETETINGHELP.COM
When user logs in using Username as “USER”
And password as “password”
Then login should be successful
And Home page should be displayed
Example of Background:
Background:
Given user logged in as databases administrator
And all the junk values are cleared

#4) Scenario Outline:

Scenario outlines are used when same test has to be performed with different data set. Let’s take the same example. We have to test login functionality with multiple different set of username and password.

Feature: Login Functionality Feature
In order to ensure Login Functionality works,
I want to run the cucumber test to verify it is working
Scenario Outline: Login Functionality
Given user navigates to SOFTWARETESTINGHELP.COM
When user logs in using Username as <username> and Password <password>
Then login should be successful
Examples:
|username        |password      |
|Tom             |password1     |
|Harry           |password2     |
|Jerry           |password3     |

Note:

  1. As shown in above example column names are passed as parameter to When statement.
  2. In place of Scenario, you have to use Scenario Outline.
  3. Examples are used to pass different arguments in tabular format. Vertical pipes are used to separate two different columns. Example can contain many different columns.

#5) Tags:

Cucumber by default runs all scenarios in all the feature files. In real time projects there could be hundreds of feature file which are not required to run at all times.

For instance: Feature files related to smoke test need not run all the time. So if you mention a tag as smokeTest in each feature file which is related to smoke test and run cucumber test with @SmokeTest tag . Cucumber will run only those feature files specific to given tags. Please follow the below example. You can specify multiple tags in one feature file. Similarly when you want your script to run for Login test use @LoginTest tag.

Example of use of single tags:

@SmokeTest @LoginTest
Feature: Login Functionality Feature
In order to ensure Login Functionality works,
I want to run the cucumber test to verify it is working
Scenario Outline: Login Functionality

Similarly you can specify tags to run specific scenario in a feature file. Please check below example to run specific scenario. Just use @positiveScenario or @negaviveScenario to do that.

#6) Junit Runner:

To run the specific feature file cucumber uses standard Junit Runner and specify tags in @Cucumber. Options. Multiple tags can be given by using comma separate. Here you can specify the path of the report and type of report you want to generate.

Example of Junit Runner:

import cucumber.api.junit.Cucumber;</pre>
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@Cucumber.Options(format={"SimpleHtmlReport:report/smokeTest.html"},tags={"@smokeTest",”@LoginTest”})
Public class JUnitRunner {
}

#7) Cucumber Report:

Cucumber generates its own html format.

Cucumber Project Setup:

Java project requires a class which contains those given, when and then statements. Cucumber uses its annotations and all the steps are embedded in those annotations (given, when, then).Each phrase starts with “^” so that cucumber understands the start of the step. Similarly each step ends with “$”. User can use regular expressions to pass different test data. Regular expressions take data from feature steps and passes to step definitions. The order of parameters depends how they are passed from feature file.

Example:

Below example is to illustrate how feature files can be implemented.

In this example we have not used any selenium API. This is to just show how cucumber works as standalone framework. Please follow next tutorial for selenium integration with cucumber.

public class LoginTest {

@Given("^user navigates to SOFTWARETETINGHELP.COM$")
public void navigatePage() {
system.out.println(“Cucumber executed Given statement”);
}

@When("^user logs in using Username as \"(.*)\" and Password \"(.*)\"$")
public void login(String usename,String password) {
system.out.println(“Username is:”+ usename);
system.out.println(“Password is:”+ password);
}

@When("^click the Submit button$")
public void clickTheSubmitButton() {
system.out.println(“Executing When statement”)
}

@Then("^Home page should be displayed$")
public void validatePage() {
system.out.println(“Executing Then statement”)
}

@Then("^login should be successful$")
public void validateLoginSuccess() {
system.out.println(“Executing 2<sup>nd</sup> Then statement”)
}

}

or  real implementation example:

1.png

Results output example (JUnit):

2.png

If we want to use default reporting then navigate the path mentioned in Junit Runner. In this case we have given path as reports->test-reports->index.html.

Open this report in Internet Explorer or in Firefox to verify the result. Below is the sample of the report:

3.png

When you execute cucumber runner class, cucumber will start reading feature file steps. For example, when you execute @smokeTest, cucumber will read Feature step and Given statement of scenario. As soon as cucumber finds Given statement, same Given statement will be searched in your java files. If same step is found in java file then cucumber executes the function specified for the same step otherwise cucumber will skip the step.

Conclusion:

In this tutorial, we have covered features of cucumber tool and its usage in real time scenario.

Cucumber is a most favorite tool for many projects as it is easy to understand, readable and contains business functionality.

This article will cover how to setup a cucumber in Java + Selenium.

Advertisements
Standard

2 thoughts on “Cucumber – bridge or another one abyss between BA and QA

    • I would strongly recommend to write use cases (and Cucumber format works for me). There are couple things I would like to add:
      1. If you use Cucumber only, you will need one place to store all stories somewhere – I would recommend UC diagram for that
      2. There is no sense to use detailed description for developers, otherwise Cucumber will be just a duplicated data for test logging

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s