question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Persist Auth from Session Storage?

See original GitHub issue

I have seen the docs on this: https://playwright.dev/docs/auth#session-storage.

However, I am a bit lost at the location I should add the hook for addInitScript.

I have tried adding it in: beforeAll, the test itself, overriding the context fixture. However, none seem to work. Where would be the proper place to place this hook?

const test = base.extend({
  // override `context` fixture to add init script
  context: async ({ context }, use) => {
    await context.addInitScript(() => {
      const entries = JSON.parse(process.env.SESSION_STORAGE);
      for (const [key, value] of Object.entries(entries)) {
         window.sessionStorage.setItem(key, value);
      }
    });
    await use(context);
  },
});

This is the attempt with overriding. If I add a console.log inside the initScript fn it doesn’t show anything, if I add it outside, it works. Also, the session storage seems to have values, it’s just that they are all set on null. The value from the environmental variable is correct.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
taigi100commented, May 13, 2022

@Citronex Not sure if this is best practices, it works. In every test suite I have, on a beforeAll I do a login and save the session in an environmental variable as per the docs:

export async function loginAndSaveSession(browser: Browser) {
  const page = await browser.newPage();
  const context = await browser.newContext();
  await login(page);
  await saveSession(page);
  await context.close();
  await page.close();
}

Then, to properly load the session on every test, I override the fixture:

export const test = base.extend({
  // override `context` fixture to add init script
  context: async ({ context }, use) => {
    const sessionStorage = process.env.SESSION_STORAGE;
    await context.addInitScript((storage: string) => {
      const entries = Object.entries(JSON.parse(storage));
      for (let i = 0; i < entries.length; i += 1) {
        const [key, value]: [key: string, value: any] = entries[i];
        window.sessionStorage.setItem(key, value);
      }
    }, sessionStorage);
    await use(context);
  },
});

The tests then look exactly like the normal tests, but just use the extended test function instead of the one from playwright.

0reactions
Citronexcommented, May 17, 2022

Thanks @taigi100 ! Yeah I’m doing something very similar, the only difference is that my login is inside global-setup.ts. I save the session inside process.env and then override the fixture.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to persist to both localStorage and sessionStorage at the ...
My first question is if my client side view and approach is correct or not. If it is, how can I update my...
Read more >
Local Storage vs. Session Storage vs. Cookies
In this post, I'll talk about how cookies compare to browser storage. You'll understand why cookies came out, the problem they solve, and...
Read more >
ASP.NET Core Blazor state management - Microsoft Learn
sessionStorage is scoped to the browser tab. If the user reloads the tab, the state persists. If the user closes the tab or...
Read more >
Storing authentication sessions in external DB
I have a 4 server PF cluster with 1 admin node and 3 engine nodes. Even after modifying the org.sourceid.saml20.service.session.data.impl.
Read more >
Web Storage API - MDN Web Docs - Mozilla
sessionStorage maintains a separate storage area for each given origin that's available for the duration of the page session (as long as the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found