Cucumber Test Automation Framework

Introduction:

Cucumber is a testing framework that helps to bridge the gap between software developers and business managers. Tests are written in plain language, based on the Behavior Driven Development (BDD) style of Given, When, Then, which anyone can understand. It is defined by a Language called “Gherkin”

Difference between TDD and BDD:

TDD: TDD (Test Driven Development) is an iterative development process. Each iteration starts with a set of tests written for a new piece of functionality.

BDD: BDD (Behavior Driven Development) is a synthesis and refinement of practices stemming from TDD. Like in TDD, in BDD also, we write tests first and then add application code.

The main objective of this automation framework is to provide the test script development environment, which is BDD based.

The major differences that we get to see here are:

  • Tests are written in plain descriptive English type grammar.
  • Tests are explained as the behavior of the application and are more user-focused.
  • Using examples to clarify requirements.

Prerequisites:

  • Install JDK and Eclipse on your machine
  • Install Cucumber in Eclipse.
  • Add J Unit to “build-path” in Eclipse.
  • Add Web Driver jar files to “build-path” in Eclipse.

What is “Gherkin”?

Gherkin is a simple, lightweight, and structured language that uses regular spoken language to describe requirements and scenarios.

Gherkin Keywords:

  • Feature: Each Gherkin file begins with a ”Feature” keyword. Feature defines the logical test functionality you will test in this feature file. e.g. if you are testing a “payment gateway”, your Feature will become “Payment Gateway”.
  • Scenario: Each Feature will contain some tests to test the feature. Each test is called a Scenario and is described using the Scenario keyword.
  • Given: Defines a “precondition” to the test.
  • When: This keyword defines the test activities that will be executed. By test action, we mean the user input action.
  • Then: This keyword defines the outcome of previous steps.
  • And: This keyword is used to add conditions to your steps.
  • But: This keyword is used to add negative type comments.
  • *: This keyword is very special. It defines the whole purpose of having Given When Then and all the other keywords.

Example:

Feature: Login Action Test
Description: This feature will test a Login and Logout functionality

Scenario 1:

Unsuccessful Login with Invalid Credentials

  1. Given User is on Homepage
  2. When User Navigate to Login Page
  3. And User enters Username and Password
  4. But The user credentials are wrong
  5. Then Message displayed Wrong Username & Password

Scenario 2: 

Successful Login with Valid Credentials

  1. Given User is on Homepage
  2. When User Navigate to Login Page
  3. And User enters Username and Password
  4. Then Message displayed Log in Successfully

Login Action Test

Note:

  1. We need to write the “Step definition” in a java class for each and every behavior written in the ‘.feature’ file.
  2. All the web elements are declared in a Class(Objects.java) under “com.cucumber.commonlibs” package.
  3. All the constants are declared in a Class(login.java) under “com.cucumber.stepdefinition” package.
  4. The trigger of execution is declared in a Class(RunCucumberTest.java) under “com.cucumber.stepdefinition” package.

Step Definition:

  • A Step Definition is a small piece of code with a pattern attached to it.
  • In other words, a Step Definition is a java method in a class with an annotation above it. An annotation followed by the pattern is used to link the Step Definition to all the matching Steps.
  • Cucumber finds the “Step Definition” file, with the help of Glue code in Cucumber Options.

Example:

@Then("^Message displayed Login Successfully$")
 public void message_displayed_Login_Successfully() throws Throwable {
 System.out.println("Login Successfully");
 }

Advantages:

  • “Mail notification” enabled, on failure of build.
  • “Scheduled execution” can be performed.
  • Captures the “screen shots” promptly, on failure and embeds with the report.
  • Supports to run the execution, individual/suite wise.
  • OS independent and Browser independent.
  • Easy to understand.
  • Easy to use, highly flexible and above all “Free of Cost”.
  • Provides “micro-reporting”, with the statistics of passed, failed, pending, skipped, undefined and missing steps.

cucumber-test-automation

Power your next-gen software products through our automation testing services. Learn from our experts.

Author

  • Kishore Kumar Naravula

    Kishore Kumar Naravula is a Software Test Engineer with Trigent Software. He has over four years of IT industry experience in testing web-based software applications using both manual and automation testing. He is experienced in identifying test scenarios and designing effective test cases and is well versed with SDLC/Agile and Scrum methodologies. Kishore has strong experience in Functional, Regression, GUI, Compatibility, Cross browser, and usability testing.