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

Creating a Scenario with the And and But keywords

When we specify a business requirement, sometimes there are multiple pre-conditions, user actions, and expected outcomes. So how do we write these specifications in Cucumber?

Getting ready…

Based on what we have learned so far we know how to create Scenarios with one Given, When, and Then keyword. Now, if we need to add multiple Steps, then we can update our Feature file like this:

Feature: login Page
  In order to test login page
  As a Registered user
  I want to specify the login conditions

  Scenario: without and & but
    Given user is on Application landing page
    Given Sign in button is present on screen
    When user clicks on Sign in button
    Then user can see login screen
    When user enters "ShankarGarg" in username field
    When user enters "123456" in password field
    When user clicks Sign in button
    Then user is on home page
    Then title of home page is "GitHub"

The problem here is that the keywords Given, When, and Then are repeated and the readability is thus affected. Having readable Feature files is one of biggest advantages of Cucumber. So how do we maintain the readability of Feature files? Let's figure this out in this recipe.

How to do it…

In this recipe, we are going to add one more Scenario and will use the And and But keywords:

Feature: login Page
  In order to test login page
  As a Registered user
  I want to specify the login conditions

  Scenario: with and & but
    Given user is on Application landing page
    And Sign in button is present on screen
    When user clicks on Sign in button
    Then user is displayed login screen
    When user enters "ShankarGarg" in username field
    And user enters "123456" in password field
    And user clicks Sign in button
    Then user is on home page
    And title of home page is "GitHub"
    But Sign in button is not present

How it works…

The And and But keywords will be useful here. These keywords help to increase the expressiveness and readability of the Feature file:

  • And: This is used for statements that are an addition to the previous Steps and represent positives statements.
  • But: This is used for statements that are an addition to previous Steps and represent negative statements.

Note

In a Step Definitions file, And and But are listed as Given/When/Then, the keyword that they appear after. There are no And and But keywords in Step Definitions.

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