Skip to main content

getState

Available from testplane v8.40.0

Overview​

Reads a JSON state file created by saveState and returns its contents. It does not restore browser state: cookies, storage and the current page stay unchanged. To restore the session, use restoreState.

Usage​

await browser.getState(options)

Command parameters​

ParameterTypeDescription
options.pathstringPath to the JSON file. Relative paths resolve from the Testplane process working directory.

When options is omitted, the command uses browser.config.stateOpts. An explicit object is not merged with configuration: getState({}) does not inherit the configured path.

Result and errors​

Returns a Promise with the parsed data. A saveState file contains SaveStateData: an optional cookies array and framesData, with localStorage and sessionStorage grouped by origin. The command does not validate the JSON schema.

If no path is set, it logs an error and returns null. File read and JSON parsing errors reject the Promise.

Example​

Set baseUrl to a test HTML page. The example saves localStorage to a temporary file, changes the browser value and verifies that reading the file does not restore the session.

const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const os = require("node:os");
const path = require("node:path");

it("getState: read a saved file without restoring the session", async ({ browser }) => {
const directory = await fs.mkdtemp(path.join(os.tmpdir(), "testplane-state-"));
const statePath = path.join(directory, "state.json");
try {
await browser.url("/");
const origin = new URL(await browser.getUrl()).origin;
await browser.execute(() => localStorage.setItem("example", "saved"));
await browser.saveState({ path: statePath, cookies: false, localStorage: true });
await browser.execute(() => localStorage.setItem("example", "current"));
const state = await browser.getState({ path: statePath });
assert.equal(state.framesData[origin].localStorage.example, "saved");
assert.equal(await browser.execute(() => localStorage.getItem("example")), "current");
} finally {
await fs.rm(directory, { recursive: true, force: true });
}
});