-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
138 lines (118 loc) · 2.9 KB
/
test.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
var async = require('async'),
wd = require('wd'),
sauceConnect = require('sauce-connect-launcher'),
TapProducer = require('tap').Producer,
runner = require('./');
var testSrc = [
'(function() {',
'console.log("TAP version 13");',
'console.log("# beep");',
'console.log("ok 1 should be equal");',
'console.log("ok 2 should be equal");',
'console.log("");',
'console.log("1..2");',
'console.log("# tests 2");',
'console.log("# pass 2");',
'console.log("");',
'console.log("# ok");',
'})();'
].join('\r\n');
var user = process.env.SAUCE_USER,
key = process.env.SAUCE_KEY;
if (!user || !key) {
console.log('Set SAUCE_USER and SAUCE_KEY to your SauceLabs credentials');
process.exit(1);
}
var browser = wd.remote('ondemand.saucelabs.com', 80, user, key),
id = 'test-' + Date.now(),
tunnel,
success = false;
run();
function run() {
async.series([
startTunnel,
startBrowser,
test,
stop
], complete);
}
function stop(callback) {
async.parallel([
stopTunnel,
stopBrowser
], callback);
}
function startTunnel(callback) {
console.log('Opening tunnel to SauceLabs');
var options = {
username: user,
accessKey: key,
tunnelIdentifier: id
};
sauceConnect(options, function(err, t) {
if (err) {
callback(err);
}
tunnel = t;
console.log('Tunnel opened');
callback();
});
}
function stopTunnel(callback) {
console.log('Closing tunnel');
tunnel.close(function() {
console.log('Tunnel closed');
callback();
});
}
function startBrowser(callback) {
console.log('Starting browser');
browser.init({
browserName: 'chrome',
'tunnel-identifier': id,
name: 'Test browser'
}, function(err) {
if (err) {
return callback(err);
}
console.log('Started browser');
callback();
});
}
function stopBrowser(callback) {
console.log('Stopping browser');
browser.quit(function() {
console.log('Stopped browser');
callback();
});
}
function test(callback) {
runner(testSrc, browser, function(err, results) {
if (err) {
console.error(err);
return callback();
}
var output = TapProducer.encode(results);
console.log('TAP Results:');
console.log(output);
if (!results.ok) {
console.error('Tests did not pass');
return callback();
}
success = true;
callback();
});
}
function complete(err) {
if (err) {
console.error('Error occurred');
console.error(err);
process.exit(1);
}
if (success) {
console.log('Success!');
} else {
console.log('Tests failed');
process.exit(1);
}
}