-
Notifications
You must be signed in to change notification settings - Fork 62
/
browser.js
159 lines (129 loc) · 4.3 KB
/
browser.js
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
"use strict";
const crypto = require("crypto");
const _ = require("lodash");
const { SAVE_HISTORY_MODE } = require("../constants/config");
const { X_REQUEST_ID_DELIMITER } = require("../constants/browser");
const history = require("./history");
const stacktrace = require("./stacktrace");
const { getBrowserCommands, getElementCommands } = require("./history/commands");
const addRunStepCommand = require("./commands/runStep").default;
const CUSTOM_SESSION_OPTS = [
"outputDir",
"agent",
"headers",
"transformRequest",
"transformResponse",
"strictSSL",
// cloud service opts
"user",
"key",
"region",
];
module.exports = class Browser {
static create(config, opts) {
return new this(config, opts);
}
constructor(config, opts) {
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._state = {
...opts.state,
isBroken: false,
};
this._customCommands = new Set();
}
setHttpTimeout(timeout) {
if (timeout === null) {
timeout = this._config.httpTimeout;
}
this._session.extendOptions({ connectionRetryTimeout: timeout });
}
restoreHttpTimeout() {
this.setHttpTimeout(this._config.httpTimeout);
}
applyState(state) {
_.extend(this._state, state);
}
_addCommands() {
this._addExtendOptionsMethod(this._session);
}
_addSteps() {
addRunStepCommand(this);
}
_extendStacktrace() {
stacktrace.enhanceStacktraces(this._session);
}
_addHistory() {
if (this._config.saveHistoryMode !== SAVE_HISTORY_MODE.NONE) {
this._callstackHistory = history.initCommandHistory(this._session);
}
}
_addExtendOptionsMethod(session) {
session.addCommand("extendOptions", opts => {
_.extend(session.options, opts);
});
}
_getSessionOptsFromConfig(optNames = CUSTOM_SESSION_OPTS) {
return optNames.reduce((options, optName) => {
if (optName === "transformRequest") {
options[optName] = req => {
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])) {
options[optName] = this._config[optName];
}
return options;
}, {});
}
_startCollectingCustomCommands() {
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() {
return this.version ? `${this.id}.${this.version}` : this.id;
}
get publicAPI() {
return this._session; // exposing webdriver API as is
}
get sessionId() {
return this.publicAPI.sessionId;
}
get config() {
return this._config;
}
get state() {
return this._state;
}
get capabilities() {
return this.publicAPI.capabilities;
}
get callstackHistory() {
return this._callstackHistory;
}
get customCommands() {
const allCustomCommands = Array.from(this._customCommands);
return _.uniqWith(allCustomCommands, _.isEqual);
}
};