Playwright Cookies and Storage
A coat check hands out a numbered ticket, and the ticket proves which coat belongs to which guest. Websites use similar tickets. Cookies and browser storage remember who a visitor is, what sits in a cart, and which theme the visitor prefers. Playwright reads, writes, and clears these items so tests can control the memory of a browser.
Three Places a Browser Remembers Things
Browser Context
|
+-- Cookies small items sent to the server with each request
|
+-- localStorage data kept even after the browser closes
|
+-- sessionStorage data kept only while the tab stays open
Reading Cookies
The cookies method of a context returns every cookie as a list of objects.
const cookies = await context.cookies();
const session = cookies.find(c => c.name === 'session_id');
expect(session).toBeDefined();
Adding Cookies
The addCookies method creates cookies before the page loads. A cookie needs a name, a value, and either a url or a domain with a path.
await context.addCookies([
{ name: 'consent', value: 'accepted', url: 'https://example.com' },
]);
await page.goto('https://example.com');
Clearing Cookies
await context.clearCookies();
Working With localStorage
Local storage lives inside the page, so tests reach it through the evaluate method, which runs a small function in the browser.
await page.evaluate(() => localStorage.setItem('theme', 'dark'));
const theme = await page.evaluate(() => localStorage.getItem('theme'));
expect(theme).toBe('dark');
Setting Storage Before the Page Loads
Some sites read storage the instant they open. The addInitScript method plants values early enough.
await context.addInitScript(() => {
window.localStorage.setItem('onboarding', 'done');
});
Cookie Flow Diagram
Test adds cookie consent=accepted
|
v
Browser opens the shop
|
v
Site reads the cookie
|
v
Banner stays hidden -> test checks the main page directly
A Real-World Scenario
A news website shows a cookie consent banner on every first visit. The banner covers the headline area and distracts from the feature under test. The test adds a consent cookie before opening the page. The banner never appears, and the test focuses on the article layout.
A second scenario tests a shopping cart. The test adds a product, reloads the page, and reads localStorage. A saved cart item proves the site remembers shoppers between visits.
Helpful Tips
- Remember that every context keeps its own cookies and storage.
- Set cookies before navigation so the first request already carries them.
- Clear storage between scenarios that need a clean visitor.
- Never print real session cookies into logs or reports.
Quick Practice Task
Open any demo site, set a localStorage key named theme with the value dark, reload the page, and read the value back. Confirm the value survives the reload.
Common Questions
Why does a cookie not stick?
A cookie needs a matching domain and path. A missing url or a mismatched domain makes the browser discard it silently.
Does clearing cookies log the user out?
Yes, for sites that store the session in a cookie. Clear cookies at the start of scenarios that need an anonymous visitor.
Can a test read sessionStorage?
Yes. Use page.evaluate with sessionStorage.getItem. The data lives only inside the tab that created it.
Key Takeaways
- Cookies, localStorage, and sessionStorage hold the memory of a browser.
- Context methods manage cookies, and
evaluatemanages local storage. - Initial scripts plant values before the site loads.
- Prepared cookies remove banners and speed up repeated setup.
