Book Image

Cucumber Cookbook

By : Shankar Garg
Book Image

Cucumber Cookbook

By: Shankar Garg

Overview of this book

Table of Contents (13 chapters)
Cucumber Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Writing your first Feature file with one Scenario


Let's assume you are a Product Owner (PO) or a Business Analyst (BA). Your team is creating a web application and you need to write specifications for that application. A very simple and basic specification for that web application is when we enter the URL of that application in a browser, the application should load. So how do we write this specification in Cucumber? We will be covering this in this recipe.

How to do it…

In this recipe, we are going to create a simple Feature file with only one Scenario that tests whether the web page has loaded or not.

Let's create a page_load.feature file:

  Feature: Test Git web Application
  In order to Test Git web Application
  As a user
  I want to specify the application flow

  Scenario: Web Site loads
  application page load should be quick
  
  Given application URL is ready with the user
  When user enters the URL in browser
  Then application page loads

How it works…

In Cucumber we write our requirements in plain English like Language, Gherkin. Gherkin is a domain-specific language that has a very well-defined syntax. It works on the basis of some predefined keywords. In the preceding example, the highlighted portions of the text are Gherkin's keywords and the rest is dependent on the application under test.

Let's understand each keyword in more detail.

Feature

In Cucumber, Feature files contain business requirements. The text that immediately follows the Feature keyword, and is in the same line, is the Title of the Feature file. Three (optional) Text lines that follow the Feature keyword line are Intent of the Feature file and intent text is whatever we want to write, up until the first Scenario. Feature file should contain either Scenario or Scenario Outline. The naming conventions for Feature files should be lowercase with underscores, for example, login.feature and home_page.feature. The names of Scenarios and Feature files must be unique.

Scenarios

Scenarios are like test cases and start with the Scenario keyword in a new line (different from the Feature intent). The text that immediately follows the Scenario keyword, and is on the same line, is the Scenario Title. Three (optional) Text lines that follow the Scenario keyword line are Intent of the Scenario. All Scenarios perform following:

  • Get the system into a particular state

  • Poke it (perform some action)

  • Examine the new state

Steps

Scenarios contain Steps which are equivalent to test Steps and use the following keywords to denote them: Given, When, Then, But, and And (case sensitive).

Note

When you save the Feature files mentioned in this chapter and run them, in the first run, Cucumber is going to give errors for the missing Step Definition files, along with suggestions for Step Definitions. To resolve these errors, copy the suggestions given by Cucumber and paste them into a default Step Definition file.