Rayrun
← Back to Discord Forum

How to set session storage for single test?

How can I set session storage for just a single test?

I tried along the lines below to no success . Just to note, I do not need to authenticate. I just need to have this dummy data in there for some other logic of the app. not related to auth.

describe.only('When using SSO client', () => {
        beforeEach(async ({ }) => {
          const mockData = '{ "ssoUser": "corporateClientId" : { "foo" } }';
          window.sessionStorage.setItem('SSO.USER', mockData);
          await homePage.goto();
        });

This thread is trying to answer question "How can I set session storage for just a single test?"

7 replies

Hi @dman777 Your tests execute in NodeJS. To have access to the 'window' object, the code needs to execute in the browser. Therefore you will need to use one of the various 'evaluate' functions from Playwright.

The link Shiva shared contains some useful hints for implementation. It uses addInitScript, which is one of the evaluate functions. Besides addInitScript you could use ... https://playwright.dev/docs/api/class-page#page-evaluate ... and several others.

But the evaluate function happens after the test runs. I need to set the session storage before the test runs.

refactoreric

If you do it in the beforeEach like your code snippet in the original post, then why would the evaluate happen after the test runs?

Thanks, I tried the below but I got a new issue. Any ideas on how to resolve, please?

Error: page.evaluate: DOMException: Failed to read the 'sessionStorage' property from 'Window': Access is denied for this document.

const data = { ssoUser : { userId: 'abc' } };
        await page.evaluate(() => {
          window.sessionStorage.setItem('USER.SSO', JSON.stringify(data));
        });

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 luc@ray.run.