Skip to content

Commit 15e0d17

Browse files
author
rocketraccoon
committed
fix: review fixes 3
1 parent 18d378f commit 15e0d17

File tree

7 files changed

+42
-12
lines changed

7 files changed

+42
-12
lines changed

src/browser/commands/restoreState/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from "fs-extra";
22

33
import { restoreStorage } from "./restoreStorage";
44

5-
import * as logger from "../../../utils/logger";
65
import type { Browser } from "../../types";
76
import { DEVTOOLS_PROTOCOL, WEBDRIVER_PROTOCOL } from "../../../constants/config";
87
import { getOverridesProtocol, getWebdriverFrames, SaveStateData } from "../saveState";
@@ -22,8 +21,7 @@ export default (browser: Browser): void => {
2221
const currentUrl = new URL(await session.getUrl());
2322

2423
if (!currentUrl.origin || currentUrl.origin === "null") {
25-
logger.error("Before restoreState first open page using url command");
26-
process.exit(1);
24+
throw new Error("Before restoreState first open page using url command");
2725
}
2826

2927
const options = { ...browser.config.stateOpts, refresh: true, ..._options };
@@ -35,8 +33,7 @@ export default (browser: Browser): void => {
3533
}
3634

3735
if (!restoreState) {
38-
logger.error("Can't restore state: please provide a path to file or data");
39-
process.exit(1);
36+
throw new Error("Can't restore state: please provide a path to file or data");
4037
}
4138

4239
if (restoreState?.cookies && options.cookieFilter) {

src/browser/commands/saveState/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Cookie } from "../../../types";
99
import type { Browser } from "../../types";
1010
import { MasterEvents } from "../../../events";
1111
import { StateOpts } from "../../../config/types";
12+
import { addGlobalFileToRemove } from "../../../globalFilesToRemove";
1213

1314
export type FrameData = StorageData;
1415

@@ -39,8 +40,7 @@ export default (browser: ExistingBrowser): void => {
3940
const currentUrl = new URL(await session.getUrl());
4041

4142
if (!currentUrl.origin || currentUrl.origin === "null") {
42-
logger.error("Before saveState first open page using url command");
43-
process.exit(1);
43+
throw new Error("Before saveState first open page using url command");
4444
}
4545

4646
const options = { ...browser.config.stateOpts, ..._options };
@@ -171,7 +171,7 @@ export default (browser: ExistingBrowser): void => {
171171

172172
if (options.keepFile) {
173173
logger.warn(
174-
"\x1b[31mPlease be aware that the file containing authorization data will not be automatically deleted after the tests are completed!!!\x1b[0m",
174+
"\x1b[31mOption keepFile in stateOpts now is true. Please be aware that the file containing authorization data will not be automatically deleted after the tests are completed!\x1b[0m",
175175
);
176176
} else {
177177
if (process.send) {
@@ -181,6 +181,8 @@ export default (browser: ExistingBrowser): void => {
181181
});
182182
}
183183

184+
addGlobalFileToRemove(options.path);
185+
184186
browser.emitter.emit(MasterEvents.ADD_FILE_TO_REMOVE, options.path);
185187
}
186188
}

src/browser/standalone/attachToBrowser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { AsyncEmitter, MasterEvents } from "../../events";
55
import { BrowserName, type W3CBrowserName, type SessionOptions } from "./../types";
66
import { getNormalizedBrowserName } from "../../utils/browser";
77
import fs from "fs-extra";
8+
import { useGlobalFilesToRemove } from "../../globalFilesToRemove";
89

910
export async function attachToBrowser(session: SessionOptions): Promise<WebdriverIO.Browser> {
1011
const browserName = session.sessionCaps?.browserName || BrowserName.CHROME;
@@ -62,7 +63,7 @@ export async function attachToBrowser(session: SessionOptions): Promise<Webdrive
6263
process.kill(session.driverPid, 9);
6364
}
6465

65-
if (filesToRemove.length > 0) {
66+
if (filesToRemove.length > 0 && !useGlobalFilesToRemove()) {
6667
await Promise.all(filesToRemove.map(path => fs.remove(path)));
6768
}
6869
});

