This repository has been archived by the owner on Oct 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
136 lines (105 loc) · 3.1 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
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
module.exports = WebdriverjsAngular;
var webdriverjs = require('webdriverio/lib/webdriverio');
var inherits = require('util').inherits;
var browserScripts = require('./browser-scripts.js');
function WebdriverjsAngular(options) {
webdriverjs.apply(this, arguments);
var client = this;
var originalUrl = client.url.bind(client);
patch('init', addTimeout);
client.addCommand('url', function(url, cb) {
if (typeof url === 'function') {
return originalUrl.apply(this, arguments);
}
originalUrl('about:blank')
.execute(function() {
var url = arguments[0];
window.name = 'NG_DEFER_BOOTSTRAP!' + window.name;
window.location.assign(url);
}, [url]);
waitForLoad(function(err) {
if (err) {
return cb(err);
}
client
.executeAsync(browserScripts.testForAngular, [20], function(err, res) {
if (err) {
return cb(err)
}
client.execute(function() {
angular.resumeBootstrap();
}, [], cb);
});
});
function waitForLoad(cb) {
var timeout;
var cancelLoading = setTimeout(function hasTimeout() {
timeout = true;
cb(new Error('Timeout while waiting for page to load'));
}, 10 * 1000);
hasLoaded(function checkForLoad(err, res) {
if (timeout === true) {
return;
}
if (err) {
clearTimeout(cancelLoading);
return cb(err);
}
if (res === true) {
clearTimeout(cancelLoading);
return cb(null)
}
hasLoaded(checkForLoad);
});
function hasLoaded(cb) {
originalUrl(function(err, url) {
if (url === 'about:blank') {
return cb(err, false);
}
cb(err, true)
});
}
}
});
[ 'element', 'elements', 'title'].forEach(waitForAngularBefore);
['url', 'elementIdClick'].forEach(waitForAngularAfter);
function waitForAngularBefore (method) {
var original = client[method];
client.addCommand(method, function(){
var originalArgs = arguments;
waitForAngular(function() {
original.apply(client, originalArgs);
});
});
}
function waitForAngularAfter (method) {
patch(method, waitForAngular);
}
function patch(method, patchFn) {
var original = client[method];
client.addCommand(method, function() {
var originalArgs = Array.prototype.slice.call(arguments);
var cb = originalArgs.pop();
originalArgs.push(function() {
var responseArgs = arguments;
patchFn(function() {
cb.apply(client, responseArgs);
});
});
original.apply(client, originalArgs);
});
}
function waitForAngular(cb) {
client.executeAsync(
browserScripts.waitForAngular,
[options.ngRoot],
cb);
}
function addTimeout(cb) {
client.timeouts('script', 10 * 1000, cb);
}
}
inherits(WebdriverjsAngular, webdriverjs);
WebdriverjsAngular.remote = function WebdriverjsAngularRemote(options) {
return require('webdriverio').remote(options, WebdriverjsAngular);
}