Skip to content

Commit c941e04

Browse files
Merge pull request #193 from browserstack/lookout-add-pid-cli-param
Sanitizing #180
2 parents a59408d + 9495f60 commit c941e04

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,13 @@ To run browser tests on BrowserStack infrastructure, you need to create a `brows
168168
* `key`: BrowserStack [access key](https://www.browserstack.com/accounts/local-testing) (Or `BROWSERSTACK_KEY` environment variable)
169169
* `test_path`: Path to the test page which will run the tests when opened in a browser.
170170
* `test_framework`: Specify test framework which will run the tests. Currently supporting qunit, jasmine, jasmine1.3.1, jasmine2 and mocha.
171+
* `test_server_port`: Specify test server port that will be opened from BrowserStack. If not set the default port 8888 will be used. Find a [list of all supported ports on browerstack.com](https://www.browserstack.com/question/664).
171172
* `timeout`: Specify worker timeout with BrowserStack.
172173
* `browsers`: A list of browsers on which tests are to be run. Find a [list of all supported browsers and platforms on browerstack.com](https://www.browserstack.com/list-of-browsers-and-platforms?product=js_testing).
173174
* `build`: A string to identify your test run in Browserstack. In `TRAVIS` setup `TRAVIS_COMMIT` will be the default identifier.
174175
* `proxy`: Specify a proxy to use for the local tunnel. Object with `host`, `port`, `username` and `password` properties.
175176
* `exit_with_fail`: If set to true the cli process will exit with fail if any of the tests failed. Useful for automatic build systems.
177+
* `tunnel_pid_file`: Specify a path to file to save the tunnel process id into. Can also by specified using the `--pid` flag while launching browserstack-runner from the command line.
176178

177179
A sample configuration file:
178180

@@ -182,6 +184,7 @@ A sample configuration file:
182184
"key": "<access key>",
183185
"test_framework": "qunit|jasmine|jasmine2|mocha",
184186
"test_path": ["relative/path/to/test/page1", "relative/path/to/test/page2"],
187+
"test_server_port": "8899",
185188
"browsers": [
186189
{
187190
"browser": "ie",

bin/cli.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ var Log = require('../lib/logger'),
99
tunnel = require('tunnel'),
1010
http = require('http'),
1111
ConfigParser = require('../lib/configParser').ConfigParser,
12-
serverPort = 8888,
1312
config,
1413
server,
1514
timeout,
@@ -106,7 +105,7 @@ function getTestBrowserInfo(browserString, path) {
106105
}
107106

108107
function buildTestUrl(test_path, worker_key, browser_string) {
109-
var url = 'http://localhost:' + serverPort + '/' + test_path;
108+
var url = 'http://localhost:' + config.test_server_port + '/' + test_path;
110109

111110
var querystring = qs.stringify({
112111
_worker_key: worker_key,
@@ -119,11 +118,11 @@ function buildTestUrl(test_path, worker_key, browser_string) {
119118
}
120119

121120
function launchServer(config, callback) {
122-
logger.trace('launchServer:', serverPort);
123-
logger.debug('Launching server on port:', serverPort);
121+
logger.trace('launchServer:', config.test_server_port);
122+
logger.debug('Launching server on port:', config.test_server_port);
124123

125124
server = new Server(client, workers, config, callback);
126-
server.listen(parseInt(serverPort, 10));
125+
server.listen(parseInt(config.test_server_port, 10));
127126
}
128127

129128
function launchBrowser(browser, path) {
@@ -382,7 +381,7 @@ function runTests(config, callback) {
382381
launchServer(config, runTestsCallback);
383382

384383
logger.trace('runTests: creating tunnel');
385-
tunnel = new Tunnel(config.key, serverPort, config.tunnelIdentifier, config, function (err) {
384+
tunnel = new Tunnel(config.key, config.test_server_port, config.tunnelIdentifier, config, function (err) {
386385
if(err) {
387386
cleanUpAndExit(null, err, [], callback);
388387
} else {

bin/runner.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var todo = process.argv[2],
44
path = require('path'),
55
config;
66

7-
if (todo === '--verbose') {
7+
if (process.argv.indexOf('--verbose') !== -1) {
88
global.logLevel = process.env.LOG_LEVEL || 'debug';
99
} else {
1010
global.logLevel = 'info';
@@ -36,6 +36,18 @@ try {
3636
}
3737
}
3838

39+
// extract a path to file to store tunnel pid
40+
var pid = process.argv.find(function (param) { return param.indexOf('--pid') !== -1; });
41+
42+
if (pid) {
43+
var extracted_path = /--pid=([\w\/\.-]+)/g.exec(pid);
44+
if (extracted_path) {
45+
config.tunnel_pid_file = extracted_path[1];
46+
} else {
47+
console.error('Error while parsing flag --pid. Usage: --pid=/path/to/file');
48+
}
49+
}
50+
3951
var runner = require('./cli.js');
4052
runner.run(config, function(err) {
4153
if(err) {

lib/config.js

+4
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,8 @@ exports.config = function(config) {
8181
for (var key in config) {
8282
this[key] = config[key];
8383
}
84+
85+
if (!this.test_server_port) {
86+
this.test_server_port = 8888;
87+
}
8488
};

lib/local.js

+11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ var Log = require('./logger'),
22
logger = new Log(global.logLevel || 'info'),
33
exec = require('child_process').execFile,
44
fs = require('fs'),
5+
path = require('path'),
56
https = require('https'),
7+
utils = require('./utils'),
68
windows = ((process.platform.match(/win32/) || process.platform.match(/win64/)) !== null),
79
localBinary = __dirname + '/BrowserStackLocal' + (windows ? '.exe' : '');
810

@@ -55,6 +57,11 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, config, callback) {
5557
}
5658
});
5759

60+
if (config.tunnel_pid_file) {
61+
utils.mkdirp(path.dirname(config.tunnel_pid_file));
62+
fs.writeFile(config.tunnel_pid_file, subProcess.pid);
63+
}
64+
5865
that.process = subProcess;
5966
}
6067

@@ -99,6 +106,10 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, config, callback) {
99106
subProcess = null;
100107
} catch (e) {
101108
logger.debug('[%s] failed to kill process:', new Date(), e);
109+
} finally {
110+
if (config.tunnel_pid_file) {
111+
fs.unlink(config.tunnel_pid_file, function () {});
112+
}
102113
}
103114
}
104115

lib/utils.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
var fs = require('fs'),
2+
path = require('path');
3+
14
String.prototype.escapeSpecialChars = function() {
25
return this.replace(/\n/g, '\\n')
36
.replace(/\r/g, '\\r')
@@ -52,8 +55,19 @@ var createTestsFailedError = function createTestsFailedError(config) {
5255
return error;
5356
};
5457

58+
var mkdirp = function mkdirp(filepath) {
59+
var dirname = path.dirname(filepath);
60+
if (!fs.existsSync(dirname)) {
61+
mkdirp(dirname);
62+
}
63+
if (!fs.existsSync(filepath)) {
64+
fs.mkdirSync(filepath);
65+
}
66+
};
67+
5568
exports.titleCase = titleCase;
5669
exports.uuid = uuid;
5770
exports.browserString = browserString;
5871
exports.objectSize = objectSize;
5972
exports.createTestsFailedError = createTestsFailedError;
73+
exports.mkdirp = mkdirp;

0 commit comments

Comments
 (0)