Skip to content

Commit 74dea3c

Browse files
author
rocketraccoon
committed
feat: add tests + fix for standalone
1 parent b70438f commit 74dea3c

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

src/browser/commands/saveState/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,15 @@ export default (browser: ExistingBrowser): void => {
190190
logger.warn(
191191
"\x1b[31mPlease be aware that the file containing authorization data will not be automatically deleted after the tests are completed!!!\x1b[0m",
192192
);
193-
} else if (process.send) {
194-
process.send({
195-
event: MasterEvents.ADD_FILE_TO_REMOVE,
196-
data: options.path,
197-
});
193+
} else {
194+
if (process.send) {
195+
process.send({
196+
event: MasterEvents.ADD_FILE_TO_REMOVE,
197+
data: options.path,
198+
});
199+
}
200+
201+
browser.emitter.emit(MasterEvents.ADD_FILE_TO_REMOVE, options.path);
198202
}
199203
}
200204

src/browser/standalone/attachToBrowser.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Config } from "../../config";
22
import { ExistingBrowser } from "./../existing-browser";
33
import { Calibrator } from "./../calibrator";
4-
import { AsyncEmitter } from "../../events";
4+
import { AsyncEmitter, MasterEvents } from "../../events";
55
import { BrowserName, type W3CBrowserName, type SessionOptions } from "./../types";
66
import { getNormalizedBrowserName } from "../../utils/browser";
7+
import fs from "fs-extra";
78

89
export async function attachToBrowser(session: SessionOptions): Promise<WebdriverIO.Browser> {
910
const browserName = session.sessionCaps?.browserName || BrowserName.CHROME;
@@ -24,6 +25,8 @@ export async function attachToBrowser(session: SessionOptions): Promise<Webdrive
2425
},
2526
};
2627

28+
const filesToRemove: string[] = [];
29+
2730
const config = new Config({
2831
browsers: {
2932
[browserName]: browserConfig,
@@ -36,6 +39,10 @@ export async function attachToBrowser(session: SessionOptions): Promise<Webdrive
3639

3740
const emitter = new AsyncEmitter();
3841

42+
emitter.on(MasterEvents.ADD_FILE_TO_REMOVE, (path: string) => {
43+
filesToRemove.push(path);
44+
});
45+
3946
const existingBrowser = new ExistingBrowser(config, {
4047
id: browserName,
4148
version: session.sessionCaps?.browserVersion,
@@ -54,6 +61,10 @@ export async function attachToBrowser(session: SessionOptions): Promise<Webdrive
5461
if (session.driverPid) {
5562
process.kill(session.driverPid, 9);
5663
}
64+
65+
if (filesToRemove.length > 0) {
66+
await Promise.all(filesToRemove.map(path => fs.remove(path)));
67+
}
5768
});
5869

5970
return existingBrowser.publicAPI;

src/browser/standalone/launchBrowser.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import { Config } from "../../config";
22
import { NewBrowser } from "./../new-browser";
33
import { ExistingBrowser } from "./../existing-browser";
44
import { Calibrator } from "./../calibrator";
5-
import { AsyncEmitter } from "../../events";
5+
import { AsyncEmitter, MasterEvents } from "../../events";
66
import { BrowserName, type W3CBrowserName } from "./../types";
77
import { getNormalizedBrowserName } from "../../utils/browser";
88
import { LOCAL_GRID_URL } from "../../constants/config";
99
import { WebdriverPool } from "../../browser-pool/webdriver-pool";
1010
import type { StandaloneBrowserOptionsInput } from "./types";
11+
import fs from "fs-extra";
1112

1213
const webdriverPool = new WebdriverPool();
1314

@@ -50,6 +51,8 @@ export async function launchBrowser(
5051
prepareBrowser: options.prepareBrowser,
5152
};
5253

54+
const filesToRemove: string[] = [];
55+
5356
const config = new Config({
5457
browsers: {
5558
[browserName]: browserConfig,
@@ -62,6 +65,10 @@ export async function launchBrowser(
6265

6366
const emitter = new AsyncEmitter();
6467

68+
emitter.on(MasterEvents.ADD_FILE_TO_REMOVE, (path: string) => {
69+
filesToRemove.push(path);
70+
});
71+
6572
const newBrowser = new NewBrowser(config, {
6673
id: browserName,
6774
version: desiredCapabilities.browserVersion,
@@ -97,6 +104,10 @@ export async function launchBrowser(
97104
existingBrowser.publicAPI.overwriteCommand("deleteSession", async function () {
98105
await existingBrowser.quit();
99106
await newBrowser.kill();
107+
108+
if (filesToRemove.length > 0) {
109+
await Promise.all(filesToRemove.map(path => fs.remove(path)));
110+
}
100111
});
101112

102113
existingBrowser.publicAPI.addCommand("getDriverPid", () => newBrowser.getDriverPid());

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from "fs";
12
import { strict as assert } from "assert";
23
import { launchBrowser } from "../../../src/browser/standalone";
34
import { BROWSER_CONFIG, BROWSER_NAME } from "./constants";
@@ -96,6 +97,46 @@ type AutomationProtocol = typeof DEVTOOLS_PROTOCOL | typeof WEBDRIVER_PROTOCOL;
9697
assert.strictEqual(await status.getText(), "You are logged in");
9798
});
9899

100+
it("saveState: {keepFile: true}", async function () {
101+
await browser.saveState({
102+
keepFile: true,
103+
path: "./state.json",
104+
});
105+
106+
await browser.deleteSession();
107+
108+
const fileExist = fs.existsSync("./state.json");
109+
assert.strictEqual(fileExist, true);
110+
fs.rmSync("./state.json");
111+
});
112+
113+
it("saveState: {keepFile: false}", async function () {
114+
await browser.saveState({
115+
keepFile: false,
116+
path: "./state.json",
117+
});
118+
119+
await browser.deleteSession();
120+
121+
const fileExist = fs.existsSync("./state.json");
122+
assert.strictEqual(fileExist, false);
123+
});
124+
125+
it("saveState: emptyState", async function () {
126+
await browser.saveState({
127+
keepFile: true,
128+
cookieFilter: () => false,
129+
path: "./state.json",
130+
localStorage: false,
131+
sessionStorage: false,
132+
});
133+
134+
await browser.deleteSession();
135+
136+
const fileExist = fs.existsSync("./state.json");
137+
assert.strictEqual(fileExist, false);
138+
});
139+
99140
it("restoreState", async function () {
100141
if (loginState) {
101142
removeDomainFromCookies(loginState);

0 commit comments

Comments
 (0)