Then the response status code should be {number}

Asserts that the HTTP status code of the current page is (or is not) a given number. The step takes the URL the browser is currently on and issues a fresh HTTP GET request to it, then compares the returned status code with the expected value. Error statuses such as 403, 404, or 500 are handled: the step still reads the status from the failed request and compares it normally.

Two gotchas follow from the fresh request. First, the request is made outside the browser session, so pages that require login may return a different status than the one the browser saw. Second, redirects are followed by the HTTP client, so asserting an intermediate code such as 301 checks the redirect response itself only when the client surfaces it; for most redirected pages the final status (usually 200) is what gets compared.

Accepted phrasings

Then the response status code should be 200
Then response status code should be 200
Then the response status code should not be 404
Then response status code should not be 404

Examples

Then the response status code should be 200
And the response status code should not be 404
Then the response status code should be 301
Then response status code should not be 500

In a real scenario

Feature: Public pages respond correctly

  Scenario: The homepage and contact page are reachable
    When I go to "https://example.org"
     And wait 2s
    Then I should see "Welcome"
     And the response status code should be 200
    When I go to "https://example.org/contact"
    Then the response status code should not be 404
     And the response should contain "Contact us"

Related steps

Back to Assertion Steps