Skip to main content

Gherkin Basics

Gherkin is the plain-English format letsrunit uses for test scenarios. Each .feature file describes one feature and one or more scenarios.

File structure

Feature: Shopping cart

Background:
Given I'm on page "/shop"

Scenario: Add item to cart
When I click button "Add to cart"
Then The page contains text "1 item in cart"

Scenario: Remove item from cart
When I click button "Add to cart"
And I click button "Remove"
Then The page contains text "Cart is empty"
KeywordPurpose
FeatureNames the feature being tested (one per file)
BackgroundSteps that run before every scenario in the file
ScenarioA single test case
GivenSets up the initial state
WhenDescribes an action
ThenAsserts an outcome
And / ButContinues the previous Given, When, or Then

With and without Background

Use Background when most scenarios share the same starting point.

Feature: Dashboard

Background:
Given I'm on page "/login"
When I set field "Email" to "admin@example.com"
And I set field "Password" to "secret"
And I click button "Sign in"

Scenario: View user list
When I click link "Users"
Then The page contains text "All users"

Scenario: View settings
When I click link "Settings"
Then The page contains text "Account settings"

Custom parameter types

letsrunit extends Cucumber with three parameter types used across the step library:

TypeAcceptsExample
{locator}Any locator expressionbutton "Sign in", field "Email", `#submit`
{value}String, number, date expression, or array"hello", 42, date of tomorrow, ["A", "B"]
{keys}Quoted key or key combination"Enter", "Control+A", "Shift+Tab"

See Locators for the full locator syntax and Step Reference for every available step.