Scenarios are Executable Specifications. Before the actual development of a software gets started in BDD, there should be some concrete examples which should define what the software is supposed to do. These are Scenarios. As the production code emerges, scenarios become more of documentation and automated tests.

Scenarios are written inside “.feature” files. Let’s create a sample Feature file (FirstApplication.feature) and define some scenarios inside it:

Feature: Testing the FirstApplication

Scenario: FirstApplication opens successfully in Desktop Chrome
  Given user opens the Chrome browser in desktop
  When user enters the application URL and searches
  Then the application gets opened successfully.

Scenario: FirstApplication opens successfully in Mobile Web
  Given user opens the Chrome browser in mobile device
  When user enters the application URL and searches
  Then the application gets opened successfully.

At this moment, if we ask Cucumber to execute this file (either by using mvn test command from command line or by running as Junit from the Runner class), Cucumber will show 2 undefined Scenarios and 4 undefined Steps. It will also suggest some code snippets that we can add to define these steps.

@Given("^user opens the Chrome browser in desktop$")
public void user_opens_Chrome_browser_in_desktop() {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Given("^user opens the Chrome browser in mobile device$")
public void user_opens_Chrome_browser_in_mobile_device() {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}


@When("^user enters the application URL and searches$")
public void user_enters_the_application_URL_and_searches() {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}


@Then("^the application gets opened successfully$")
public void the_application_gets_opened_successfully() {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

You can copy each of these suggested code snippets and paste them inside the Step Definition file. Let’s fill the Step Definition file with some code:

package hellocucumber;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import cucumber.api.java.en.Then;


public class Stepdefs {

@Given("^user opens the Chrome browser in desktop$")
public void user_opens_Chrome_browser_in_desktop() {
    System.out.println(“Chrome Browser opened successfully in Desktop.”);
}

@Given("^user opens the Chrome browser in mobile device$")
public void user_opens_Chrome_browser_in_mobile_device() {
    System.out.println(“Chrome Browser opened successfully in Mobile.”);

}


@When("^user enters the application URL and searches$")
public void user_enters_the_application_URL_and_searches() {
    System.out.println(“Application URL search is successful.”);
}


@Then("^the application gets opened successfully$")
public void the_application_gets_opened_successfully() {
    System.out.println(“Application got opened successfully.”);
}

}

Run Cucumber again (either by using mvn test command from command line or by running as Junit from the Runner class). Your tests should now pass.

CONGRATULATIONS!! You have run your first successful tests using Cucumber.

Variables, Scenario Outline and Examples

As you can see in our FirstApplication.feature feature file, we are using different Given steps in the two scenarios, but the When and Then steps are same. We are basically replicating the When and Then steps in the scenarios. To facilitate reusability, let’s combine the 2 scenarios and create a single Scenario Outline which uses Variables and Examples.

Feature: Testing the FirstApplication

Scenario Outline: FirstApplication opens successfully
  Given user opens the Chrome browser in <device>
  When user enters the application URL and searches
  Then the application gets opened successfully.
Examples:
|device        |
|desktop       |
|mobile device |

To make this run, we also need to update the respective Given code snippet in the Step Definition file with the below code:

@Given("^user opens the Chrome browser in \"([^\"]*)\"$")
public void user_opens_Chrome_browser_in_desktop(String deviceType) {
    System.out.println(“Chrome Browser opened successfully in “+deviceType);
}

Credits:
https://cucumber.io/docs/cucumber/