src/browser/standalone/launchBrowser.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { LOCAL_GRID_URL } from "../../constants/config";
99
import { WebdriverPool } from "../../browser-pool/webdriver-pool";
1010
import type { StandaloneBrowserOptionsInput } from "./types";
1111
import fs from "fs-extra";
12+
import { useGlobalFilesToRemove } from "../../globalFilesToRemove";
1213

1314
const webdriverPool = new WebdriverPool();
1415

@@ -49,6 +50,7 @@ export async function launchBrowser(
4950
user: options.user,
5051
key: options.key,
5152
prepareBrowser: options.prepareBrowser,
53+
stateOpts: options.stateOpts,
5254
};
5355

5456
const filesToRemove: string[] = [];
@@ -105,7 +107,7 @@ export async function launchBrowser(
105107
await existingBrowser.quit();
106108
await newBrowser.kill();
107109

108-
if (filesToRemove.length > 0) {
110+
if (filesToRemove.length > 0 && !useGlobalFilesToRemove()) {
109111
await Promise.all(filesToRemove.map(path => fs.remove(path)));
110112
}
111113
});

src/browser/standalone/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type StandaloneBrowserOptions = Pick<
2020
| "user"
2121
| "key"
2222
| "system"
23+
| "stateOpts"
2324
>;
2425

2526
export type StandaloneBrowserOptionsInput = Partial<Omit<StandaloneBrowserOptions, "system">> & {

src/globalFilesToRemove.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const TESTPLANE_FILES_TO_REMOVE = Symbol.for("testplaneFilesToRemove");
2+
3+
type TestplanaGlobal = typeof globalThis & {
4+
[TESTPLANE_FILES_TO_REMOVE]?: string[];
5+
};
6+
7+
export const initGlobalFilesToRemove = (): void => {
8+
(global as TestplanaGlobal)[TESTPLANE_FILES_TO_REMOVE] = [];
9+
};
10+
11+
export const useGlobalFilesToRemove = (): boolean =>
12+
Array.isArray((global as TestplanaGlobal)[TESTPLANE_FILES_TO_REMOVE]);
13+
14+
export const getGlobalFilesToRemove = (): string[] => (global as TestplanaGlobal)[TESTPLANE_FILES_TO_REMOVE] || [];
15+
16+
export const addGlobalFileToRemove = (path: string): void => {
17+
const filesToRemove = (global as TestplanaGlobal)[TESTPLANE_FILES_TO_REMOVE];
18+
19+
if (filesToRemove) {
20+
filesToRemove.push(path);
21+
}
22+
};

src/testplane.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { preloadWebdriverIO } from "./utils/preload-utils";
2121
import { updateSelectivityHashes } from "./browser/cdp/selectivity";
2222
import { TagFilter } from "./utils/cli";
2323
import { ViteServer } from "./runner/browser-env/vite/server";
24+
import { getGlobalFilesToRemove, initGlobalFilesToRemove } from "./globalFilesToRemove";
2425

2526
interface RunOpts {
2627
browsers: string[];
@@ -178,6 +179,8 @@ export class Testplane extends BaseTestplane {
178179

179180
preloadWebdriverIO();
180181

182+
initGlobalFilesToRemove();
183+
181184
if (this.config.beforeAll) {
182185
await this.config.beforeAll.call({ config: this.config }, { config: this.config });
183186
}
@@ -200,8 +203,10 @@ export class Testplane extends BaseTestplane {
200203
await this.config.afterAll.call({ config: this.config }, { config: this.config });
201204
}
202205

203-
if (this._filesToRemove.length > 0) {
204-
await Promise.all(this._filesToRemove.map(path => fs.remove(path)));
206+
const filesToRemove = [...new Set([...this._filesToRemove, ...getGlobalFilesToRemove()])];
207+
208+
if (filesToRemove.length > 0) {
209+
await Promise.all(filesToRemove.map(path => fs.remove(path)));
205210
}
206211

207212
return !this.isFailed();

0 commit comments

Comments
 (0)