forked from canjs/canjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-sauce-labs.js
212 lines (171 loc) · 5.5 KB
/
test-sauce-labs.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
/* jshint esversion: 6 */
'use strict';
var series = require('async/series');
var webdriver = require('wd');
var SauceLabs = require('saucelabs');
// status of tests on all platforms
var allPlatformsPassed = true;
// amount of time between polling Sauce Labs Job (ms)
var statusPollingInterval = 10000;
// timeout if nothing happens in 300 seconds
var idleTimeout = 300;
// sauce labs account
var account = new SauceLabs({
username: process.env.SAUCE_USERNAME,
password: process.env.SAUCE_ACCESS_KEY
});
// https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities
var platforms = [{
browserName: 'firefox',
platform: 'Windows 10',
version: '49.0'
}, {
browserName: 'googlechrome',
platform: 'Windows 10'
}, {
browserName: 'safari',
platform: 'OS X 10.11',
version: '10.0'
}, {
browserName: 'internet explorer',
platform: 'Windows 10',
version: '11.0'
}, {
browserName: 'internet explorer',
platform: 'Windows 8',
version: '10.0'
}, {
browserName: 'internet explorer',
platform: 'Windows 7',
version: '9'
}, {
browserName: 'Safari',
'appium-version': '1.6.0',
platformName: 'iOS',
platformVersion: '10.0',
deviceName: 'iPhone 7 Simulator'
}];
// add properties to all platforms
platforms.forEach((platform) => {
var name = 'qunit tests - ';
name += platform.deviceName ? platform.deviceName + ' ' : '';
name += platform.platform ? platform.platform + ' ' : '';
name += platform.platformName ? platform.platformName + ' ' : '';
name += platform.platformVersion ? platform.platformVersion + ' ' : '';
name += platform.browserName ? platform.browserName + ' ' : '';
name += platform.version ? platform.version + ' ' : '';
Object.assign(platform, {
name: name,
// https://wiki.saucelabs.com/display/DOCS/Test+Configuration+Options#TestConfigurationOptions-MaximumTestDuration
maxDuration: 1800,// seconds, default 1800, max 10800
commandTimeout: 300,// seconds, default 300, max 600
idleTimeout: idleTimeout,// seconds, default 90, max 1000
// https://wiki.saucelabs.com/display/DOCS/Test+Configuration+Options#TestConfigurationOptions-BuildNumbers
build: process.env.TRAVIS_JOB_ID,
// make sure jobs use tunnel provied by sauce_connect
// https://wiki.saucelabs.com/display/DOCS/Test+Configuration+Options#TestConfigurationOptions-IdentifiedTunnels
tunnelIdentifier: process.env.TRAVIS_JOB_NUMBER
});
});
// array of test functions
var tests = [];
platforms.forEach((platform) => {
tests.push(makeTest(platform));
});
var url = `http://${process.env.SAUCE_USERNAME}:${process.env.SAUCE_ACCESS_KEY}@ondemand.saucelabs.com:80/wd/hub`;
var driver = webdriver.remote(url);
series(tests, () => {
console.log(`All tests completed with status ${allPlatformsPassed}`);
driver.quit(() => {
process.exit(allPlatformsPassed ? 0 : 1);
});
});
// return a function that will run tests on a given platform
function makeTest(platform) {
return function(cb) {
var url = 'http://localhost:3000/test/index.html?hidepassed';
var jobTimeoutId, initTimeoutId;
console.log(`Running ${platform.name}`);
var testComplete = function(status) {
if (jobTimeoutId) {
clearTimeout(jobTimeoutId);
}
// update status of this platform's tests
driver.sauceJobStatus(status);
// close the browser
driver.quit();
// update status of all tests - process.exit status
allPlatformsPassed = allPlatformsPassed && status;
// don't fail the job so that tests will run on other platforms
cb(null);
};
var initKeepAlive = function() {
// add indicator that driver is being initialized to keep Travis from timing out
process.stdout.write('>');
initTimeoutId = setTimeout(initKeepAlive, statusPollingInterval);
};
initKeepAlive();
driver.init(platform, (err, sessionId) => {
if (err) {
console.log(`Error calling driver.init: ${err}`);
testComplete(false);
return;
}
console.log(`\nSauce Labs Job: https://saucelabs.com/jobs/${sessionId}`);
if (initTimeoutId) {
clearTimeout(initTimeoutId);
}
var pollSauceLabsStatus = function() {
account.showJob(sessionId, (err, job) => {
if (err) {
console.log(`\nError calling account.showJob: ${err}`);
return;
}
if (job.error) {
console.log(`\nJob Error: ${job.error}`);
testComplete(false);
return;
}
// add indicator that tests are running to keep Travis from timing out
process.stdout.write('.');
jobTimeoutId = setTimeout(pollSauceLabsStatus, statusPollingInterval);
});
};
console.log(`Opening: ${url}`);
driver.get(url);
var getElementText = function(selector) {
var timeout = idleTimeout * 1000;
var pollingFrequency = 2000;
return function(callback) {
driver
.waitForElementsByCssSelector(selector, timeout, pollingFrequency, (err, el) => {
if (err) {
return callback(err);
}
driver.text(el, (err, text) => {
callback(err ? err : null, text);
});
});
};
};
var checkTestResults = function() {
series([
getElementText('#qunit-testresult .passed'),
getElementText('#qunit-testresult .failed'),
getElementText('#qunit-testresult .total')
], (err, [passed, failed, total]) => {
if (err) {
console.log(`\nError checking test results: ${err}`);
testComplete(false);
return;
}
var allTestsPassed = (passed === total && failed === "0");
console.log(`\nPassed: ${allTestsPassed} (${passed} / ${total})\n`);
testComplete(allTestsPassed);
});
};
pollSauceLabsStatus();
checkTestResults();
});
};
}