Skip to content

Commit 37396e1

Browse files
authored
fix(testplane): fix save/restore state
1 parent 94856b0 commit 37396e1

File tree

9 files changed

+142
-213
lines changed

9 files changed

+142
-213
lines changed

package-lock.json

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@
101101
"png-validator": "1.1.0",
102102
"recast": "0.23.6",
103103
"resolve.exports": "2.0.2",
104-
"set-cookie-parser": "2.7.1",
105104
"sizzle": "2.3.6",
106105
"socket.io": "4.7.5",
107106
"socket.io-client": "4.7.5",

src/browser/commands/restoreState/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { Browser } from "../../types";
77
import { DEVTOOLS_PROTOCOL, WEBDRIVER_PROTOCOL } from "../../../constants/config";
88
import {
99
defaultOptions,
10-
getCalculatedProtocol,
10+
getOverridesProtocol,
1111
getWebdriverFrames,
1212
SaveStateData,
1313
SaveStateOptions,
@@ -48,7 +48,7 @@ export default (browser: Browser): void => {
4848
restoreState.cookies = restoreState?.cookies.filter(options.cookieFilter);
4949
}
5050

51-
switch (getCalculatedProtocol(browser)) {
51+
switch (getOverridesProtocol(browser)) {
5252
case WEBDRIVER_PROTOCOL: {
5353
await session.switchToParentFrame();
5454

src/browser/commands/saveState/index.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import fs from "fs-extra";
22

33
import _ from "lodash";
4-
import type { Browser } from "../../types";
54
import { dumpStorage, StorageData } from "./dumpStorage";
65
import { DEVTOOLS_PROTOCOL, WEBDRIVER_PROTOCOL } from "../../../constants/config";
7-
import { isSupportIsolation } from "../../../utils/browser";
86
import { ExistingBrowser, getActivePuppeteerPage } from "../../existing-browser";
97
import * as logger from "../../../utils/logger";
108
import { Cookie } from "../../../types";
9+
import type { Browser } from "../../types";
1110

1211
export type SaveStateOptions = {
1312
path?: string;
@@ -32,23 +31,13 @@ export const defaultOptions = {
3231
sessionStorage: true,
3332
};
3433

35-
export const getCalculatedProtocol = (browser: Browser): typeof DEVTOOLS_PROTOCOL | typeof WEBDRIVER_PROTOCOL => {
36-
const protocol = browser.config.automationProtocol;
37-
38-
if (protocol === DEVTOOLS_PROTOCOL) {
39-
return DEVTOOLS_PROTOCOL;
40-
}
41-
42-
if (
43-
protocol === WEBDRIVER_PROTOCOL &&
44-
browser.config.isolation &&
45-
isSupportIsolation(browser.publicAPI.capabilities.browserName!, browser.publicAPI.capabilities.browserVersion!)
46-
) {
47-
return DEVTOOLS_PROTOCOL;
48-
}
49-
50-
return protocol;
51-
};
34+
// in case when we use webdriver protocol, bidi and isolation
35+
// we have to force change protocol to devtools, for use puppeteer,
36+
// because we use it for create incognito window
37+
export const getOverridesProtocol = (browser: Browser): typeof WEBDRIVER_PROTOCOL | typeof DEVTOOLS_PROTOCOL =>
38+
browser.config.automationProtocol === WEBDRIVER_PROTOCOL && browser.publicAPI.isBidi && browser.config.isolation
39+
? DEVTOOLS_PROTOCOL
40+
: browser.config.automationProtocol;
5241

5342
export const getWebdriverFrames = async (session: WebdriverIO.Browser): Promise<string[]> =>
5443
session.execute<string[], []>(() =>
@@ -74,10 +63,10 @@ export default (browser: ExistingBrowser): void => {
7463
framesData: {},
7564
};
7665

77-
switch (getCalculatedProtocol(browser)) {
66+
switch (getOverridesProtocol(browser)) {
7867
case WEBDRIVER_PROTOCOL: {
7968
if (options.cookies) {
80-
const cookies = await session.getCookies();
69+
const cookies = await session.getAllCookies();
8170

8271
data.cookies = cookies.map(
8372
cookie =>
@@ -133,7 +122,7 @@ export default (browser: ExistingBrowser): void => {
133122
}
134123
case DEVTOOLS_PROTOCOL: {
135124
if (options.cookies) {
136-
const cookies = await browser.getAllRequestsCookies();
125+
const cookies = await session.getAllCookies();
137126

138127
data.cookies = cookies.map(
139128
cookie =>

src/browser/existing-browser.ts

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import url from "url";
22
import _ from "lodash";
3-
import { parse as parseCookiesString, Cookie } from "set-cookie-parser";
43
import { attach, type AttachOptions, type ElementArray } from "@testplane/webdriverio";
54
import { sessionEnvironmentDetector } from "@testplane/wdio-utils";
65
import { Browser, BrowserOpts } from "./browser";
@@ -9,7 +8,7 @@ import { Camera, PageMeta } from "./camera";
98
import { type ClientBridge, build as buildClientBridge } from "./client-bridge";
109
import * as history from "./history";
1110
import * as logger from "../utils/logger";
12-
import { DEVTOOLS_PROTOCOL, WEBDRIVER_PROTOCOL } from "../constants/config";
11+
import { WEBDRIVER_PROTOCOL } from "../constants/config";
1312
import { MIN_CHROME_VERSION_SUPPORT_ISOLATION } from "../constants/browser";
1413
import { isSupportIsolation } from "../utils/browser";
1514
import { isRunInNodeJsEnv } from "../utils/config";
@@ -19,8 +18,6 @@ import type { CalibrationResult, Calibrator } from "./calibrator";
1918
import { NEW_ISSUE_LINK } from "../constants/help";
2019
import { runWithoutHistory } from "./history";
2120
import type { SessionOptions } from "./types";
22-
import { Protocol } from "devtools-protocol";
23-
import { getCalculatedProtocol } from "./commands/saveState";
2421
import { Page } from "puppeteer-core";
2522
import { CDP } from "./cdp";
2623
import type { ElementReference } from "@testplane/wdio-protocols";
@@ -94,7 +91,6 @@ export class ExistingBrowser extends Browser {
9491
protected _calibration?: CalibrationResult;
9592
protected _clientBridge?: ClientBridge;
9693
protected _cdp: CDP | null = null;
97-
private _allCookies: Map<string, Protocol.Network.CookieParam> = new Map();
9894

9995
constructor(config: Config, opts: BrowserOpts) {
10096
super(config, opts);
@@ -134,10 +130,6 @@ export class ExistingBrowser extends Browser {
134130

135131
await isolationPromise;
136132

137-
if (getCalculatedProtocol(this) === DEVTOOLS_PROTOCOL) {
138-
await this.startCollectCookies();
139-
}
140-
141133
this._callstackHistory?.clear();
142134

143135
try {
@@ -155,72 +147,6 @@ export class ExistingBrowser extends Browser {
155147
return this;
156148
}
157149

158-
getCookieIndex(cookie: Cookie): string {
159-
return [cookie.name, cookie.domain, cookie.path].join("-");
160-
}
161-
162-
async getAllRequestsCookies(): Promise<Array<Protocol.Network.CookieParam>> {
163-
if (this._session) {
164-
const cookies = await this._session.getAllCookies();
165-
166-
if (cookies) {
167-
cookies.forEach(cookie => {
168-
this._allCookies.set(this.getCookieIndex(cookie), cookie as Protocol.Network.CookieParam);
169-
});
170-
}
171-
}
172-
173-
return [...this._allCookies.values()].map(cookie => ({
174-
name: cookie.name,
175-
value: cookie.value,
176-
domain: cookie.domain,
177-
path: cookie.path,
178-
expires: cookie.expires ? cookie.expires : undefined,
179-
httpOnly: cookie.httpOnly,
180-
secure: cookie.secure,
181-
sameSite: cookie.sameSite,
182-
}));
183-
}
184-
185-
async startCollectCookies(): Promise<void> {
186-
if (!this._session) {
187-
return;
188-
}
189-
190-
this._allCookies = new Map();
191-
192-
const page = await getActivePuppeteerPage(this._session);
193-
194-
if (!page) {
195-
return;
196-
}
197-
198-
page.on("response", async res => {
199-
try {
200-
const headers = res.headers();
201-
202-
if (headers["set-cookie"]) {
203-
headers["set-cookie"].split("\n").forEach(str => {
204-
parseCookiesString(str, { map: false }).forEach((cookie: Cookie) => {
205-
const index = this.getCookieIndex(cookie);
206-
const expires = cookie.expires
207-
? Math.floor(new Date(cookie.expires).getTime() / 1000)
208-
: undefined;
209-
210-
this._allCookies.set(index, {
211-
...cookie,
212-
domain: cookie.domain ?? new URL(res.url()).hostname,
213-
expires,
214-
} as Protocol.Network.CookieParam);
215-
});
216-
});
217-
}
218-
} catch (err) {
219-
console.error(err);
220-
}
221-
});
222-
}
223-
224150
markAsBroken(): void {
225151
if (this.state.isBroken) {
226152
return;

src/browser/standalone/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type StandaloneBrowserOptions = Pick<
55
| "automationProtocol"
66
| "desiredCapabilities"
77
| "gridUrl"
8+
| "isolation"
89
| "baseUrl"
910
| "httpTimeout"
1011
| "pageLoadTimeout"

src/testplane.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ export class Testplane extends BaseTestplane {
209209
keepBrowserMode,
210210
runnableOpts,
211211
});
212+
212213
const collection = TestCollection.create(specs);
213214

214215
collection.getBrowsers().forEach(bro => {

test/integration/standalone/constants.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import _ from "lodash";
2-
import { BrowserName } from "../../../src/browser/types";
32

4-
export const BROWSER_NAME = (process.env.BROWSER || "chrome").toLowerCase() as keyof typeof BrowserName;
3+
export const BROWSER_NAME = (process.env.BROWSER || "chrome").toLowerCase();
54

65
export const BROWSER_CONFIG = {
76
desiredCapabilities: {

0 commit comments

Comments
 (0)