Skip to content

Commit bff1648

Browse files
committed
Merge pull request #162 from gauntface/chrome-notification-permission
Refactor of tests, Firefox + Chrome
2 parents 6292060 + 39c4f65 commit bff1648

17 files changed

+503
-426
lines changed

.travis.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
dist: trusty
12
os:
23
- linux
34
- osx
5+
addons:
6+
apt:
7+
sources:
8+
- google-chrome
9+
packages:
10+
- google-chrome-stable
11+
- google-chrome-beta
412
env:
513
global:
614
- DISPLAY=$(if [[ $TRAVIS_OS_NAME == "linux" ]]; then echo ':99.0'; fi)
@@ -23,6 +31,5 @@ install:
2331
before_script:
2432
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then sh -e /etc/init.d/xvfb start; fi
2533
script:
26-
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then sudo $NODE test/selenium-chrome-init.js; fi
2734
- $NPM test
2835
sudo: required

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@
4545
"mocha": "^2.4.5",
4646
"portfinder": "^1.0.2",
4747
"request": "^2.69.0",
48-
"selenium-webdriver": "~2.47.0",
48+
"selenium-webdriver": "~2.53.2",
4949
"semver": "^5.1.0",
50-
"temp": "^0.8.3"
50+
"temp": "^0.8.3",
51+
"which": "^1.2.9"
5152
},
5253
"bin": {
5354
"web-push": "bin/web-push.js"
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var webdriver = require('selenium-webdriver');
4+
var chrome = require('selenium-webdriver/chrome');
5+
var which = require('which');
6+
7+
var seleniumInit = require('../selenium-init');
8+
9+
function ChromeBrowsers() {
10+
var chromeStablePath;
11+
if (process.platform === 'linux') {
12+
chromeStablePath = which.sync('google-chrome');
13+
} else if (process.platform === 'darwin') {
14+
chromeStablePath = '/Applications/Google Chrome.app/' +
15+
'Contents/MacOS/Google Chrome';
16+
}
17+
18+
var chromeBetaPath;
19+
if (process.platform === 'linux') {
20+
chromeBetaPath = which.sync('google-chrome-beta');
21+
} else if (process.platform === 'darwin') {
22+
chromeBetaPath = '/Applications/Google Chrome Beta.app/' +
23+
'Contents/MacOS/Google Chrome Beta';
24+
}
25+
26+
var chromiumPath
27+
if (process.platform === 'linux') {
28+
chromiumPath = 'test_tools/chrome-linux/chrome';
29+
} else if (process.platform === 'darwin') {
30+
chromiumPath = 'test_tools/chrome-mac/Chromium.app/Contents/MacOS/Chromium';
31+
}
32+
33+
this.downloadBrowsers = function() {
34+
var promises = [];
35+
if (process.env.GCM_API_KEY) {
36+
promises.push(seleniumInit.downloadChromiumNightly());
37+
promises.push(seleniumInit.downloadChromeDriver());
38+
}
39+
return Promise.all(promises);
40+
}
41+
42+
this.getBrowserDriver = function(browserId, testServerURL) {
43+
var chromeBinaryPath;
44+
switch(browserId) {
45+
case 'chrome':
46+
chromeBinaryPath = chromeStablePath;
47+
break;
48+
case 'chrome-beta':
49+
chromeBinaryPath = chromeBetaPath;
50+
break;
51+
case 'chromium':
52+
chromeBinaryPath = chromiumPath;
53+
break;
54+
default:
55+
throw new Error('Unable to find browser with ID: ' + browserId);
56+
break;
57+
}
58+
59+
if (chromeBinaryPath) {
60+
chromeBinaryPath = path.resolve(chromeBinaryPath);
61+
}
62+
63+
if (!fs.existsSync(chromeBinaryPath)) {
64+
throw new Error('Chrome binary doesn\'t exist at ' + chromeBinaryPath + '. Use your installed Chrome binary by setting the CHROME environment'.bold.red);
65+
}
66+
67+
try {
68+
console.log('Using Chromium: ' + chromeBinaryPath);
69+
console.log('Version: ' + childProcess.execSync(chromeBinaryPath + ' --version').toString().replace('\n', ''));
70+
} catch (e) {}
71+
72+
73+
const chromePreferences = {
74+
profile: {
75+
content_settings: {
76+
exceptions: {
77+
notifications: {}
78+
}
79+
}
80+
}
81+
};
82+
chromePreferences.profile.content_settings.exceptions.notifications[testServerURL + ',*'] = {
83+
setting: 1
84+
};
85+
var chromeOptions = new chrome.Options()
86+
.setChromeBinaryPath(chromeBinaryPath)
87+
.setUserPreferences(chromePreferences);
88+
89+
return new webdriver.Builder()
90+
.forBrowser('chrome')
91+
.setChromeOptions(chromeOptions)
92+
.buildAsync();
93+
}
94+
}
95+
96+
module.exports = new ChromeBrowsers();
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var webdriver = require('selenium-webdriver');
4+
var firefox = require('selenium-webdriver/firefox');
5+
6+
var seleniumInit = require('../selenium-init');
7+
8+
function FirefoxBrowsers() {
9+
var firefoxStableBinaryPath;
10+
if (process.platform === 'linux') {
11+
firefoxStableBinaryPath = 'test_tools/stable/firefox/firefox-bin';
12+
} else if (process.platform === 'darwin') {
13+
firefoxStableBinaryPath = 'test_tools/stable/Firefox.app/Contents/MacOS/firefox-bin';
14+
}
15+
16+
var firefoxNightlyBinaryPath;
17+
if (process.platform === 'linux') {
18+
firefoxNightlyBinaryPath = 'test_tools/firefox/firefox-bin';
19+
} else if (process.platform === 'darwin') {
20+
firefoxNightlyBinaryPath = 'test_tools/FirefoxNightly.app/Contents/MacOS/firefox-bin';
21+
}
22+
23+
/*if (process.platform === 'linux') {
24+
firefoxAuroraBinaryPath = 'test_tools/aurora/firefox/firefox-bin';
25+
} else if (process.platform === 'darwin') {
26+
firefoxAuroraBinaryPath = 'test_tools/aurora/Firefox.app/Contents/MacOS/firefox-bin';
27+
}**/
28+
29+
/*if (process.platform === 'linux') {
30+
firefoxBetaBinaryPath = 'test_tools/beta/firefox/firefox-bin';
31+
} else if (process.platform === 'darwin') {
32+
firefoxBetaBinaryPath = 'test_tools/beta/Firefox.app/Contents/MacOS/firefox-bin';
33+
}**/
34+
35+
/*if (firefoxBetaBinaryPath && !fs.existsSync(firefoxBetaBinaryPath)) {
36+
throw new Error('Firefox binary doesn\'t exist at ' + firefoxBetaBinaryPath + '.'.bold.red);
37+
}*/
38+
39+
this.downloadBrowsers = function() {
40+
return seleniumInit.downloadFirefoxRelease();
41+
// promises.push(seleniumInit.downloadFirefoxBeta());
42+
// promises.push(seleniumInit.downloadFirefoxAurora());
43+
// promises.push(seleniumInit.downloadFirefoxNightly());
44+
}
45+
46+
this.getBrowserDriver = function(browserId) {
47+
var firefoxBinaryPath;
48+
switch(browserId) {
49+
case 'firefox':
50+
firefoxBinaryPath = firefoxStableBinaryPath;
51+
break
52+
case 'firefox-beta':
53+
firefoxBinaryPath = firefoxBetaBinaryPath;
54+
break;
55+
case 'firefox-aurora':
56+
firefoxBinaryPath = firefoxAuroraBinaryPath;
57+
process.env.SELENIUM_MARIONETTE = true;
58+
break;
59+
default:
60+
throw new Error('Unable to find browser with ID: ' + browserId);
61+
break;
62+
}
63+
64+
if (firefoxBinaryPath) {
65+
firefoxBinaryPath = path.resolve(firefoxBinaryPath);
66+
}
67+
68+
if (!fs.existsSync(firefoxBinaryPath)) {
69+
throw new Error('Firefox binary doesn\'t exist at ' + firefoxBinaryPath + '. Use your installed Firefox binary by setting the FIREFOX environment'.bold.red);
70+
}
71+
72+
try {
73+
console.log('Using Firefox: ' + firefoxBinaryPath);
74+
console.log('Version: ' + childProcess.execSync(firefoxBinaryPath + ' --version').toString().replace('\n', ''));
75+
} catch (e) {}
76+
77+
var profile = new firefox.Profile();
78+
profile.setPreference('security.turn_off_all_security_so_that_viruses_can_take_over_this_computer', true);
79+
profile.setPreference('extensions.checkCompatibility.nightly', false);
80+
// Only allow installation of third-party addons from the user's profile dir (needed to block the third-party
81+
// installation prompt for the Ubuntu Modifications addon on Ubuntu).
82+
profile.setPreference('extensions.enabledScopes', 1);
83+
//profile.setPreference('dom.push.debug', true);
84+
//profile.setPreference('browser.dom.window.dump.enabled', true);
85+
86+
var firefoxBinary = new firefox.Binary(firefoxBinaryPath);
87+
var firefoxOptions = new firefox.Options().setProfile(profile)
88+
.setBinary(firefoxBinary);
89+
90+
return new webdriver.Builder()
91+
.forBrowser('firefox')
92+
.setFirefoxOptions(firefoxOptions)
93+
.buildAsync();
94+
}
95+
}
96+
97+
module.exports = new FirefoxBrowsers();
File renamed without changes.
File renamed without changes.

test/data/demo/index.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8">
4+
<title>Web Push Demo</title>
5+
<link rel="manifest" href="manifest.json">
6+
</head>
7+
<body>
8+
<script type="text/javascript">
9+
(function() {
10+
if (!('serviceWorker' in navigator)) {
11+
return;
12+
}
13+
14+
navigator.serviceWorker.register('service-worker.js')
15+
.then(function(registration) {
16+
console.log('service worker registered');
17+
return navigator.serviceWorker.ready;
18+
})
19+
.then(function(reg) {
20+
var channel = new MessageChannel();
21+
channel.port1.onmessage = function(e) {
22+
window.document.title = e.data;
23+
}
24+
reg.active.postMessage('setup', [channel.port2]);
25+
26+
return reg.pushManager.subscribe({ userVisibleOnly: true });
27+
})
28+
.then(function(subscription) {
29+
var key = subscription.getKey ? subscription.getKey('p256dh') : '';
30+
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+
});
43+
});
44+
})();
45+
</script>
46+
</body>
47+
</html>
File renamed without changes.

0 commit comments

Comments
 (0)