Maintain Login Session after first browser instance in playwright

Viewed 5015

I am running multiple instances of chromium with playwright.

On each instance i am logging in with the same credentials.

Is there a way to automatically login into the other browser instances when i have already logged in to the first browser instance with the same credentials?

1 Answers

Playwright APIs can extract logged in state (cookies/local storage) from one browser context and load it into another browser context. This way, you can save the time required to log in via the UI. The choice of auth state will depend on your application.

// Get cookies and store as an env variable
const cookies = await context.cookies();
process.env.COOKIES = JSON.stringify(cookies);

// Set cookies in a new context
const deserializedCookies = JSON.parse(process.env.COOKIES)
await context.addCookies(deserializedCookies);

For more info see Authentication in the Playwright docs.

Related