Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Cucumber Cookbook

You're reading from   Cucumber Cookbook Over 35 hands-on recipes to efficiently master the art of behaviour-driven development using Cucumber-JVM

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher
ISBN-13 9781785286001
Length 162 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Shankar Garg Shankar Garg
Author Profile Icon Shankar Garg
Shankar Garg
Arrow right icon
View More author details
Toc

Table of Contents (8) Chapters Close

Preface 1. Writing Feature Files FREE CHAPTER 2. Creating Step Definitions 3. Enabling Fixtures 4. Configuring Cucumber 5. Running Cucumber 6. Building Cucumber Frameworks Index

Adding Backgrounds to Feature files

When we write Feature files, we write multiple Scenarios. Now all of these Scenarios start from one particular point. If I'm writing home page Scenarios, then I need to start the flow from the login functionality. So it is better to write the repetitive Steps at one place rather than in all Scenarios. Let's understand how to do this in the next Section.

Getting ready

Based on what we have learned so far, this is what our Feature file will look like:

Feature: Home Page
  In order to test Home Page of application
  As a Registered user
  I want to specify the features of home page

  Scenario: Home Page Default content
    Given a registered user exists
    Given user is on GitHub login page
    When user enters username
    And user enters password
    And user clicks on login button
    Then user is on Application home page
    And user gets a GitHub bootcamp section

  Scenario: GitHub Bootcamp Section
    Given user is on GitHub loginpage
    When user enters username
    And user enters password
    And user clicks on login button
    Then user is on Application home page
    When user focuses on GitHub Bootcamp Section
    Then user gets an option to setup git

  Scenario: Top Banner content
    Given user is on GitHub login page
    When user enters username
    And user enters password
    And user clicks on login button
    Then user is on Application home page
    When user focuses on Top Banner
    Then user gets a logout option

The problem here is that first five statements are repeated in all the Scenarios. This affects the readability of the Feature files, and there is a lot of duplicated effort.

The problems with this way of writing Feature files are:

  • Repetition: Many statements are repeated in all the Scenarios
  • Readability: The readability of the Feature files is affected.
  • Duplication: Copying these Steps to all the Scenarios is redundant.
  • Maintainability: Even if a single Step changes, we have to change all occurrences.

How to do it…

We are going to update the home_page.feature file and we are going to use the Background keyword to put the common Steps across all the Scenarios in one place:

Feature: Home Page
  In order to test Home Page of application
  As a Registered user
  I want to specify the features of home page

  Background: flow till home page
    Given user is on Application home page
    When user enters username
    And user enters password
    And user clicks on login button
    Then user is on Application home page

  Scenario: Home Page Default content
    Then user gets a GitHub bootcamp section

  Scenario: GitHub Bootcamp Section
    When user focuses on GitHub Bootcamp Section
    Then user gets an option to setup git

  Scenario: Top Banner content
    When user focuses on Top Banner
    Then user gets an option of home page

How it works…

Here, we have used the Background keyword. All the Steps mentioned in the Background keyword will be executed before each Scenario or Scenario Outline in a Feature file. Let's understand this keyword in greater detail:

  • There can be only one Background in one Feature file and it allows us to set a precondition for all Scenarios in a Feature file.
  • A Background is like a Scenario, containing a number of Steps.
  • Background is run before each Scenario, but after the BeforeScenario Hooks. (We will read about Hooks in Chapter 3, Enabling Fixtures).
  • The title and multiline description / intent of Background are optional.
  • Since the Steps mentioned in Background will be run for all Scenarios in a Feature file, we need to be careful when adding the Steps to Background. For example, we should not add a Step that is not common to all Scenarios.

This is what the output of the preceding file looks like:

How it works…

Don't use Background to set up a complicated state unless that state is actually something the client needs to know.

  • Keep your Background section short because you expect a person to remember these Steps when you are adding a new Scenario
  • Make your Background section vivid, because that way it will be easier for a person to remember it
You have been reading a chapter from
Cucumber Cookbook
Published in: Jun 2015
Publisher:
ISBN-13: 9781785286001
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image