1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var _ = require('lodash');
|
11 | var request = require('request');
|
12 |
|
13 | var browsers = require('./browsers');
|
14 | var sauce = require('./sauce');
|
15 |
|
16 |
|
17 | module.exports = function(wct, pluginOptions) {
|
18 |
|
19 |
|
20 |
|
21 | var eachCapabilities = [];
|
22 |
|
23 | wct.hook('configure', function(done) {
|
24 | if (!pluginOptions.browsers || pluginOptions.browsers.length === 0) return done();
|
25 |
|
26 | expandOptions(pluginOptions);
|
27 |
|
28 | browsers.expand(pluginOptions, function(error, expanded) {
|
29 | if (error) return done(error);
|
30 | wct.emit('log:debug', 'Using sauce browsers:', expanded);
|
31 |
|
32 |
|
33 |
|
34 | var activeBrowsers = wct.options.activeBrowsers;
|
35 | activeBrowsers.push.apply(activeBrowsers, expanded);
|
36 |
|
37 | eachCapabilities = expanded;
|
38 |
|
39 | done();
|
40 | });
|
41 | });
|
42 |
|
43 | wct.hook('prepare', function(done) {
|
44 |
|
45 |
|
46 | if (eachCapabilities.length === 0) return done();
|
47 |
|
48 |
|
49 | if (pluginOptions.tunnelId) {
|
50 | _injectTunnelId(eachCapabilities, pluginOptions.tunnelId);
|
51 | return done();
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 | wct.emitHook('prepare:sauce-tunnel', function(error) {
|
57 | if (error) return done(error);
|
58 | sauce.startTunnel(pluginOptions, wct, function(error, tunnelId) {
|
59 | if (error) return done(error);
|
60 | _injectTunnelId(eachCapabilities, tunnelId);
|
61 | done();
|
62 | });
|
63 | });
|
64 | });
|
65 |
|
66 | wct.on('browser-start', function(def, data, stats, browser) {
|
67 | if (!browser) return;
|
68 |
|
69 | browser._keepalive = setInterval(function(){
|
70 | browser.title(function() {});
|
71 | }, (def.testTimeout / 2) || 45 * 1000);
|
72 |
|
73 | browser._keepalive.unref();
|
74 | });
|
75 |
|
76 | wct.on('browser-end', function(def, error, stats, sessionId, browser) {
|
77 | if (eachCapabilities.length === 0 || !sessionId) return;
|
78 |
|
79 | if (browser._keepalive) {
|
80 | clearInterval(browser._keepalive);
|
81 | }
|
82 |
|
83 | var payload = {
|
84 | passed: (stats.status === 'complete' && stats.failing === 0),
|
85 | 'public': pluginOptions.visibility,
|
86 | build: parseInt(pluginOptions.buildNumber, 10),
|
87 | name: pluginOptions.jobName
|
88 | };
|
89 | wct.emit('log:debug', 'Updating sauce job', sessionId, payload);
|
90 |
|
91 |
|
92 | var username = wct.options.plugins.sauce.username;
|
93 | var accessKey = wct.options.plugins.sauce.accessKey;
|
94 | request.put({
|
95 | url: 'https://saucelabs.com/rest/v1/' + encodeURIComponent(username) + '/jobs/' + encodeURIComponent(sessionId),
|
96 | auth: {user: username, pass: accessKey},
|
97 | json: true,
|
98 | body: payload,
|
99 | });
|
100 | });
|
101 |
|
102 | };
|
103 |
|
104 | function expandOptions(options) {
|
105 | _.defaults(options, {
|
106 | username: process.env.SAUCE_USERNAME,
|
107 | accessKey: process.env.SAUCE_ACCESS_KEY,
|
108 | tunnelId: process.env.SAUCE_TUNNEL_ID,
|
109 |
|
110 |
|
111 | buildNumber: process.env.TRAVIS_BUILD_NUMBER,
|
112 | jobName: process.env.TRAVIS_REPO_SLUG,
|
113 | visibility: 'public'
|
114 | });
|
115 | if (sauce.isTravisSauceConnectRunning()) {
|
116 | _.defaults(options, {
|
117 |
|
118 | tunnelId: process.env.TRAVIS_JOB_NUMBER
|
119 | });
|
120 | }
|
121 | }
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function _injectTunnelId(eachCapabilities, tunnelId) {
|
128 | eachCapabilities.forEach(function(browser) {
|
129 | browser['tunnel-identifier'] = tunnelId;
|
130 | });
|
131 | }
|
132 |
|
133 |
|
134 | module.exports.expandOptions = expandOptions;
|
135 | module.exports.startTunnel = sauce.startTunnel;
|