Skip to content

Commit ab6251f

Browse files
author
rocketraccoon
committed
feat: add get state
1 parent da2703f commit ab6251f

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

src/browser/commands/getState.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import fs from "fs-extra";
2+
3+
import type { Browser } from "../types";
4+
import { SaveStateData, SaveStateOptions } from "./saveState";
5+
6+
export type GetStateOptions = Required<Pick<SaveStateOptions, "path">>;
7+
8+
export default (browser: Browser): void => {
9+
const { publicAPI: session } = browser;
10+
11+
session.addCommand("getState", async ({ path }: GetStateOptions): Promise<SaveStateData> => fs.readJson(path));
12+
};

src/browser/commands/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ export const customCommandFileNames = [
1212
"waitForStaticToLoad",
1313
"saveState",
1414
"restoreState",
15+
"getState",
1516
"unstable_getCdp",
1617
];

src/browser/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { Test, Hook } from "../test-reader/test-object";
1010
import type { CaptureSnapshotOptions, CaptureSnapshotResult } from "./commands/captureDomSnapshot";
1111
import type { Options } from "@testplane/wdio-types";
1212
import type { SaveStateData, SaveStateOptions } from "./commands/saveState";
13+
import type { GetStateOptions } from "./commands/getState";
1314
import type { RestoreStateOptions } from "./commands/restoreState";
1415
import type { WaitForStaticToLoadResult } from "./commands/waitForStaticToLoad";
1516
import type { CDP } from "./cdp";
@@ -83,6 +84,7 @@ declare global {
8384

8485
saveState(options?: SaveStateOptions): Promise<SaveStateData>;
8586
restoreState(options: RestoreStateOptions): Promise<void>;
87+
getState(options: GetStateOptions): Promise<SaveStateData>;
8688

8789
overwriteCommand<CommandName extends BrowserCommand>(
8890
name: CommandName,

test/integration/standalone/standalone-save-state.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { DEVTOOLS_PROTOCOL, WEBDRIVER_PROTOCOL } from "../../../src/constants/co
88
import { AuthServer } from "./mock-auth-page/server";
99
import process from "node:process";
1010

11-
const TIMEOUT = 180000;
11+
const TIMEOUT = 320000;
1212

1313
// fix for ff, he doesn't like localhost in domain
1414
const removeDomainFromCookies = (loginState: SaveStateData): void => {
@@ -137,6 +137,17 @@ type AutomationProtocol = typeof DEVTOOLS_PROTOCOL | typeof WEBDRIVER_PROTOCOL;
137137
assert.strictEqual(fileExist, false);
138138
});
139139

140+
it("getState", async function () {
141+
const options = {
142+
path: "./state.json",
143+
};
144+
145+
const saveResult = await browser.saveState(options);
146+
const getResult = await browser.getState(options);
147+
148+
assert.deepEqual(saveResult, getResult);
149+
});
150+
140151
it("restoreState", async function () {
141152
if (loginState) {
142153
removeDomainFromCookies(loginState);

0 commit comments

Comments
 (0)