Storage Steps

The storage steps manipulate cookies, local storage, and session storage so a scenario can pre-seed client-side state without clicking through the UI. They are the fastest way to put the browser into a known condition - an accepted cookie consent, a completed onboarding flag, an A/B-test bucket assignment, a language preference, a feature toggle - and then verify how the site behaves with (or without) that state. Front-end personalization, preference, and consent flows all become directly testable.

Scoping matters and differs by mechanism. Cookies are set on the browser context and scoped to the current page's domain - or localhost (derived from launchUrl) when no page has been loaded yet - so a cookie step can legitimately run before the first navigation. Local storage and session storage, by contrast, are per-origin and require a navigated page first: open the page, set the entry, then reload or navigate for scripts that read storage at load time to pick it up.

All of these are Given steps: they arrange state, they do not assert it. To assert cookie values, use the cookie assertion steps (e.g. Then a cookie with the name "session_id" should exist) from the cookie steps group. Storage values are always strings - JSON-encode complex objects manually before storing them.

Quick reference

StepWhat it does
Given the cookie "session" is set to "abc123"Set a cookie on the current browser context.
Given the cookie "session" is removedRemove a single cookie by name (other cookies preserved).
Given all cookies are clearedWipe every cookie on the browser context.
Given the local storage "user" is set to "Alice"Set a local storage entry on the current page.
Given the local storage "user" is removedRemove a single local storage entry.
Given local storage is clearedWipe local storage for the current origin.
Given the session storage "checkout step" is set to "2"Set a session storage entry on the current page.
Given the session storage "checkout step" is removedRemove a single session storage entry.
Given session storage is clearedWipe session storage for the current origin.

The leading the is optional throughout: Given cookie "ab_bucket" is set to "variant-b" and Given local storage "lang" is set to "fr" both match.

Steps in detail

Given the cookie "session" is set to "abc123"

Adds a cookie to the browser context with path /. The domain comes from the current page's URL, or from launchUrl (falling back to localhost) when the scenario has not navigated yet - so it is safe, and common, to set cookies before the first page load. Use it to seed sessions, consent flags, language choices, and any cookie-driven server behavior.

Given the cookie "consent" is set to "accepted"
  And I am on the homepage
 Then I should not see "Accept cookies"

Given the cookie "session" is removed

Deletes exactly one cookie by name and preserves the rest (the implementation reads all cookies, clears the jar, and re-adds the survivors). Ideal for testing what happens when one piece of state expires while the rest of the session remains.

Given the cookie "consent" is removed
 When I reload the page
 Then I should see "Accept cookies"

Given all cookies are cleared

Empties the entire cookie jar for the browser context - the "fresh visitor" reset. Pair it with the storage-clearing steps for a fully clean client state.

Given all cookies are cleared
  And local storage is cleared
  And session storage is cleared
 When I am on the homepage
 Then I should see "Sign in"

Given the local storage "user" is set to "Alice"

Writes a key/value pair into localStorage on the current page. Because local storage is per-origin, a page must be loaded first - navigate, set the entry, then reload so load-time scripts read it. Values are strings; store JSON text for structured data.

Given I am on the homepage
  And the local storage "theme" is set to "dark"
 When I reload the page
 Then "<body>" should have class "theme-dark"

Given the local storage "user" is removed

Removes one localStorage key, leaving the rest untouched. Use it to simulate a single expired or missing piece of persisted state - an emptied cart, a cleared auth token, a reset onboarding flag.

Given the local storage "cart" is removed
  And I am on "/cart"
 Then I should see "Your cart is empty"

Given local storage is cleared

Calls localStorage.clear() for the current origin, wiping every key at once - the storage counterpart of clearing all cookies for first-visit scenarios.

Given I am on the homepage
  And local storage is cleared
 When I reload the page
 Then I should see "First-time visitor"

Given the session storage "checkout step" is set to "2"

Writes a key/value pair into sessionStorage, which lives only for the current tab's session - the natural home of wizard progress, temporary form drafts, and tab-scoped identifiers. Like local storage, it requires a loaded page and stores strings only. Keys may contain spaces.

Given the session storage "wizard progress" is set to "3/5"
  And I am on "/wizard"
 Then I should see "Step 3"

Given the session storage "checkout step" is removed

Removes one sessionStorage key. Useful for verifying that a flow degrades gracefully when its mid-flow state disappears - for example, that a wizard restarts at step one.

Given the session storage "wizard progress" is removed
 When I reload the page
 Then I should see "Step 1"

Given session storage is cleared

Calls sessionStorage.clear() for the current origin, removing all tab-session state in one step.

Given session storage is cleared
 When I reload the page
 Then I should see "Session expired"

Complete example

Feature: Client-side state seeding with cookies and web storage
  As a tester,
  I want to pre-seed cookies, local storage, and session storage
  so that I can verify personalization and consent flows without clicking through setup.

  Scenario: Returning visitor with saved preferences skips the consent banner
    Given the cookie "consent" is set to "accepted"
      And I am on the homepage
      And the local storage "theme" is set to "dark"
      And the local storage "onboarding_complete" is set to "true"
     When I reload the page
     Then I should not see "Accept cookies"
      And I should not see "Welcome tour"

  Scenario: A brand-new visitor sees the first-run experience
    Given all cookies are cleared
      And I am on the homepage
      And local storage is cleared
      And session storage is cleared
     When I reload the page
     Then I should see "Accept cookies"

Tips

  • Order matters for web storage: navigate first (Given I am on the homepage), then set the entry, then reload - scripts that read storage on load will not see values set after the page rendered.
  • Cookie steps work before any navigation; the domain falls back to the host of launchUrl, so seeded cookies are already in place on the very first page load.
  • These steps only arrange state. Assert cookies with the cookie steps (the examples/cookies.html fixture and cookie.feature show them in action), and assert storage-driven behavior through the UI it affects.
  • Session storage is per tab: opening a new tab or context starts empty, which makes it a good target for "state lost mid-flow" scenarios.

Back to all step groups