Skip to main content

restoreState

Overview

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

Usage

You can restore the browser state from either a file (using the path parameter) or directly from an object (using the data parameter).

Important: If you provide both path and data parameters, the file specified in path will take priority.

You can optionally specify which storage types to restore using the cookies, localStorage, and sessionStorage parameters. This allows you to restore only the specific data you need.

The state data for restoration can be obtained from the saveState command.

warning

You must be on the exact same page from which the cookies were originally saved. You need to navigate to the page first, use the url command before restoring state.

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

Command Parameters

NameTypeDefaultDescription
pathstring-Path to file with state.
dataSaveStateData-Object with state.
cookiesbooleantrueEnable restore cookies (true by default).
localStoragebooleantrueEnable restore localStorage (true by default).
sessionStoragebooleantrueEnable restore sessionStorage (true by default).
cookieFilter(cookie: Cookie) => boolean-

Function 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();
});

Example of implementing authentication in tests with saveState/restoreState and beforeAll hook.

import { ConfigInput, WdioBrowser } from "testplane";
import { launchBrowser } from "testplane/unstable";

export default {
gridUrl: "local",
beforeAll: async ({ config }) => {
const b = await launchBrowser(config.browsers["chrome"]!);

await b.url("https://our-site.com");
await b.$("input.login").setValue("user@example.com");
await b.$("input.password").setValue("password123");

await b.saveState({ path: "./.testplane/state.json" });
await b.deleteSession();
},
sets: {
/* ... */
},
browsers: {
chrome: {
headless: false,
desiredCapabilities: {
browserName: "chrome",
},
},
},
plugins: {
/* ... */
"@testplane/global-hook": {
enabled: true,
beforeEach: async ({ browser }: { browser: WdioBrowser }) => {
await browser.url("https://our-site.com");
await browser.restoreState({ path: "./.testplane/state.json" });
},
},
},
} satisfies ConfigInput;