Skip to content

Commit

Permalink
Merge pull request #193 from browserstack/lookout-add-pid-cli-param
Browse files Browse the repository at this point in the history
Sanitizing #180
  • Loading branch information
pulkitsharma07 authored Dec 7, 2017
2 parents a59408d + 9495f60 commit c941e04
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,13 @@ To run browser tests on BrowserStack infrastructure, you need to create a `brows
* `key`: BrowserStack [access key](https://www.browserstack.com/accounts/local-testing) (Or `BROWSERSTACK_KEY` environment variable)
* `test_path`: Path to the test page which will run the tests when opened in a browser.
* `test_framework`: Specify test framework which will run the tests. Currently supporting qunit, jasmine, jasmine1.3.1, jasmine2 and mocha.
* `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).
* `timeout`: Specify worker timeout with BrowserStack.
* `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).
* `build`: A string to identify your test run in Browserstack. In `TRAVIS` setup `TRAVIS_COMMIT` will be the default identifier.
* `proxy`: Specify a proxy to use for the local tunnel. Object with `host`, `port`, `username` and `password` properties.
* `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.
* `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.

A sample configuration file:

Expand All @@ -182,6 +184,7 @@ A sample configuration file:
"key": "<access key>",
"test_framework": "qunit|jasmine|jasmine2|mocha",
"test_path": ["relative/path/to/test/page1", "relative/path/to/test/page2"],
"test_server_port": "8899",
"browsers": [
{
"browser": "ie",
Expand Down
11 changes: 5 additions & 6 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var Log = require('../lib/logger'),
tunnel = require('tunnel'),
http = require('http'),
ConfigParser = require('../lib/configParser').ConfigParser,
serverPort = 8888,
config,
server,
timeout,
Expand Down Expand Up @@ -106,7 +105,7 @@ function getTestBrowserInfo(browserString, path) {
}

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

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

function launchServer(config, callback) {
logger.trace('launchServer:', serverPort);
logger.debug('Launching server on port:', serverPort);
logger.trace('launchServer:', config.test_server_port);
logger.debug('Launching server on port:', config.test_server_port);

server = new Server(client, workers, config, callback);
server.listen(parseInt(serverPort, 10));
server.listen(parseInt(config.test_server_port, 10));
}

function launchBrowser(browser, path) {
Expand Down Expand Up @@ -382,7 +381,7 @@ function runTests(config, callback) {
launchServer(config, runTestsCallback);

logger.trace('runTests: creating tunnel');
tunnel = new Tunnel(config.key, serverPort, config.tunnelIdentifier, config, function (err) {
tunnel = new Tunnel(config.key, config.test_server_port, config.tunnelIdentifier, config, function (err) {
if(err) {
cleanUpAndExit(null, err, [], callback);
} else {
Expand Down
14 changes: 13 additions & 1 deletion bin/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var todo = process.argv[2],
path = require('path'),
config;

if (todo === '--verbose') {
if (process.argv.indexOf('--verbose') !== -1) {
global.logLevel = process.env.LOG_LEVEL || 'debug';
} else {
global.logLevel = 'info';
Expand Down Expand Up @@ -36,6 +36,18 @@ try {
}
}

// extract a path to file to store tunnel pid
var pid = process.argv.find(function (param) { return param.indexOf('--pid') !== -1; });

if (pid) {
var extracted_path = /--pid=([\w\/\.-]+)/g.exec(pid);
if (extracted_path) {
config.tunnel_pid_file = extracted_path[1];
} else {
console.error('Error while parsing flag --pid. Usage: --pid=/path/to/file');
}
}

var runner = require('./cli.js');
runner.run(config, function(err) {
if(err) {
Expand Down
4 changes: 4 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,8 @@ exports.config = function(config) {
for (var key in config) {
this[key] = config[key];
}

if (!this.test_server_port) {
this.test_server_port = 8888;
}
};
11 changes: 11 additions & 0 deletions lib/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ var Log = require('./logger'),
logger = new Log(global.logLevel || 'info'),
exec = require('child_process').execFile,
fs = require('fs'),
path = require('path'),
https = require('https'),
utils = require('./utils'),
windows = ((process.platform.match(/win32/) || process.platform.match(/win64/)) !== null),
localBinary = __dirname + '/BrowserStackLocal' + (windows ? '.exe' : '');

Expand Down Expand Up @@ -55,6 +57,11 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, config, callback) {
}
});

if (config.tunnel_pid_file) {
utils.mkdirp(path.dirname(config.tunnel_pid_file));
fs.writeFile(config.tunnel_pid_file, subProcess.pid);
}

that.process = subProcess;
}

Expand Down Expand Up @@ -99,6 +106,10 @@ var Tunnel = function Tunnel(key, port, uniqueIdentifier, config, callback) {
subProcess = null;
} catch (e) {
logger.debug('[%s] failed to kill process:', new Date(), e);
} finally {
if (config.tunnel_pid_file) {
fs.unlink(config.tunnel_pid_file, function () {});
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var fs = require('fs'),
path = require('path');

String.prototype.escapeSpecialChars = function() {
return this.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
Expand Down Expand Up @@ -52,8 +55,19 @@ var createTestsFailedError = function createTestsFailedError(config) {
return error;
};

var mkdirp = function mkdirp(filepath) {
var dirname = path.dirname(filepath);
if (!fs.existsSync(dirname)) {
mkdirp(dirname);
}
if (!fs.existsSync(filepath)) {
fs.mkdirSync(filepath);
}
};

exports.titleCase = titleCase;
exports.uuid = uuid;
exports.browserString = browserString;
exports.objectSize = objectSize;
exports.createTestsFailedError = createTestsFailedError;
exports.mkdirp = mkdirp;

0 comments on commit c941e04

Please sign in to comment.