Introduction

Cucumber is an Open source automation tool which supports Behavioral Driven Development (BDD) approach. BDD is a commonly used approach to implement acceptance test scenarios while development is in-progress. BDD is used to create test scripts from both developer’s and the User’s perspective.

What is BDD?

BDD is a way for the software team to  work that closes the gap between business people and technical people by

  1.  Encourage collaboration across roles to build shared understanding of the problem to be solved.
  2.  Working in rapid, small iteration to increase the feedback and the flow of value.
  3.  Producing the system documentation that is automatically checked again the system behavior.
BDD Activity

Three Iterative process:

  1. Upcoming changes to the System- a User Story
  2. Document those examples in way that can be automated and check for agreement.
  3. Implement the behavior described by each documented example, starting with an automated test to guide the development of the code.

Call these as Discovery, Formulation, & Automation

  1. Discovery – What it could do
  2. Formulation – What it should do
  3. Automation – What it actually does
What is Cucumber?

Cucumber reads the executable specification written in Plain text and automate the functional validation that the software does what those specification say. The specification consists of various scenarios or examples.

Feature: Login Validation

Scenario: Valid Username and Valid password

Given the Valid Username and Valid Password

When I click the Login Button

Then I should see Schedule Page

Each Scenario are the steps for cucumber to walk through. Cucumber should verifies that the software conforms with the specification and generates a reports indicating success and failure for each scenario.

To understand the scenarios by the cucumber, they must follow some basic syntax rules called Gherkin.

What is Gherkin?

It is a set of grammar rules that makes plain text structured enough for cucumber to understand. The above scenario is written in Gherkin.

Gherkin documents is stored in .feature text files and are source control alongside the software.

Gherkin serves multiple Purpose:
  1. Unambiguous executable specifications
  2. Automated testing using cucumber
  3. Document how the system actually behaves
Cucumber with Selenium Architecture

In Cucumber, first we need to defined the feature file, step definition, and Runner class.

  1. Feature file – define the basic steps like Scenario, Given, When and Then keywords, etc.
  2. Step definition Class – define the respective methods (implementation) for the steps which we have defined in the feature file.
  3. Test Runner Class – By running the test runner class, the respective feature file and step definition class files will be run and results will be displayed.
Steps to run the program using Cucumber in Selenium
  1. Create a Java Project File > New > Java Project > CucumberStarts
  2. Right-click on the Java project “CucumberStarts” Create a Package named “com.w2a.cucumber”
  3. Right-click on the package com.w2a.cucumber and create a new file.
  4. Mention the feature and scenario in the test.feature.
  5. Import the following jars by downloading from https://mvnrepository.com/.
    1. cucumber-core-4.8.0
    1. cucumber-html-0.2.3
    1. cucumber-java-4.8.0
    1. cucumber-junit-4.8.0
    1. cucumber-jvm-deps-1.0.6
    1. gherkin-8.1.1
    1. hamcrest-core-1.3
    1. jchronic-0.2.6
    1. junit-4.12
  6. Right click on the package com.w2a.cucumber and create a new java file Tester.java.
  7. Mention the steps to be tested in Tester.java
  8. Right-click on the package com.w2a.cucumber and create a runner file testRunner.java.
  9. Mention the codes in testRunner.java
  10. Right-click on the Java project “CucumberStarts” and click on Run As > Junit Test
  11. Find the Output.
Login_Test.feature

Feature: Login to iCoremr Application
Scenario: Successful Login with valid credentials
Given User is on Home Page
When User navigates to Login page
Then Message displayed Login Successfully
And Displays the name of the Landing Page.

Test_Steps.java

package stepDefinition;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;

import cucumber.api.java.en.And;

import cucumber.api.java.en.Given;

import cucumber.api.java.en.Then;

import cucumber.api.java.en.When;

public class Test_Steps {

public static WebDriver driver;

@Given(“^User is on Home Page$”)

public void User_is_on_Home_Page() {

System.setProperty(“webdriver.chrome.driver”,

“E:\\Balaji\\Balaji\\Testing Software\\Software\\chromedriver_win32\\chromedriver.exe”);

driver = new ChromeDriver();

driver.manage().window().maximize();

driver.get(“https://test3.icoreemr.com”);

}

@When(“^User navigates to Login page$”)

public void User_navigates_to_Login_page() throws InterruptedException {

driver.findElement(By.id(“username”)).clear();

Thread.sleep(3000);

driver.findElement(By.id(“username”)).sendKeys(“XXXXX”);

driver.findElement(By.id(“password”)).clear();

driver.findElement(By.id(“password”)).sendKeys(“XXXXXX”);

driver.findElement(By.id(“login”)).click();

}

@Then(“^Message displayed Login Successfully$”)

public void Message_displayed_Login_Successfully() {

System.out.println(“Logged in Successfully”);

}

@And(“^Displays the name of the Landing Page.$”)

public void LandingPageName_will_be_displayed() {

try {

Thread.sleep(1000);

if (driver.findElement(By.xpath(“//*[@id=\”messages\”]”)).isDisplayed()) {

// System.out.println(“Error”);

String ErrorMsg = driver.findElement(By.xpath(“//*[@id=\”messages\”]”)).getText();

System.out.println(ErrorMsg);

}

} catch (Exception e) {

// System.out.println(“succs”);

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement userName = wait.until(ExpectedConditions.visibilityOfElementLocated(

By.xpath(“/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/ul[1]/li[1]/a[1]/span[1]”)));

System.out.println(userName.getText());

}

driver.close();

}

}

TestRunner.java

package CucumberTest;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)

@Cucumber.Options(

features = “src/Feature”

,glue= {“stepDefinition”}

)

public class TestRunner {

}

Output:
Conclusion

Based on the above sample behaviour driven program, we have learnt how to Install and run Cucumber BDD using Selenium Webdriver. We can see more advanced topics like Parameterization, Data Driven Framework, and Cucumber Jenkins Reporting Plugin in next post.