Skip to navigation

Gherkin is the Given/When/Then language of Cucumber and other behavior-driven development tools. Doc Detective reads .feature files with no configuration. Each scenario runs as a test, and each step phrase maps to a Doc Detective action. You don’t write step definitions or install Cucumber.

Feature: Sign up
Background:
Given I go to "https://example.com/signup"
Scenario: Sign up with an email address
When I type "ada@example.com" into "#email"
And I click "Sign up"
Then I should see "Check your inbox"

File extensions

Doc Detective processes Gherkin files with the .feature extension.

To read files with another extension as Gherkin, add a file type that extends gherkin to fileTypes in your config file:

{
"fileTypes": [
{
"name": "stories",
"extends": "gherkin",
"extensions": ["story"]
}
]
}

A fileTypes list in your config replaces the default list. If your config already sets fileTypes, add "gherkin" to it to keep reading .feature files.

How scenarios become tests

  • Each .feature file is a spec.
  • Each Scenario (or Example) is a test. Its name is the test’s testId and description.
  • Background steps run at the start of every scenario in the file. A Background inside a Rule runs after the file’s Background, in that rule’s scenarios only.
  • A Scenario Outline (or Scenario Template) runs once per row of its Examples (or Scenarios) tables. Each <name> placeholder takes the row’s value, in step text, doc strings, and table cells. Each row’s testId is the scenario name plus [row N], counting from 1 across all of the outline’s Examples.
  • Rule groups scenarios. Its tags and Background apply to the scenarios inside it.

To filter scenarios with --test, use the scenario name, or set your own ID with an @id: tag.

Keywords must be in English.

Step phrases

With step detection on, which is the default, each step’s text after Given, When, Then, And, But, or * maps to an action:

Step phraseAction
I go to "<url>", I open "<url>"goTo
I click "<text or selector>"click
I see "<text or selector>", I should see "<text or selector>"find
I should not see "<text>"Checks that the page’s text doesn’t contain the text, with runBrowserScript
I type "<keys>"type
I type "<keys>" into "<selector>"type into an element
I press "<key>"type with a named key, such as "Enter", "Escape", or "Page Down"
I hover over "<text>", I scroll to "<text>"find with moveTo, which scrolls the element into view and moves the pointer to it
I drag "<source>" to "<target>"dragAndDrop
I swipe up, down, left, or rightswipe
the link "<url>" workscheckLink
I take a screenshot "<path>"screenshot
I take a screenshot of "<text or selector>" as "<path>"screenshot, cropped to the element
I start recording "<path>", I stop recordingrecord, stopRecord
I save the session as "<path>", I restore the session "<path>"saveState, loadState
I run "<command>", I run the command "<command>", I run the script "<command>"runShell
I run: with a doc stringrunCode. The doc string’s media type is the language
I wait <n> seconds, I wait <n> millisecondswait
the variables in "<path>"loadVariables
I send a <METHOD> request to "<url>", optionally ending in with: and a doc string bodyhttpRequest
the response status should be <n>, the response status is <n>Sets the accepted status code of the request step right before it
the API "<METHOD> <url>" returns "<file>", or returns: and a doc stringmockRequest. The method is optional

I run: makes a runCode step, which is unsafe, so it runs only with allowUnsafeSteps.

A step that matches no phrase doesn’t run, and Doc Detective warns:

Couldn't map step `When I frobnicate` in `/abs/path/login.feature:12` to an action. Add a `step` statement or a `markup` entry.

The rest of the scenario still runs. To run something there, add a # step statement, or add your own phrase in a file type that extends gherkin. Each phrase is a markup entry that matches element nodes with the tag step, and its regex runs on the step text without its keyword:

{
"fileTypes": [
{
"extends": "gherkin",
"markup": [
{
"name": "signIn",
"element": { "tag": ["step"] },
"regex": ["^I sign in as \"([^\"]+)\"$"],
"actions": [{ "type": { "keys": "$1", "selector": "#user" } }, { "click": "Sign in" }]
}
]
}
]
}

See Custom inputs for how markup entries merge.

Doc strings and data tables

A doc string (""" or ```) after I run:, a with: request, or returns: is the code, the request body, or the mocked body. Text after the opening """, such as bash or json, is the media type:

Scenario: Create a user
When I send a POST request to "https://api.example.com/users" with:
"""json
{"name": "Ada"}
"""
Then the response status should be 201

A data table after I fill in: types each row’s value into the row’s selector. A one-column table after I should see: finds each row’s text:

Scenario: Fill in the form
When I fill in:
| #email | ada@example.com |
| #name | Ada |
Then I should see:
| Welcome, Ada |

A selector can contain double quotes, such as [name="email"]. A value can’t. Doc Detective warns about that row and doesn’t type it, so use a # step comment with a type step instead.

Tags

Tags on a Feature, Rule, Scenario, or Examples table apply to everything inside it.

TagEffect
@chrome, @firefox, @safari, @webkitRuns the scenario in those browsers
@windows, @mac, @linux, @android, @iosRuns the scenario only on those platforms
@headlessRuns the browsers headless
@recordRecords a video of the scenario. See automatic recording
@skip, @wipSkips the scenario, with Skipped: test guard `if` condition not met.
@id:<id>Sets the scenario’s testId

Browser and platform tags set the test’s runOn, which replaces the contexts in your config for that scenario. Other tags have no effect.

Emulation steps

These steps set how the page sees the browser instead of running an action:

StepSets
a <width>x<height> viewport, as in Given a 390x844 viewportviewport
dark mode, light modecolorScheme
the locale is "<tag>", as in Given the locale is "de-DE"locale
the timezone is "<zone>", as in Given the timezone is "Europe/Berlin"timezone

They apply to the whole scenario, wherever they appear in it, and a Background sets them for every scenario. With no browser tag, the scenario runs in Chrome, the one browser that supports every setting. A browser that can’t apply a setting skips the scenario’s context. See page emulation for what each browser supports.

Combine browser tags with emulation steps to run one file across browsers and settings:

@chrome @firefox
Feature: Dashboard
Scenario: Dark mode
Given dark mode
And a 1280x800 viewport
When I go to "https://example.com/dashboard"
Then I take a screenshot "dashboard-dark.png"

Inline statements

Gherkin comments hold inline statements:

  • # step {...} adds a step where it sits in the scenario.
  • # test {...} above a scenario, or above its tags, adds settings to that scenario’s test. Its keys win over the ones tags set.
  • # test ignore start and # test ignore end skip the steps between them, including emulation steps. A block covers steps inside one scenario. To skip steps in several scenarios, put a block in each.

Each scenario is already a test, so it needs no # test end. Gherkin statements take JSON only.

Feature: Search
# test {"testId": "search", "runOn": [{"browsers": "firefox"}]}
Scenario: Search for kittens
Given I go to "https://example.com"
# step {"screenshot": {"path": "search.png", "maxVariation": 0.05}}
When I type "kittens" into "#q"
# test ignore start
Then I should see "Results for kittens"
# test ignore end

To run only the inline statements, turn off step detection with detectSteps: false in your config, or with # test {"detectSteps": false} above a scenario. Step phrases then don’t run or warn.

Warnings

WhenWarning
A step matches no phraseCouldn't map step `<step>` in `<file>:<line>` to an action. Add a `step` statement or a `markup` entry.
A Scenario Outline has no ExamplesCouldn't expand `Scenario Outline: <name>` in `<file>:<line>`: it has no `Examples`.

An outline with no Examples and a scenario with no steps don’t run.

Next steps