Skip to content

Commit 1341187

Browse files
committed
Merge pull request #155 from marco-c/automatically_download_and_execute_selenium_server
Automatically download and execute Selenium server
2 parents c55ba87 + 40f4256 commit 1341187

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ before_install:
1717
- nvm install $NODE_VERSION
1818
- export NODE=$(nvm which $NODE_VERSION)
1919
- export NPM=${NODE%node}npm
20-
- wget http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar
21-
- java -jar selenium-server-standalone-2.53.0.jar &> /dev/null &
2220
install:
2321
- $NPM install
2422
- $NPM install mocha

README.md

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,13 @@ The [Service Worker Cookbook](https://serviceworke.rs/) is full of Web Push exam
6363

6464
#### Prerequisites:
6565
* Java JDK or JRE (http://www.oracle.com/technetwork/java/javase/downloads/index.html)
66-
* Selenium Server 2.53.0 ([Download](http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar))
67-
68-
### Setup
69-
70-
* Run Selenium Server
71-
72-
e.g. in shell form:
73-
74-
```sh
75-
java -jar selenium-server-standalone-2.53.0.jar
76-
```
77-
78-
To run tests locally with Selenium:
7966

67+
To run tests:
8068
```sh
8169
npm test
8270
```
8371

8472
In order to make the tests run in Chromium/Chrome, you need a GCM API key and you need to define a GCM_API_KEY environment variable:
85-
```
73+
```sh
8674
GCM_API_KEY=your_API_key npm test
8775
```

test/selenium-init.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,18 @@ function downloadChromeDriver() {
279279
});
280280
}
281281

282+
// Download Selenium server
283+
284+
function downloadSeleniumServer() {
285+
return wget(destDir, 'https://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar');
286+
}
287+
282288
module.exports = {
283289
downloadFirefoxNightly: downloadFirefoxNightly,
284290
downloadFirefoxRelease: downloadFirefoxRelease,
285291
downloadFirefoxBeta: downloadFirefoxBeta,
286292
downloadFirefoxAurora: downloadFirefoxAurora,
287293
downloadChromiumNightly: downloadChromiumNightly,
288294
downloadChromeDriver: downloadChromeDriver,
295+
downloadSeleniumServer: downloadSeleniumServer,
289296
};

test/testSelenium.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var temp = require('temp').track();
66
var colors = require('colors');
77
var semver = require('semver');
88
var childProcess = require('child_process');
9+
var net = require('net');
910
var seleniumInit = require('./selenium-init');
1011
var createServer = require('./server');
1112
var webPush = require('../index.js');
@@ -24,6 +25,25 @@ if (!process.env.VAPID_PRIVATE_KEY || !process.env.VAPID_PUBLIC_KEY) {
2425

2526
process.env.PATH = process.env.PATH + ':test_tools/';
2627

28+
function isPortOpen(port) {
29+
return new Promise(function(resolve, reject) {
30+
var socket = new net.Socket();
31+
32+
socket.on('connect', function() {
33+
socket.end();
34+
resolve(true);
35+
});
36+
37+
socket.on('error', function() {
38+
resolve(false);
39+
});
40+
41+
socket.connect({
42+
port: port,
43+
});
44+
});
45+
}
46+
2747
suite('selenium', function() {
2848
var webdriver = require('selenium-webdriver');
2949
var firefox = require('selenium-webdriver/firefox');
@@ -140,6 +160,8 @@ suite('selenium', function() {
140160

141161
promises.push(seleniumInit.downloadFirefoxNightly());
142162

163+
promises.push(seleniumInit.downloadSeleniumServer());
164+
143165
if (process.env.GCM_API_KEY) {
144166
if (process.platform === 'linux') {
145167
chromeBinaryPath = 'test_tools/chrome-linux/chrome';
@@ -178,6 +200,29 @@ suite('selenium', function() {
178200
console.log('Using Chromium: ' + chromeBinaryPath);
179201
console.log('Version: ' + childProcess.execSync(chromeBinaryPath + ' --version').toString().replace('\n', ''));
180202
} catch (e) {}
203+
204+
if (!fs.existsSync('test_tools/selenium-server-standalone-2.53.0.jar')) {
205+
throw new Error('Selenium server doesn\'t exist.');
206+
}
207+
208+
childProcess.exec('java -jar test_tools/selenium-server-standalone-2.53.0.jar');
209+
210+
// Return a promise that resolved once the Selenium server is listening to the port 4444.
211+
return new Promise(function(resolve, reject) {
212+
var timerID = setInterval(function() {
213+
isPortOpen(4444)
214+
.then(function(isOpen) {
215+
if (isOpen) {
216+
clearInterval(timerID);
217+
resolve();
218+
}
219+
})
220+
.catch(function() {
221+
clearInterval(timerID);
222+
reject();
223+
});
224+
}, 1000);
225+
})
181226
});
182227
});
183228

0 commit comments

Comments
 (0)