Answer
Purpose of Scenario Keywords in Cucumber
The Five Step Keywords Explained
1. Given — Precondition
Sets up the initial state of the system before any action is performed. Describes what is true at the start of the scenario.
Given the user is on the login page
Given the shopping cart contains 3 items
Given the database has a record with ID "001"
2. When — Action
Describes the action or event that the user performs. This is the trigger that initiates the behavior being tested.
When the user enters the username "admin"
When the user clicks the Submit button
When the user deletes the item from cart
3. Then — Expected Outcome
Specifies the observable outcome or assertion — what should happen after the When action. Always maps to an assertion in the step definition.
Then the dashboard should be displayed
Then the error message "Invalid login" should appear
Then the cart total should update to "$699"
4. And — Continuation
Used to join multiple steps of the same type (Given, When, or Then) without repeating the keyword. Makes scenarios more readable.
Given the user is on the login page
And the user has valid credentials ← same as Given
When the user enters username "admin"
And the user enters password "Admin@123" ← same as When
And the user clicks Login ← same as When
Then the dashboard should be visible
And the welcome message should be displayed ← same as Then
5. But — Negative Continuation
Used to add a negative condition after Then or Given. Semantically similar to And, but adds negative contrast for readability.
Then the user should be logged in
But the admin panel should not be visible
But the error message should not appear
Full Scenario Using All Keywords
Scenario: Successful product purchase
Given the user is logged in as "customer"
And the user has $500 credit available
When the user adds "Headphones" to the cart
And the user proceeds to checkout
And the user selects "Credit Card" payment
Then the order confirmation page should appear
And the order ID should be generated
But the payment error should not be displayed
