forked from ziolko/wdio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwdio.js
126 lines (106 loc) · 3.65 KB
/
wdio.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
const selenium = require('selenium-standalone');
const webdriverio = require('webdriverio');
const Future = require('fibers/future');
const Fiber = require('fibers');
const tcpPortUsed = require('tcp-port-used');
function getAsyncCommandWrapper(fn) {
return function (arg1, arg2, arg3, arg4, arg5) {
if (!Fiber.current) {
throw new Error('It seems you\'ve forgotten to wrap a call to webdriver.io method into w wdio.wrap. For details see\n' +
'https://github.com/ziolko/wdio#errors-description');
}
return Future.fromPromise(fn.call(this, arg1, arg2, arg3, arg4, arg5)).wait();
}
}
function getWaitUntilCommandWrapper(fn) {
return getAsyncCommandWrapper(function (condition, ms, interval) {
return fn.call(this, function () {
return new Promise(function (resolve, reject) {
Fiber(function () {
try {
resolve(condition());
} catch (error) {
reject(error)
}
}).run();
});
}, ms, interval);
});
}
exports.getBrowser = function getBrowser(options) {
const instance = webdriverio.remote(options);
const SYNC_COMMANDS = ['domain', '_events', '_maxListeners', 'setMaxListeners', 'emit',
'addListener', 'on', 'once', 'removeListener', 'removeAllListeners', 'listeners',
'getMaxListeners', 'listenerCount'];
const SPECIAL_COMMANDS = ['waitUntil'];
Object.keys(Object.getPrototypeOf(instance)).forEach(function (commandName) {
if (SYNC_COMMANDS.indexOf(commandName) === -1 && SPECIAL_COMMANDS.indexOf(commandName) === -1) {
instance[commandName] = getAsyncCommandWrapper(instance[commandName]);
}
});
instance.waitUntil = getWaitUntilCommandWrapper(instance.waitUntil);
instance.addCommand = function (name, code) {
instance[name] = code;
};
return instance;
};
exports.wrap = function wrap(code) {
return function (callback) {
if (!callback) {
const message = 'No callback for the wdio.wrap provided. For details see\n' +
'https://github.com/ziolko/wdio#errors-description';
throw new Error(message)
}
const self = this;
Fiber(function () {
try {
code.call(self);
callback();
} catch (error) {
if (callback.fail) {
callback.fail(error)
} else {
callback(error);
}
}
}).run();
}
};
exports.run = function (code, callback) {
if (!callback) {
const message = 'No callback for the wdio.run provided. For details see\n' +
'https://github.com/ziolko/wdio#errors-description';
throw new Error(message)
}
exports.wrap(code)(callback);
};
exports.initSelenium = function (options, done) {
const SELENIUM_PORT = 4444;
if (typeof options === 'function') {
done = options;
options = {};
}
// Don't create another selenium instance if one is already running
// Developer can run selenium on his own so that tests run slightly faster
tcpPortUsed.check(SELENIUM_PORT)
.then(function (isPortInUse) {
if (isPortInUse) {
done();
} else {
installAndStartSelenium();
}
});
function installAndStartSelenium() {
const installOptions = Object.assign({ version: '3.4.0' }, options.install);
const startOptions = Object.assign({ version: '3.4.0' }, options.start);
selenium.install(installOptions, function (err) {
if (err) return done(err);
selenium.start(startOptions, function (err, process) {
done(err, process);
});
})
}
};
exports.endSelenium = function (process) {
if (process) process.kill();
};