forked from lgandecki/sync-webdriverio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (64 loc) · 2.38 KB
/
index.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
// A wrapped webdriverio with synchronous API using fibers.
var webdriverio = require('webdriverio');
var _ = require('underscore');
var fs = require('fs');
var path = require('path');
var Fiber = require('fibers');
var Future = require('fibers/future');
var Promise = require('meteor-promise');
Promise.Fiber = Fiber;
var wrapAsync = require('xolvio-fiber-utils').wrapAsync;
var wrapAsyncObject = require('xolvio-fiber-utils').wrapAsyncObject;
var wrapAsyncForWebdriver = function (fn, context) {
return wrapAsync(fn, context, {supportCallback: false});
};
var webdriverioWithSync = _.clone(webdriverio);
webdriverioWithSync.wrapAsync = wrapAsync;
webdriverioWithSync.wrapAsyncObject = wrapAsyncObject;
webdriverioWithSync.remote = function (options) {
var syncByDefault = !(options && options.sync === false);
var remote = webdriverio.remote.apply(webdriverio, arguments);
// Run condition function in fiber
var waitUntil = remote.waitUntil;
remote.waitUntil = function (condition/*, arguments */) {
arguments[0] = Promise.async(condition);
return waitUntil.apply(remote, arguments);
};
// Wrap async all core commands
var webdriverioPath = path.dirname(require.resolve('webdriverio'));
var commandNames = _.chain(['protocol', 'commands'])
.map(function(commandType) {
var dir = path.resolve(webdriverioPath, path.join('lib', commandType));
var files = fs.readdirSync(dir);
return files.map(function(filename) {
return filename.slice(0, -3);
});
})
.flatten(true)
.uniq()
.value();
var remoteWrapper = wrapAsyncObject(remote, commandNames, {
syncByDefault: syncByDefault,
wrapAsync: wrapAsyncForWebdriver
});
// Wrap async added commands
var addCommand = remote.addCommand;
remote.addCommand = function (fnName, fn, forceOverwrite) {
var result = addCommand.call(remote, fnName, Promise.async(fn), forceOverwrite);
var commandWrapper = wrapAsyncObject(remote, [fnName], {
syncByDefault: syncByDefault,
wrapAsync: wrapAsyncForWebdriver
});
_.defaults(remoteWrapper, commandWrapper);
return result;
};
_.forEach([
'addCommand',
'transferPromiseness',
'on', 'once', 'emit', 'removeListener', 'removeAllListeners'
], function (methodName) {
remoteWrapper[methodName] = remote[methodName].bind(remote);
});
return remoteWrapper;
};
module.exports = webdriverioWithSync;