Skip to main content

And and But keyword in Cucumber

Our Business requirement does not only consist of a single pre-condition, user action, or expected outcomes. We mention Given, When, and Then more than once for a few scenarios. 

Cucumber will not restrict you to using Given, When, and Then multiple times, but it is good to avoid using the same keyword in the following line for readability and expressiveness.

AND keywords in Cucumber

The And keyword is used to add conditions to your steps or add more detail to an action. It's an addition to the previous statement. AND is used to add more conditions to Given, When and Then statements. It represents a positive statement. 

BUT keyword Cucumber

BUT keyword is used to add negative type comments. It is good to use when your step describes a condition that is not expected. For example, when you expect some text or element to not be present on the page.​

Example of Scenario without And and But

@userflow
Feature: Registration, Login and MyAccount

  @regression @testing
  Scenario: Verify Login Functionality
    Given I am on the homepage
    Given I should see "Sign in" link
    When I follow "Sign in" link
    When I fill "email address" with "goswami.tarun77@gmail.com"
    When I fill "password" with "tarun@123"
    When I click "sign in" button
    Then I should see "My Account" Heading
    Then I should see "my account" link
    Then I should not see "Login" Heading

The problem with the above scenario is that Given, When, and, Then are repeated multiple times, and readability is affected.

Example of Scenario with And and But

@userflow
Feature: Registration, Login and MyAccount

  @regression @testing
  Scenario: Verify Login Functionality
    Given I am on the homepage
    And I should see "Sign in" link
    When I follow "Sign in" link
    And I fill "email address" with "goswami.tarun77@gmail.com"
    And I fill "password" with "tarun@123"
    And I click "sign in" button
    Then I should see "My Account" Heading
    And I should see "my account" link
    But I should not see "Login" Heading

Video Tutorial: Difference between AND and BUT in Cucumber

Submitted by tgoswami on August 30, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.