Answer
Gherkin Keywords for Writing Scenarios
Step Keywords (Most Important)
| Keyword | Purpose | Example |
|---|---|---|
Given | Precondition — sets initial context | Given the user is on the login page |
When | Action — event that triggers behavior | When the user enters valid credentials |
Then | Outcome — expected result / assertion | Then the dashboard should be displayed |
And | Adds another step of the same type | And the user clicks the Login button |
But | Negative continuation | But the error message should not appear |
Structural Keywords
| Keyword | Purpose |
|---|---|
Feature | Describes the feature being tested |
Scenario | A single test case |
Scenario Outline | Parameterized test with multiple data sets |
Background | Common steps before each scenario |
Examples | Data table used with Scenario Outline |
Complete Example
Gherkin
Feature: Shopping Cart
Background:
Given the user is logged in
And the user is on the product page
Scenario: Add single item to cart
When the user clicks "Add to Cart" for "Laptop"
Then the cart count should show "1"
And the cart total should be "$999"
But the checkout button should not be disabled
Scenario Outline: Add multiple products
When the user adds "<product>" to the cart
Then the cart should contain "<product>"
And the price shown should be "<price>"
Examples:
| product | price |
| Laptop | $999 |
| Phone | $599 |
| Tablet | $399 |
Tips
- ✓
And/Butinherit the type of the preceding step (Given/When/Then) - ✓Use
Andto keep scenarios readable without repeating Given/When/Then - ✓Steps should describe business behavior, not implementation details
