Rayrun
← Back to Discord Forum

Storage state from a json in code

Hi! I saw this commands exists for loading a local storage into a json await page.context().storageState({ path: authFile }); is there a way to load a json into local storage (globally, on every page visited, even before navigating to it)? something like: await page.context().loadIntoLocalStorage({ "demo": "json" }); I'm using Playwright to Node.js

This thread is trying to answer question "Is there a way to load a JSON into local storage globally on every page visited, even before navigating to it, using Playwright for Node.js?"

6 replies

Thanks for the help Daniel I does work using page.evaluate. The thing is you must navigate to the page, add the local storage key using page.evalute, reload the page, and only then the local storage keys will be taken into effect. Is there any way to do it immediately?

refactoreric

When you create a new browser context, you can pass the storage state (cookies + local storage) as one of the options: https://playwright.dev/docs/api/class-browser#browser-new-context-option-storage-state

If a browser context is opened, you can save/retrieve the storage state using: https://playwright.dev/docs/api/class-browsercontext#browser-context-storage-state

And here's a guide how to use storageState with the @playwright/test runner: https://playwright.dev/docs/auth

thanks! does it mean it will attach those cookies + local storage to every page you're visiting?

As long as that browser context lives, those cookies and local storage will be available to the application (unless they are cleared by the application, or the cookies expire).

cool, thank you!

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].