Understanding Gherkin Scenarios
The AI Recorder generates a Gherkin Scenario for every test along with the Playwright and Cucumber.js test automation code. This document explains the contents of a Gherkin Scenario file so you can undertand the scenarios generated from the AI Recorder.
What is Gherkin?
A Gherkin scenario is a test case that describes how a feature should work using simple, structured sentences. The Gherkin syntax uses specific keywords to organize these test cases.
Example
gherkin
Scenario Outline: Create a new GitHub repository
Given Create a new repository named "<repo>" with description "<description>" and a README file
When Navigate to the home page
When Filter the top repositories by "<repo>"
Then Verify the repository "<repo>" is listed under Top Repositories
Examples:
| repo | description |
| {{string.alpha(10)}} | Test repository|
Supported Gherkin Keywords
Here are the keywords you can use to write a Gherkin scenario:
- Scenario Outline: Describes the test scenario.
- Given: Sets up the conditions for the test.
- When: Specifies actions or events.
- Then: Describes the expected result.
- And: Connects multiple steps.
- Examples: Defines test data for the scenario.
Scenario Outline
This keyword gives a brief description of what the test scenario is about. It’s the first line of the scenario.
Example:
gherkin
Scenario Outline: Create a new Github repository
Given & When
Use Given
for setup steps like logging in or navigating to a page. Use When
for actions like clicking buttons or filling forms.
gherkin
Scenario Outline: Buy an item
Given Log in with username "blinq_admin" and password "let_me_in"
And Add "keyboard" to the basket
When Open the basket
And Checkout with first name "John", last name "Doe", zip "10001"
Then
Then
is used for verifying expected results, such as checking if the price is correct or if a message appears on the screen.
gherkin
Scenario Outline: Buy an item
Given Log in with username "blinq_admin"
When Open the basket
Then The total price is 25.99
And Verify "Thank you for your order" is displayed
NOTE
Use Then
only for visible outcomes, not for database checks or internal system states.
And
You can use And
to make your steps easier to read when there are multiple Given
, When
, or Then
steps in a row.
gherkin
Scenario Outline: Buy an item
Given Log in with username "blinq_admin"
And Add "keyboard" to the basket
When Open the basket
And Select premium shipping
Then The total price is 25.99
And Verify "Thank you for your order" is displayed
The Examples
Section
The Examples
section defines the test data for your scenario. Each row in the table represents a different test run.
Gherkin
Scenario Outline: Create a new GitHub repository
Given Create a new repository named "<repo>" with description "<description>"
When Navigate to the home page
Then Verify the repository "<repo>" is listed under Top Repositories
Examples:
| repo | description |
| {{string.alpha(10)}} | A test repository |