Rayrun
← Back to Discord Forum

get local/session storage/cookies info in playwright

Hello, how do I get access to local storage / session storage / cookies information within the playwright tests? Thanks!

This thread is trying to answer question "How do I get access to local storage / session storage / cookies information within the playwright tests?"

2 replies

EG:

// if a page, we need to check for the csrf token from the page
      try {
        const maybeCsrf = await page.evaluate(() => {
          return window.sessionStorage.getItem('session_csrf_token');
        });
        assertIsDefined(maybeCsrf, 'existing page must have a csrf token');
        csrf = maybeCsrf;
      } catch (e) {
        // no csrf, proceed to login the page instance
        conditionalLogError('error getting csrf from page', e);
      }

Answer

Accessing Storage and Cookies in Playwright Tests

Playwright provides APIs to access local storage, session storage, and cookies in your tests.

Session Storage

To retrieve session storage data, use page.evaluate(). This method executes JavaScript code in the page context. Here's how to save session storage as an environment variable:

const sessionStorage = await page.evaluate(() => JSON.stringify(sessionStorage));
fs.writeFileSync('playwright/.auth/session.json', JSON.stringify(sessionStorage), 'utf-8');

To set session storage in a new context, parse and load it from a file using fs.readFileSync(). Then, use context.addInitScript() to set the session storage based on specific conditions:

const sessionStorage = JSON.parse(fs.readFileSync('playwright/.auth/session.json', 'utf-8'));
await context.addInitScript(storage => {
  if (window.location.hostname === 'ray.run') {
    for (const [key, value] of Object.entries(storage)) {
      window.sessionStorage.setItem(key, value);
    }
  }
}, sessionStorage);

Local Storage and Cookies

For local storage or cookies, use Playwright's built-in methods like context.storageState() and context.cookies().

After performing login actions on your application page, save the state of local storage by calling page.context().storageState({ path: STORAGE_STATE }). This saves all relevant local storage data into a specified file path.

To reuse this authenticated state, load this stored state by passing it as an argument to browser.newContext() using the storageState option:

const browserContext2 = await browser.newContext({ storageState });

This lets you continue generating tests from the logged-in state without needing to authenticate again.

Remember to handle sensitive information securely. Only use the storage file locally and consider adding it to your .gitignore or deleting it once you're done with your tests.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].