forked from web-push-libs/web-push
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestSelenium.js
More file actions
226 lines (184 loc) · 6.91 KB
/
Copy pathtestSelenium.js
File metadata and controls
226 lines (184 loc) · 6.91 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var fse = require('fs-extra');
var temp = require('temp').track();
var colors = require('colors');
if (!process.env.GCM_API_KEY) {
console.log('You need to set the GCM_API_KEY env variable to run these tests.'.bold.red);
return;
}
var firefoxBinaryPath = process.env.FIREFOX;
if (!firefoxBinaryPath) {
if (process.platform === 'linux') {
firefoxBinaryPath = 'test_tools/firefox/firefox-bin';
} else if (process.platform === 'darwin') {
firefoxBinaryPath = 'test_tools/FirefoxNightly.app/Contents/MacOS/firefox-bin';
}
}
if (!fs.existsSync(firefoxBinaryPath)) {
throw new Error('Firefox binary doesn\'t exist at ' + firefoxBinaryPath + '. Use your installed Firefox binary by setting the FIREFOX environment'.bold.red);
}
var chromeBinaryPath = process.env.CHROME;
if (!chromeBinaryPath) {
if (process.platform === 'linux') {
chromeBinaryPath = 'test_tools/chrome-linux/chrome';
} else if (process.platform === 'darwin') {
chromeBinaryPath = 'test_tools/chrome-mac/Chromium.app/Contents/MacOS/Chromium';
}
}
if (!fs.existsSync(chromeBinaryPath)) {
throw new Error('Chrome binary doesn\'t exist at ' + chromeBinaryPath + '. Use your installed Chrome binary by setting the CHROME environment'.bold.red);
}
process.env.PATH = 'test_tools/:' + process.env.PATH;
var pageLoaded = false;
var clientRegistered = 0;
var createServer = require('../../demo/server');
var server;
function startServer(pushPayload, pushTimeout) {
server = createServer(pushPayload, pushTimeout ? pushTimeout : 0);
pageLoaded = false;
clientRegistered = 0;
server.onClientRegistered = function() {
pageLoaded = true;
clientRegistered++;
return clientRegistered > 1;
}
}
var webdriver = require('selenium-webdriver'),
By = require('selenium-webdriver').By,
until = require('selenium-webdriver').until;
var firefox = require('selenium-webdriver/firefox');
var chrome = require('selenium-webdriver/chrome');
var profilePath = temp.mkdirSync('marco');
var driver;
function startBrowser() {
var profile = new firefox.Profile(profilePath);
profile.acceptUntrustedCerts();
profile.setPreference('security.turn_off_all_security_so_that_viruses_can_take_over_this_computer', true);
//profile.setPreference('dom.push.debug', true);
//profile.setPreference('browser.dom.window.dump.enabled', true);
var firefoxBinary = new firefox.Binary(firefoxBinaryPath);
var firefoxOptions = new firefox.Options().setProfile(profile).setBinary(firefoxBinary);
var chromeOptions = new chrome.Options()
.setChromeBinaryPath(chromeBinaryPath)
.addArguments('--no-sandbox')
.addArguments('user-data-dir=' + profilePath);
var builder = new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(firefoxOptions)
.setChromeOptions(chromeOptions);
var driver = builder.build();
driver.wait(function() {
return server.listening;
});
driver.executeScript(function() {
if (typeof netscape !== 'undefined') {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
Components.utils.import('resource://gre/modules/Services.jsm');
var uri = Services.io.newURI('https://127.0.0.1:50005', null, null);
var principal = Services.scriptSecurityManager.getNoAppCodebasePrincipal(uri);
Services.perms.addFromPrincipal(principal, 'desktop-notification', Services.perms.ALLOW_ACTION);
}
});
/*
This currently doesn't work in Firefox Nightly.
driver.get('https://127.0.0.1:50005');
*/
driver.executeScript(function() {
window.location = 'https://127.0.0.1:50005';
});
driver.wait(function() {
return pageLoaded;
});
return driver;
}
function checkEnd(driver, done, pushPayload) {
driver.wait(until.titleIs(pushPayload ? pushPayload : 'no payload'), 60000).then(function() {
done();
});
}
function noRestartTest(browser, done, pushPayload, pushTimeout) {
process.env.SELENIUM_BROWSER = browser;
startServer(pushPayload, pushTimeout);
driver = startBrowser();
checkEnd(driver, done, pushPayload);
}
function restartTest(browser, done, pushPayload, pushTimeout) {
process.env.SELENIUM_BROWSER = browser;
startServer(pushPayload, pushTimeout);
driver = startBrowser();
function restart() {
console.log('Browser - Restart');
driver = startBrowser();
checkEnd(driver, done, pushPayload);
}
driver.close().then(function() {
console.log('Browser - Closed');
pageLoaded = false;
setTimeout(function() {
try {
// In Firefox, we need to copy the storage directory (because the PushDB is
// stored in an IndexedDB) and the prefs.js file, which contains a preference
// (dom.push.userAgentID) storing the User Agent ID.
// We need to wait a bit before copying these files because Firefox updates
// some of them when shutting down.
[ 'storage', 'prefs.js', 'serviceworker.txt' ].forEach(function(file) {
fse.copySync(path.join(driver.profilePath_, file), path.join(profilePath, file));
});
} catch (e) {
console.log('Error while copying: ' + e);
}
if (server.notificationSent) {
restart();
} else {
server.onNotificationSent = function() {
server.onNotificationSent = null;
restart();
};
}
}, 1000);
});
}
suite('selenium', function() {
this.timeout(0);
teardown(function(done) {
driver.quit().then(function() {
server.close(done);
});
});
test('send/receive notification without payload with Firefox', function(done) {
noRestartTest('firefox', done);
});
if (process.env.TRAVIS_OS_NAME !== 'osx') {
test('send/receive notification without payload with Chrome', function(done) {
noRestartTest('chrome', done);
});
}
test('send/receive notification with payload with Firefox', function(done) {
noRestartTest('firefox', done, 'marco');
});
/*
if (process.env.TRAVIS_OS_NAME !== 'osx') {
test('send/receive notification with payload with Chrome', function(done) {
noRestartTest('chrome', done, 'marco');
});
}*/
test('send/receive notification without payload with TTL with Firefox (closing and restarting the browser)', function(done) {
restartTest('firefox', done, undefined, 2);
});
if (process.env.TRAVIS_OS_NAME !== 'osx') {
test('send/receive notification without payload with TTL with Chrome (closing and restarting the browser)', function(done) {
restartTest('chrome', done, undefined, 2);
});
}
test('send/receive notification with payload with TTL with Firefox (closing and restarting the browser)', function(done) {
restartTest('firefox', done, 'marco', 2);
});
/*
if (process.env.TRAVIS_OS_NAME !== 'osx') {
test('send/receive notification with payload with TTL with Chrome (closing and restarting the browser)', function(done) {
restartTest('chrome', done, 'marco', 2);
});
}*/
});