-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (55 loc) · 1.7 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
/**
* wd-deasync
*
* A wrapper around wd.js to make all Webdriver and Element methods
* synchronous, using deasync. Adapted loosely from node-wd-sync.
*/
// jshint node:true
'use strict';
var wd = require('wd');
var deasync = require('deasync');
var events = require('events');
function wrapObject(target) {
var wrapped = {};
wrapped.wdOriginal = target; // preserve original
var fname, f, isAsyncMethod;
for (fname in target) {
f = target[fname];
if (typeof f !== 'function') continue;
// deasync the same methods that would be promisified with wd's promise
// chaining interface (promise-webdriver.js:14-20):
isAsyncMethod =
!/^newElement$|^toJSON$|^toString$|^_/.test(fname) &&
!events.EventEmitter.prototype[fname];
if (isAsyncMethod)
f = deasync(f);
wrapped[fname] = wrapMethod(target, f);
}
return wrapped;
}
function wrapMethod(target, f) {
return function () {
var fresult = f.apply(target, arguments);
// make the returned object's methods synchronous too:
if (isElement(fresult)) {
fresult = wrapObject(fresult);
} else if (Array.isArray(fresult)) {
fresult = fresult.map(function (v) { return isElement(v) ? wrapObject(v) : v; });
}
return fresult;
};
}
function isElement(obj) {
return obj instanceof wd.Element;
}
var wdd = {
remote: function () {
return wrapObject(wd.remote.apply(wd, arguments));
},
SPECIAL_KEYS: wd.SPECIAL_KEYS,
asserters: wd.asserters,
TouchAction: wd.TouchAction,
MultiAction: wd.MultiAction,
wd: wd
};
module.exports = wdd;