Skip to content

Commit d244f9f

Browse files
committed
Merge pull request #166 from gauntface/test-errors
Makes tests fail faster with helpful errors
2 parents a438ce7 + fcad6a6 commit d244f9f

File tree

6 files changed

+105
-63
lines changed

6 files changed

+105
-63
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ addons:
99
packages:
1010
- google-chrome-stable
1111
- google-chrome-beta
12+
- google-chrome-unstable
1213
env:
1314
global:
1415
- DISPLAY=$(if [[ $TRAVIS_OS_NAME == "linux" ]]; then echo ':99.0'; fi)

test/browser-managers/chrome-browsers.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,25 @@ function ChromeBrowsers() {
2323
'Contents/MacOS/Google Chrome Beta';
2424
}
2525

26+
var chromeUnstablePath;
27+
if (process.platform === 'linux') {
28+
chromeUnstablePath = which.sync('google-chrome-unstable');
29+
} else if (process.platform === 'darwin') {
30+
chromeUnstablePath = '/Applications/Google Chrome Canary.app/' +
31+
'Contents/MacOS/Google Chrome Canary';
32+
}
33+
2634
var chromiumPath
2735
if (process.platform === 'linux') {
2836
chromiumPath = 'test_tools/chrome-linux/chrome';
2937
} else if (process.platform === 'darwin') {
3038
chromiumPath = 'test_tools/chrome-mac/Chromium.app/Contents/MacOS/Chromium';
3139
}
3240

