Test Scenario using Cucumber-JUnit

In this tutorial, we will write our first test scenario using cucumber tool. The testing framework used with cucumber in this tutorial is Junit.

After creating a project in eclipse add below jar files into the project:

  • cucumber-junit
  • cucumber-reporting
  • junit

You can download these jar files from Maven website. (https://mvnrepository.com/)

If you have created a Maven project add below dependencies:

Create a feature file:

Create a Java project or Maven java project in eclipse.

Create a folder with the name “feature”.

Create a feature file say with name Login.feature. Right-click project name >> new folder with name feature >> new feature file with name Login.feature.

What is a feature file?

A “feature” file is a plain text file with the .feature extension. We write test cases in the feature file using Gherkin language.

Feature file consists of the following components –

Feature: Feature keyword describes what we are going to test in the test scripts.

Scenario: Scenario keyword describes a particular test scenario to test in a feature.

Scenario Outline: A scenario can be executed for multiple sets of data using the scenario outline. The data is provided by a tabular structure separated by (||).

Given: It specifies the context of the text to be executed.

When: “When” specifies the test action that has to be performed.

Then: The expected outcome of the test can be represented by “Then”.

Let us see an example of a feature file and then we will understand each statement:

In the above code, the test case is written in plain English language. The code starts with Feature keyword which tells about what feature this file consists of and the next statement has a description of the feature.

Next, is the Scenario keyword which is the first test scenario. A feature file has multiple scenarios.

After that, we write the steps to execute in the scenario using tags like Given, When, And, Then, But, etc.

When you will create the feature files it will show you a warning with each step stating – Step <step> does not have a matching glue code. See the below screenshot.

To remove these error let us first understand –

How does Cucumber tool work?

– Cucumber first reads the feature files and identify all the steps in that feature file.
– Then Cucumber maps these steps with the step-definition (using TestRunner class).
– Then Cucumber executes these steps of step definition on the Application.

Create a Test Runner:

Next, we will write a code which will execute this code written above.

  • Create a package with name testRunner.
  • Create a class with the name TestRunner.java.
    • In TestRunner.java class add an annotation @RunWith(Cucumber.class) where @RunWith is from org.junit.runner.RunWith class and Cucumber.class is from cucumber.api.junit.Cucumber class.
    • Add @CucumberOptions annotation – this informs test engine with some properties
      • informs the test engine where are the feature files.
      • glue – It is to bind the feature files with the step definition

See the below code:

Create a Step Definition class:

Next step is to create a Step definition class in steps package. Right-click steps package >> new >> class >> LoginSteps.java.

Right click on TestRunner.java class >> Run As >> JUnit Test cases. It will execute the code and will show you some message in the console. like below:

Copy all the steps from above and paste them into LoginSteps.java class and edit each step. You can also change the name of the methods. See the below code:

Wherever there is (.*) symbol it is a special character and is used to pass a parameter in a test step. Like in above code email Id and password are passed as an argument in the test step.

We will learn about these special characters in detail in upcoming tutorials.

Run the code again and you will get below output:

PS: For any questions, queries or feedback. Feel free to write us at saurabh@qatechhub.com or support@qatechhub.com

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: