-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
webdriverio.js
402 lines (334 loc) · 12.8 KB
/
webdriverio.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
'use strict';
var q = require('q'),
fs = require('fs'),
path = require('path'),
merge = require('deepmerge'),
EventEmitter = require('events').EventEmitter,
safeExecute = require('./utils/safeExecute'),
RequestHandler = require('./utils/RequestHandler'),
ErrorHandler = require('./utils/ErrorHandler'),
Logger = require('./utils/Logger'),
sanitize = require('./helpers/sanitize'),
isMobileHelper = require('./helpers/isMobile'),
detectSeleniumBackend = require('./helpers/detectSeleniumBackend');
/**
* WebdriverIO v3
*/
module.exports = function WebdriverIO(args, modifier) {
var prototype = Object.create(Object.prototype),
eventHandler = new EventEmitter(),
internalEvents = ['init', 'command', 'error', 'result', 'end'],
fulFilledPromise = q(),
stacktrace = [],
commandList = [];
/**
* merge default options with given user options
*/
var options = merge({
protocol: 'http',
waitforTimeout: 500,
waitforInterval: 250,
coloredLogs: true,
logLevel: 'silent',
baseUrl: null
}, typeof args !== 'string' ? args : {});
/**
* define Selenium backend given on user options
*/
options = merge(detectSeleniumBackend(args), options);
/**
* only set globals we wouldn't get otherwise
*/
if(!process.env.WEBDRIVERIO_COLORED_LOGS) {
process.env.WEBDRIVERIO_COLORED_LOGS = options.coloredLogs;
}
var logger = new Logger(options, eventHandler),
requestHandler = new RequestHandler(options, eventHandler, logger);
/**
* assign instance to existing session
*/
if(typeof args === 'string') {
requestHandler.sessionID = args;
}
var desiredCapabilities = merge({
browserName: 'firefox',
version: '',
javascriptEnabled: true,
locationContextEnabled: true,
handlesAlerts: true,
rotatable: true,
platform: 'ANY'
}, options.desiredCapabilities || {});
/**
* set default logging prefs to enable log commands (mainly for chromedriver)
*/
if(typeof desiredCapabilities.loggingPrefs === 'undefined') {
desiredCapabilities.loggingPrefs = {
browser: 'ALL',
driver: 'ALL'
};
}
var isMobile = isMobileHelper(desiredCapabilities);
var resolve = function(result, isErrorHandled) {
if(typeof result === 'function') {
this.isExecuted = true;
result = result.call(this);
}
var resolveMethod = result instanceof Error ? 'reject' : 'resolve';
this.defer[resolveMethod](result);
/**
* By using finally in our next method we omit the duty to throw an exception an some
* point. To avoid propagating rejected promises until everything crashes silently we
* check if the last and current promise got rejected. If so we can throw the error.
*/
if(this.promise.isRejected() && !isErrorHandled) {
/**
* take screenshot only if screenshotPath is given
*/
if(typeof options.screenshotPath !== 'string') {
return throwException(result, stacktrace);
}
var screenshotPath = path.join(process.cwd(), options.screenshotPath);
/**
* take screenshot only if directory exists
*/
if(!fs.existsSync(screenshotPath)) {
return throwException(result, stacktrace);
}
var client = unit();
client.next(prototype.saveScreenshot, [
path.join(screenshotPath, 'ERROR_' + sanitize.caps(desiredCapabilities) + '_' + new Date().toJSON() + '.png')
], 'saveScreenshot');
var stack = stacktrace.slice();
return throwException.bind(null, result, stack);
}
return this.promise;
};
function throwException(e, stack) {
stack = stack.slice(0, -1).map(function(trace) {
return ' at ' + trace;
});
e.stack = e.type + ': ' + e.message + '\n' + stack.reverse().join('\n');
throw e;
}
/**
* WebdriverIO Monad
*/
function unit(lastPromise) {
var client = Object.create(prototype),
defer = q.defer(),
promise = defer.promise;
client.defer = defer;
client.promise = promise;
client.lastPromise = lastPromise || fulFilledPromise;
client.desiredCapabilities = desiredCapabilities;
client.requestHandler = requestHandler;
client.logger = logger;
client.options = options;
client.isMobile = isMobile;
client.commandList = commandList;
/**
* actual bind function
*/
client.next = function (func, args, name) {
var self = this;
/**
* use finally to propagate rejected promises up the chain
*/
return self.lastPromise.then(function() {
/**
* store command into command list so `getHistory` can return it
*/
commandList.push({
name: name,
args: args
});
return resolve.call(self, safeExecute(func, args));
}, function(e) {
/**
* reject pending commands in chain
*/
if(e.isPropagatedError) {
return self.defer.reject(e);
}
self.emit('error', {
message: e.message,
type: e.type,
stack: stacktrace
});
/**
* mark error as propagated so that error messages get only printed once
*/
e.isPropagatedError = true;
logger.printException(e.type || 'Error', e.message, stacktrace);
self.defer.reject(e);
});
};
client.finally = function(fn) {
var client = unit(this.promise.finally(function() {
return resolve.call(client, safeExecute(fn, []).bind(this));
}.bind(this)));
return client;
};
client.call = function(fn) {
var client = unit(this.promise.done(function() {
return resolve.call(client, safeExecute(fn, []).bind(this));
}.bind(this)));
return client;
};
client.then = function(onFulfilled, onRejected) {
var self = this;
if(typeof onFulfilled !== 'function' && typeof onRejected !== 'function') {
return this;
}
/**
* execute then function in context of the new instance
* but resolve result with this
*/
var client = unit(this.promise.then(function() {
return resolve.call(client, safeExecute(onFulfilled, arguments).bind(self));
}, function() {
return resolve.call(client, safeExecute(onRejected, arguments).bind(self), typeof onRejected === 'function');
}));
return client;
};
client.catch = function(onRejected) {
return this.then(undefined, onRejected);
};
client.inspect = function() {
return this.promise.inspect();
};
/**
* internal helper method to handle command results
*
* @param {Promise[]} promises list of promises
* @param {Boolean} option if true extract value property from selenium result
*/
client.unify = function(promises, option) {
option = option || {};
promises = Array.isArray(promises) ? promises : [promises];
return q.all(promises)
/**
* extract value property from result if desired
*/
.then(function(result) {
if(!option.extractValue || !Array.isArray(result)) {
return result;
}
return result.map(function(res) {
return res.value && typeof res.value === 'string' ? res.value.trim() : res.value;
});
/**
* sanitize result for better assertion
*/
}).then(function(result) {
if(Array.isArray(result) && result.length === 1) {
result = result[0];
}
if(option.lowercase && typeof result === 'string') {
result = result.toLowerCase();
}
return result;
});
};
client.addCommand = function(fnName, fn, forceOverwrite) {
if(client[fnName] && !forceOverwrite) {
throw new ErrorHandler.RuntimeError('Command "' + fnName + '" is already defined!');
}
return unit.lift.apply(null, arguments);
};
client.transferPromiseness = function(target, promise) {
/**
* transfer WebdriverIO commands
*/
var clientFunctions = Object.keys(prototype);
var promiseFunctions = ['then', 'catch', 'finally'];
var functionsToTranfer = clientFunctions.concat(promiseFunctions);
functionsToTranfer.forEach(function(fnName) {
if(typeof promise[fnName] === 'function') {
target[fnName] = promise[fnName].bind(promise);
}
});
};
if (typeof modifier === 'function') {
client = modifier(client, options);
}
return client;
}
/**
* enhance base monad prototype with methods
*/
unit.lift = function (name, func) {
prototype[name] = function () {
var nextPromise = this.promise,
args = Array.prototype.slice.apply(arguments),
callback;
/**
* commands executed inside commands don't have to wait
* on any promise
*/
if(this.isExecuted) {
nextPromise = this.lastPromise;
}
var client = unit(nextPromise);
/**
* catch stack to find information about where the command that causes
* the error was used (stack line 2) and only save it when it was not
* within WebdriverIO context
*/
var stack = new Error().stack,
lineInTest = stack.split('\n').slice(2, 3).join('\n'),
fileAndPosition = lineInTest.slice(lineInTest.indexOf('(') + 1, lineInTest.indexOf(')')),
atCommand = lineInTest.trim().slice(3).split(' ')[0];
atCommand = atCommand.slice(atCommand.lastIndexOf('.') + 1);
var trace = name + '(' + sanitize.args(args) + ') - ' + fileAndPosition.slice(fileAndPosition.lastIndexOf('/') + 1);
if(Object.keys(prototype).indexOf(atCommand) === -1 && atCommand !== 'exports') {
stacktrace = [trace];
} else {
/**
* save trace for nested commands
*/
stacktrace.push(trace);
}
/**
* queue command
*/
client.next(func, args, name, trace);
/**
* ensure backwards compatibility
* care about callbacks as last parameter for all commands
* but execute(Async) and selectExecute(Async)
*/
if(!name.match(/(selector)*(E|e)xecute(Async)*/) && !name.match(/waitUntil/) && typeof args[args.length - 1] === 'function') {
callback = args.pop();
return client.then(function(res) {
return callback.call(this, undefined, res);
}, function(err) {
return callback.call(this, err);
});
}
return client;
};
return unit;
};
/**
* register event emitter
*/
Object.keys(Object.getPrototypeOf(eventHandler)).forEach(function(eventCommand) {
prototype[eventCommand] = function() {
var args = Array.prototype.slice.apply(arguments);
/**
* custom commands needs to get emitted and registered in order
* to prevent race conditions
*/
if(internalEvents.indexOf(args[0]) === -1) {
return this.finally(function() {
eventHandler[eventCommand].apply(eventHandler, args);
});
}
eventHandler[eventCommand].apply(eventHandler, args);
return this;
};
});
return unit;
};