This repository was archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathnew-browser.js
330 lines (270 loc) Β· 9.96 KB
/
new-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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
'use strict';
const debug = require('debug');
const chalk = require('chalk');
const _ = require('lodash');
const Promise = require('bluebird');
const striptags = require('striptags');
const Browser = require('./browser');
const ClientBridge = require('./client-bridge');
const GeminiError = require('../errors/gemini-error');
const WdErrors = require('../constants/wd-errors');
const OPERA_NOT_SUPPORTED = 'Not supported in OperaDriver yet';
module.exports = class NewBrowser extends Browser {
constructor(config) {
super(config);
this.log = debug(`gemini:browser: ${this.id}`);
const wdLog = debug(`gemini:webdriver: ${this.id}`);
this._wd.on('connection', (code, message) => wdLog(`Error: code ${code}, ${message}`));
this._wd.on('status', (info) => wdLog(info));
this._wd.on('command', (eventType, command, response) => {
if (eventType === 'RESPONSE' && command === 'takeScreenshot()') {
response = '<binary-data>';
}
if (typeof response !== 'string') {
response = JSON.stringify(response);
}
wdLog(chalk.cyan(eventType), command, chalk.grey(response || ''));
});
this._exposeWdApi([
'sleep',
'waitForElementByCssSelector',
'waitFor',
'moveTo',
'click',
'doubleclick',
'buttonDown',
'buttonUp',
'keys',
'type',
'tapElement',
'flick',
'execute',
'setWindowSize',
'getWindowSize',
// 'setOrientation' and 'getOrientation' work only in context 'NATIVE_APP' with 'appium' below 1.5.x
{name: 'getOrientation', context: 'NATIVE_APP'},
{name: 'setOrientation', context: 'NATIVE_APP'}
]);
}
_exposeWdApi(methods) {
methods
.map((method) => _.isPlainObject(method) ? method : {name: method})
.forEach((method) => this[method.name] = this._exposeWdMethod(method));
}
_exposeWdMethod(method) {
return function() {
return method.context
? this._applyWdMethodInContext(method.name, method.context, arguments)
: this._applyWdMethod(method.name, arguments);
};
}
_applyWdMethodInContext(method, context, args) {
return this._wd.currentContext()
.then((originalContext) => {
return this._wd.context(context)
.then(() => this._applyWdMethod(method, args))
.finally(() => this._wd.context(originalContext));
});
}
_applyWdMethod(method, args) {
return this._wd[method].apply(this._wd, args);
}
launch(calibrator) {
return this.initSession()
.then(() => this._setDefaultSize())
.then(() => this._setDefaultOrientation())
.then(() => {
// maximize is required, because default
// windows size in phantomjs can prevent
// some shadows from fitting in
if (this._shouldMaximize()) {
return this._maximize();
}
})
.then(() => {
if (!this.config.calibrate || this._calibration) {
return;
}
return calibrator.calibrate(this)
.then((calibration) => this._setCalibration(calibration));
})
.then(() => this.buildScripts())
.then(() => this.chooseLocator())
.catch((e) => {
if (e.code === 'ECONNREFUSED') {
return Promise.reject(new GeminiError(
`Unable to connect to ${this.config.gridUrl}.`,
'Make sure that URL in config file is correct and selenium\nserver is running.'
));
}
const error = new GeminiError(`Cannot launch browser ${this.id}:\n${e.message}.`);
if (e.data) {
error.message += '\nReason: ' + this._parseHtmlBody(e.data);
}
error.browserId = this.id;
error.sessionId = this.sessionId;
// selenium does not provide a way to distinguish different reasons of failure
return Promise.reject(error);
});
}
_parseHtmlBody(html) {
const body = html.match(/<body[\s\S]*<\/body>/i);
if (!body) {
return html;
}
const rawText = striptags(body[0]);
return rawText
? (rawText).replace(/\s+/g, ' ').trim()
: html;
}
_setSessionTimeout(timeout) {
if (_.isNull(timeout)) {
timeout = this.config.httpTimeout;
}
return this._configureHttp(timeout);
}
initSession() {
return this._setSessionTimeout(this.config.sessionRequestTimeout)
.then(() => this._wd.init(this.capabilities))
.spread((sessionId) => {
this.sessionId = sessionId;
this.log('launched session %o', this);
})
.then(() => this._setHttpTimeout());
}
_setDefaultSize() {
const size = this.config.windowSize;
if (!size) {
return;
}
return this._wd.setWindowSize(size.width, size.height)
.catch((e) => {
// Its the only reliable way to detect not supported operation
// in legacy operadriver.
const message = _.get(e, 'cause.value.message');
if (message === OPERA_NOT_SUPPORTED) {
console.warn(chalk.yellow('WARNING!'));
console.warn('Legacy Opera Driver does not support window resizing');
console.warn('windowSize setting will be ignored.');
return;
}
return Promise.reject(e);
});
}
_setDefaultOrientation() {
const orientation = this.config.orientation;
if (!orientation) {
return;
}
return this._wd.setOrientation(orientation);
}
openRelative(relativeURL) {
return this.open(this.config.getAbsoluteUrl(relativeURL), {resetZoom: true});
}
// Zoom reset should be skipped before calibration cause we're unable to build client scripts before
// calibration done. Reset will be executed as 1 of calibration steps.
open(url, params) {
params = _.defaults(params || {}, {
resetZoom: false
});
return this._wd.get(url)
.then((url) => {
return params.resetZoom
? this._clientBridge.call('resetZoom').then(() => url)
: url;
});
}
injectScript(script) {
return this._wd.execute(script);
}
evalScript(script) {
return this._wd.eval(script);
}
buildScripts() {
return ClientBridge.build(this, {calibration: this._calibration, coverage: this._coverageEnabled, supportDeprecated: true})
.then((clientBridge) => this._clientBridge = clientBridge);
}
get _coverageEnabled() {
return this.config.system.coverage.enabled;
}
get _needsCompatLib() {
return this._calibration && this._calibration.needsCompatLib;
}
chooseLocator() {
this.findElement = this._needsCompatLib ? this._findElementScript : this._findElementWd;
}
reset() {
// We can't use findElement here because it requires page with body tag
return this.evalScript('document.body')
.then(body => this._wd.moveTo(body, 0, 0))
.catch(e => {
return Promise.reject(_.extend(e || {}, {
browserId: this.id,
sessionId: this.sessionId
}));
});
}
get browserName() {
return this.capabilities.browserName;
}
get version() {
return this.capabilities.version;
}
get capabilities() {
return this.config.desiredCapabilities;
}
_shouldMaximize() {
if (this.config.windowSize) {
return false;
}
return this.browserName === 'phantomjs';
}
_maximize() {
return this._wd.windowHandle()
.then((handle) => this._wd.maximize(handle));
}
findElement() {
throw new Error('findElement is called before appropriate locator is chosen');
}
_findElementWd(selector) {
return this._wd.elementByCssSelector(selector)
.catch((error) => {
if (error.status === WdErrors.ELEMENT_NOT_FOUND) {
error.selector = selector;
}
return Promise.reject(error);
});
}
_findElementScript(selector) {
return this._clientBridge.call('queryFirst', [selector])
.then((element) => {
if (element) {
return element;
}
const error = new Error('Unable to find element');
error.status = WdErrors.ELEMENT_NOT_FOUND;
error.selector = selector;
return Promise.reject(error);
});
}
prepareScreenshot(selectors, opts) {
opts = _.extend(opts, {
usePixelRatio: this._calibration ? this._calibration.usePixelRatio : true,
coverage: this._coverageEnabled
});
return this._clientBridge.call('prepareScreenshot', [selectors, opts]);
}
quit() {
if (!this.sessionId) {
return Promise.resolve();
}
return this._setSessionTimeout(this.config.sessionQuitTimeout)
.then(() => this._wd.quit())
.then(() => this.log('kill browser %o', this))
.then(() => this._setHttpTimeout())
.catch((err) => this.log(err));
}
inspect() {
return `[${this.id} (${this.sessionId})]`;
}
};