Skip to main content

restoreState

Overview

Browser command that restores session state (cookies, local and session storages) from a file or variable.

warning

If you are using the webdriver automation protocol, you have to provide webSocketUrl: true in desiredCapabilities in config browser settings. For the devtools protocol, you don't need additional settings.

Usage

You can restore state from file (using the path param) or from object (using the data param). But if you provide both, the file will have the highest priority. Also, you can provide cookies, localStorage and sessionStorage params to restore only what you need. Data for restore state you can get from the saveState command.

await browser.restoreState({
path: "./stateDump.json",
data: stateDump,
cookies: true,
localStorage: true,
sessionStorage: true,
});

Command Parameters

NameTypeDescription
pathStringPath to file with state.
dataSaveStateDataObject with state.
cookiesBooleanEnable restore cookies (true by default).
localStorageBooleanEnable restore localStorage (true by default).
sessionStorageBooleanEnable restore sessionStorage (true by default).
cookieFilter(cookie: Cookie) => booleanFunction for filtering cookies, receiving cookie objects, and returning boolean.

Usage Examples

Restore state from file.

it("test", async ({ browser }) => {
await browser.url("https://github.com/gemini-testing/testplane");

await browser.restoreState({
path: "./stateDump.json",
cookieFilter: ({ domain }) => domain === ".example.com",
});

// Reload page for see auth result.
await browser.refresh();
});