AND & BUT Keywords

 

And & But Keywords in Cucumber Framework

  • In Cucumber, the And and But keywords are used to improve the readability of test scenarios by avoiding repetitive Given, When, or Then statements. These keywords help structure multiple conditions within a scenario.

1️⃣ And Keyword

The And keyword is used to add additional/Positive conditions to a Given, When, or Then step.

Example 1: Using "And" in Given, When, and Then

Feature: Login Functionality  

Scenario: Successful Login  
  Given the user is on the login page  
  And the user has a valid username and password  
  When the user enters the credentials  
  And clicks on the login button  
  Then the user should be redirected to the homepage  
  And a welcome message should be displayed  

2️⃣ But Keyword

The But keyword is used to specify a negative condition (something that should not happen).

Example 2: Using "But" for Negative Condition

Feature: Login Functionality  

Scenario: Unsuccessful Login with Incorrect Password  
  Given the user is on the login page  
  When the user enters a valid username  
  But enters an incorrect password  
  Then the system should display an "Invalid credentials" message  
  And the user should not be logged in  

Conclusion

  • And is used to add multiple positive conditions.

  • But is used to add negative conditions.

  • Both improve readability and make scenarios more business-friendly.