-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathbrowser.ts
More file actions
205 lines (170 loc) · 6.14 KB
/
Copy pathbrowser.ts
File metadata and controls
205 lines (170 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import crypto from "crypto";
import { RequestOptions } from "https";
import _ from "lodash";
import { SAVE_HISTORY_MODE } from "../constants/config";
import { X_REQUEST_ID_DELIMITER } from "../constants/browser";
import * as history from "./history";
import { enhanceStacktraces } from "./stacktrace";
import { getBrowserCommands, getElementCommands } from "./history/commands";
import addRunStepCommand from "./commands/runStep";
import { Config } from "../config";
import { AsyncEmitter } from "../events";
import { BrowserConfig } from "../config/browser-config";
import type { Callstack } from "./history/callstack";
import type { WdProcess, WebdriverPool } from "../browser-pool/webdriver-pool";
const CUSTOM_SESSION_OPTS = [
"outputDir",
"agent",
"headers",
"transformRequest",
"transformResponse",
"strictSSL",
// cloud service opts
"user",
"key",
"region",
] as const;
export type BrowserOpts = {
id: string;
version?: string;
state?: Record<string, unknown>;
emitter: AsyncEmitter;
wdPool?: WebdriverPool;
};
export type BrowserState = {
testXReqId?: string;
traceparent?: string;
isBroken?: boolean;
};
export type CustomCommand = { name: string; elementScope: boolean };
export class Browser {
protected _emitter: AsyncEmitter;
protected _config: BrowserConfig;
protected _debug: boolean;
protected _session: WebdriverIO.Browser | null;
protected _callstackHistory: Callstack | null;
protected _state: BrowserState;
protected _customCommands: Set<CustomCommand>;
protected _wdPool?: WebdriverPool;
protected _wdProcess: WdProcess | null;
id: string;
version?: string;
static create<T extends Browser>(
this: new (config: Config, opts: BrowserOpts) => T,
config: Config,
opts: BrowserOpts,
): T {
return new this(config, opts);
}
constructor(config: Config, opts: BrowserOpts) {
this.id = opts.id;
this.version = opts.version;
this._config = config.forBrowser(this.id);
this._debug = config.system.debug;
this._session = null;
this._callstackHistory = null;
this._wdProcess = null;
this._state = {
...opts.state,
isBroken: false,
};
this._customCommands = new Set();
this._wdPool = opts.wdPool;
this._emitter = opts.emitter;
}
setHttpTimeout(timeout: number | null): void {
if (timeout === null) {
timeout = this._config.httpTimeout;
}
this._session!.extendOptions({ connectionRetryTimeout: timeout });
}
restoreHttpTimeout(): void {
this.setHttpTimeout(this._config.httpTimeout);
}
applyState(state: Record<string, unknown>): void {
_.extend(this._state, state);
}
protected _addCommands(): void {
this._addExtendOptionsMethod(this._session!);
}
protected _addSteps(): void {
addRunStepCommand(this);
}
protected _extendStacktrace(): void {
enhanceStacktraces(this._session!);
}
protected _addHistory(): void {
if (this._config.saveHistoryMode !== SAVE_HISTORY_MODE.NONE) {
this._callstackHistory = history.initCommandHistory(this._session as WebdriverIO.Browser, this._config);
}
}
protected _addExtendOptionsMethod(session: WebdriverIO.Browser): void {
session.addCommand("extendOptions", opts => {
_.extend(session.options, opts);
});
}
protected _getSessionOptsFromConfig(
optNames: ReadonlyArray<(typeof CUSTOM_SESSION_OPTS)[number]> = CUSTOM_SESSION_OPTS,
): Record<string, unknown> {
return optNames.reduce((options: Record<string, unknown>, optName) => {
if (optName === "transformRequest") {
options[optName] = (req: RequestOptions): RequestOptions => {
if (!_.isNull(this._config[optName])) {
req = this._config[optName](req);
}
if (!req.headers!["X-Request-ID"]) {
req.headers!["X-Request-ID"] = `${
this.state.testXReqId
}${X_REQUEST_ID_DELIMITER}${crypto.randomUUID()}`;
}
if (!req.headers!["traceparent"] && this.state.traceparent) {
req.headers!["traceparent"] = this.state.traceparent;
}
return req;
};
} else if (!_.isNull(this._config[optName as keyof BrowserConfig])) {
options[optName] = this._config[optName as keyof BrowserConfig];
}
return options;
}, {});
}
protected _startCollectingCustomCommands(): void {
const browserCommands = getBrowserCommands();
const elementCommands = getElementCommands();
this._session!.overwriteCommand("addCommand", (origCommand, name, wrapper, elementScope, ...rest) => {
const isKnownCommand = elementScope ? elementCommands.includes(name) : browserCommands.includes(name);
if (!isKnownCommand) {
this._customCommands.add({ name, elementScope: Boolean(elementScope) });
}
return origCommand(name, wrapper, elementScope, ...rest);
});
}
get fullId(): string {
return this.version ? `${this.id}.${this.version}` : this.id;
}
get publicAPI(): WebdriverIO.Browser {
return this._session!; // exposing webdriver API as is
}
get sessionId(): string {
return this.publicAPI.sessionId;
}
get config(): BrowserConfig {
return this._config;
}
get state(): BrowserState {
return this._state;
}
get capabilities(): WebdriverIO.Capabilities {
return this.publicAPI.capabilities;
}
get callstackHistory(): Callstack {
return this._callstackHistory!;
}
get customCommands(): CustomCommand[] {
const allCustomCommands = Array.from(this._customCommands);
return _.uniqWith(allCustomCommands, _.isEqual);
}
get emitter(): AsyncEmitter {
return this._emitter;
}
}