Introduction to BDD with Cucumber:
Cucumber is a framework for writing and executing high level descriptions of your software's functionality. Call these tests, examples, specifications, whatever... it doesn't matter too much. What I'm talking about has traditionally been called functional, integration, and/or system tests. In XP terms this includes tests called Story Tests, Customer Tests, and/or Acceptance Tests. 

One of Cucumber's most compelling features is that it provides the ability to write these descriptions using plain text in your native language. Cucumber's language, Gherkin, is usable in a growing variety of human languages, including LOLZ. The advantage of this is that these feature descriptions can be written and/or understood by non-technical people involved in the project. 

One important thing to keep in mind is that Cucumber is NOT a replacement for TestNG, RSpec, test/unit, etc. It is not a low level testing/specification framework.
Cucumber plays a central role in a development approach called Behaviour Driven Development (BDD). 

A Bit about BDD:
Dan North describes BDD as “writing software that matters” in The RSpec Book and outlines 3 principles:
  1. Enough is enough: do as much planning, analysis, and design as you need, but no more.
  2. Deliver stakeholder value: everything you do should deliver value or increase your ability to do so.
  3. It's a behavior: everyone involved should have the same way of talking about the system and what it does.
BDD in its grandest sense is about communication and viewing your software as a system with behaviour. BDD tools such as RSpec and Cucumber strive to enable you to describe the behavior of your software in a very understandable way: understandable to everyone involved.
  1. 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.
  2.  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 language is called as “gherkin”.
  3. Given: This is used to describe the initial context of the system---the scene of the scenario. It is typically something that happened in the past.
  4. When: This is what the feature is talking about, the action, the behaviour that we're focused on.
  5. Then: This is used to describe an expected outcome, or result
  6. 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.
  7. If there are multiple Given or When steps underneath each other, you can use AndorBut.
Examples:
Feature: Login Action
  @login
  Scenario: Successful Login with Valid Credentials
    Given User is on Home Page
    When User Navigate to Test Page
    And User enters UserName and Password
    Then Message displayed Login Successfully

Step Definitions:
Cucumber doesn't know how to execute your scenarios out-of-the-box. It needs Step Definitions to translate plain text Gherkin steps into actions that will interact with the system.
When Cucumber executes a Step in a Scenario it will look for a matching Step Definition to execute.
A Step Definition is a small piece of code with a pattern attached to it. The pattern is used to link the step definition to all the matching Steps, and the code is what Cucumber will execute when it sees a Gherkin Step.

To understand how Step Definitions work, consider the following Scenario:
  Scenario: Successful Login with Valid Credentials
    Given User is on Home Page
The User is on Home Page part of the step (the text following the Given keyword) will match the Step Definition below:
@Given("^User is on Home Page$")
public void user_is_on_Home_Page() throws Throwable {
    driver.get("http://techyworks.blogspot.in/");
    String strPageTitle = driver.getTitle();
    System.out.println("Page Title:" + strPageTitle);
}
Commenting and Uncommenting lines of code in Gherkin:
You can start a line with a # to tell Cucumber that the remainder of the line is a comment, and shouldn't be executed.
If you want to comment multiple-line blocks then Press Ctrl+Slash


full-width