33-
this.downloadBrowsers = function() {
41+
this.downloadDependencies = function() {
3442
return Promise.all([
35-
seleniumInit.downloadChromiumNightly(),
36-
seleniumInit.downloadChromeDriver(),
43+
// seleniumInit.downloadChromiumNightly(),
44+
seleniumInit.downloadChromeDriver()
3745
]);
3846
}
3947

@@ -46,6 +54,9 @@ function ChromeBrowsers() {
4654
case 'chrome-beta':
4755
chromeBinaryPath = chromeBetaPath;
4856
break;
57+
case 'chrome-unstable':
58+
chromeBinaryPath = chromeUnstablePath;
59+
break;
4960
case 'chromium':
5061
chromeBinaryPath = chromiumPath;
5162
break;

test/browser-managers/firefox-browsers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function FirefoxBrowsers() {
3636
throw new Error('Firefox binary doesn\'t exist at ' + firefoxBetaBinaryPath + '.'.bold.red);
3737
}*/
3838

39-
this.downloadBrowsers = function() {
39+
this.downloadDependencies = function() {
4040
return seleniumInit.downloadFirefoxRelease();
4141
// promises.push(seleniumInit.downloadFirefoxBeta());
4242
// promises.push(seleniumInit.downloadFirefoxAurora());

test/data/demo/index.html

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
return;
1212
}
1313

14-
navigator.serviceWorker.register('service-worker.js')
14+
return navigator.serviceWorker.register('service-worker.js')
1515
.then(function(registration) {
1616
console.log('service worker registered');
1717
return navigator.serviceWorker.ready;
@@ -26,20 +26,18 @@
2626
return reg.pushManager.subscribe({ userVisibleOnly: true });
2727
})
2828
.then(function(subscription) {
29+
window.subscribeSuccess = true;
2930
var key = subscription.getKey ? subscription.getKey('p256dh') : '';
3031
var auth = subscription.getKey ? subscription.getKey('auth') : '';
31-
32-
return fetch('/', {
33-
method: 'post',
34-
headers: {
35-
'Content-type': 'application/json'
36-
},
37-
body: JSON.stringify({
38-
endpoint: subscription.endpoint,
39-
key: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : '',
40-
auth: auth ? btoa(String.fromCharCode.apply(null, new Uint8Array(auth))) : '',
41-
}),
42-
});
32+
window.testSubscription = {
33+
endpoint: subscription.endpoint,
34+
key: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : '',
35+
auth: auth ? btoa(String.fromCharCode.apply(null, new Uint8Array(auth))) : '',
36+
};
37+
})
38+
.catch(function(err) {
39+
window.subscribeSuccess = false;
40+
window.subscribeError = err;
4341
});
4442
})();
4543
</script>

test/helpers/create-server.js

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,6 @@ var portfinder = require('portfinder');
33
var fs = require('fs');
44
var path = require('path');
55

6-
function sendWebPush(webPush, obj, pushPayload, vapid) {
7-
// console.log('Push Application Server - Register: ' + obj.endpoint);
8-
// console.log('Push Application Server - Send notification to ' + obj.endpoint);
9-
10-
var promise;
11-
if (!pushPayload) {
12-
promise = webPush.sendNotification(obj.endpoint, {
13-
vapid: vapid,
14-
});
15-
} else {
16-
promise = webPush.sendNotification(obj.endpoint, {
17-
payload: pushPayload,
18-
userPublicKey: obj.key,
19-
userAuth: obj.auth,
20-
vapid: vapid,
21-
});
22-
}
23-
24-
promise
25-
.then(function() {
26-
//console.log('Push Application Server - Notification sent to ' + obj.endpoint);
27-
})
28-
.catch(function(error) {
29-
console.log('Push Application Server - Error in sending notification to ' + obj.endpoint);
30-
console.log(error);
31-
})
32-
}
33-
346
function createServer(options, webPush) {
357
var demoPath = 'test/data/demo';
368
var pushPayload = null;
@@ -62,22 +34,7 @@ function createServer(options, webPush) {
6234
});
6335
res.end(data);
6436
} else {
65-
var body = '';
66-
67-
req.on('data', function(chunk) {
68-
body += chunk;
69-
})
70-
71-
req.on('end', function() {
72-
sendWebPush(webPush, JSON.parse(body), pushPayload, vapid);
73-
});
74-
75-
res.writeHead(200, {
76-
'Content-Type': 'application/json',
77-
'Access-Control-Allow-Origin': '*',
78-
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
79-
});
80-
res.end('ok');
37+
throw new Error('Unable to handle post requests.');
8138
}
8239
} catch (err) {
8340
console.error('An error occured handling request.', err);

test/testSelenium.js

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var assert = require('assert');
12
var webPush = require('../index');
23
var createServer = require('./helpers/create-server');
34
var isPortOpen = require('./helpers/port-open');
@@ -31,8 +32,8 @@ suite('selenium', function() {
3132
this.timeout(0);
3233

3334
var promises = [];
34-
promises.push(firefoxBrowsers.downloadBrowsers());
35-
promises.push(chromeBrowsers.downloadBrowsers());
35+
promises.push(firefoxBrowsers.downloadDependencies());
36+
promises.push(chromeBrowsers.downloadDependencies());
3637

3738
return Promise.all(promises)
3839
.then(function() {
@@ -83,6 +84,14 @@ suite('selenium', function() {
8384
// Not the web driver promise of then and thenCatch
8485
return new Promise(function(resolve, reject) {
8586
globalDriver.get('http://127.0.0.1:' + globalServer.port)
87+
.then(function() {
88+
return globalDriver.executeScript(function() {
89+
return typeof navigator.serviceWorker !== 'undefined';
90+
});
91+
})
92+
.then(function(serviceWorkerSupported) {
93+
assert(serviceWorkerSupported);
94+
})
8695
.then(function() {
8796
return globalDriver.executeScript(function(port) {
8897
if (typeof netscape !== 'undefined') {
@@ -94,6 +103,72 @@ suite('selenium', function() {
94103
}
95104
}, globalServer.port);
96105
})
106+
.then(function() {
107+
return globalDriver.wait(function() {
108+
return globalDriver.executeScript(function() {
109+
return typeof window.subscribeSuccess !== 'undefined';
110+
});
111+
});
112+
})
113+
.then(function() {
114+
return globalDriver.executeScript(function() {
115+
if (!window.subscribeSuccess) {
116+
return window.subscribeError;
117+
}
118+
119+
return null;
120+
});
121+
})
122+
.then(function(subscribeError) {
123+
if (subscribeError) {
124+
throw subscribeError;
125+
}
126+
127+
return globalDriver.executeScript(function() {
128+
return window.testSubscription;
129+
});
130+
})
131+
.then(function(subscription) {
132+
if (!subscription) {
133+
throw new Error('No subscription found.');
134+
}
135+
136+
// console.log('Push Application Server - Register: ' + obj.endpoint);
137+
// console.log('Push Application Server - Send notification to ' + obj.endpoint);
138+
139+
var promise;
140+
var pushPayload = null;
141+
var vapid = null;
142+
if (options) {
143+
pushPayload = options.payload;
144+
vapid = options.vapid;
145+
}
146+
147+
if (!pushPayload) {
148+
promise = webPush.sendNotification(subscription.endpoint, {
149+
vapid: vapid,
150+
});
151+
} else {
152+
promise = webPush.sendNotification(subscription.endpoint, {
153+
payload: pushPayload,
154+
userPublicKey: subscription.key,
155+
userAuth: subscription.auth,
156+
vapid: vapid,
157+
});
158+
}
159+
160+
return promise
161+
.then(function(response) {
162+
if (response.length > 0) {
163+
var data = JSON.parse(response);
164+
if (typeof data.failure !== 'undefined' && data.failure > 0) {
165+
throw new Error('Bad GCM Response: ' + response);
166+
}
167+
}
168+
169+
//console.log('Push Application Server - Notification sent to ' + obj.endpoint);
170+
});
171+
})
97172
.then(function() {
98173
var expectedTitle = options.payload ? options.payload : 'no payload';
99174
return globalDriver.wait(webdriver.until.titleIs(expectedTitle, 60000));

0 commit comments

Comments
 (0)