Skip to content

Commit 0c51875

Browse files
committed
Merge branch 'master' of github.com:browserstack/browserstack-runner into travis_ci
2 parents b268153 + 2ff95f2 commit 0c51875

File tree

6 files changed

+156
-35
lines changed

6 files changed

+156
-35
lines changed

README.md

+33-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ If you're getting an error `EACCES open ... BrowserStackLocal`, configure npm to
2525

2626
Where `[user]` is replaced with a local user with enough permissions.
2727

28+
CLI options:
29+
30+
`--path`: Can be used if a different test runner is needed other than the one present in the `browserstack.json` file.
31+
32+
`--pid`: Custom `pid` file that stores the pid's of the BrowserStackLocal instances created.
33+
34+
`--verbose` or `-v`: For verbose logging.
35+
36+
`--browsers` or `-b`: Space separated list of `cli_key` as defined in the `browserstack.json` file. This will run tests on the selected browsers only. If not present tests will run on all browsers present in the configuration file.
37+
38+
Sample Usage:
39+
`browserstack_runner --browsers 1 2 3 --path 'path/to/test/runner' --pid 'path/to/pid/file' -v`
40+
2841
## Usage as a module
2942

3043
`browserstack-runner` can also be used as a module. To run your tests, inside your project do -
@@ -160,7 +173,11 @@ The structure of the `report` object is as follows -
160173

161174
To run browser tests on BrowserStack infrastructure, you need to create a `browserstack.json` file in project's root directory (the directory from which tests are run), by running this command:
162175

163-
browserstack-runner init
176+
`browserstack-runner init [preset] [path]`
177+
178+
`preset`: Path of a custom preset file. Default: `presets/default.json`
179+
180+
`path`: Path to test file. Default: `path/to/test/runner`
164181

165182
### Parameters for `browserstack.json`
166183

@@ -191,33 +208,37 @@ A sample configuration file:
191208
"browser_version": "10.0",
192209
"device": null,
193210
"os": "Windows",
194-
"os_version": "8"
211+
"os_version": "8",
212+
"cli_key": 1
195213
},
196214
{
197215
"os": "android",
198216
"os_version": "4.0",
199-
"device": "Samsung Galaxy Nexus"
217+
"device": "Samsung Galaxy Nexus",
218+
"cli_key": 2
200219
},
201220
{
202221
"os": "ios",
203222
"os_version": "7.0",
204-
"device": "iPhone 5S"
223+
"device": "iPhone 5S",
224+
"cli_key": 3
205225
}
206226
]
207227
}
208228
```
209229

210230
#### `browsers` parameter
211231

212-
`browsers` parameter is a list of objects, where each object contains the details of the browsers on which you want to run your tests. This object differs for browsers on desktop platforms and browsers on mobile platforms. Browsers on desktop platform should contain `browser`, `browser_version`, `os `, `os_version` parameters set as required.
232+
`browsers` parameter is a list of objects, where each object contains the details of the browsers on which you want to run your tests. This object differs for browsers on desktop platforms and browsers on mobile platforms. Browsers on desktop platform should contain `browser`, `browser_version`, `os`, `os_version` parameters set as required and the `cli_key` parameter is optional and can be used in the command line when tests need to be run on a set of browsers from the `browserstack.json` file.
213233

214234
Example:
215235
```json
216236
{
217237
"browser": "ie",
218238
"browser_version": "10.0",
219239
"os": "Windows",
220-
"os_version": "8"
240+
"os_version": "8",
241+
"cli_key": 1
221242
}
222243
```
223244

@@ -228,12 +249,14 @@ Example:
228249
[{
229250
"os": "ios",
230251
"os_version": "8.3",
231-
"device": "iPhone 6 Plus"
252+
"device": "iPhone 6 Plus",
253+
"cli_key": 1
232254
},
233255
{
234256
"os": "android",
235257
"os_version": "4.0",
236-
"device": "Google Nexus"
258+
"device": "Google Nexus",
259+
"cli_key": 2
237260
}
238261
]
239262
```
@@ -266,7 +289,8 @@ Example:
266289
"browser_version": "10.0",
267290
"device": null,
268291
"os": "Windows",
269-
"os_version": "8"
292+
"os_version": "8",
293+
"cli_key": 1
270294
}
271295
]
272296
}

bin/init.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#! /usr/bin/env node
22

33
var fs = require('fs');
4-
var preset = process.argv[3] || 'default';
4+
var preset = require('./runner').preset;
5+
var path = require('./runner').path;
56
var browsers = require('../presets/' + preset + '.json');
67

78
var config = {
89
username: 'BROWSERSTACK_USERNAME',
910
key: 'BROWSERSTACK_KEY',
10-
test_path: 'path/to/test/runner',
11+
test_path: path || 'path/to/test/runner',
1112
browsers: browsers
1213
};
1314

bin/runner.js

+63-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,53 @@
11
#! /usr/bin/env node
22

3-
var todo = process.argv[2],
4-
path = require('path'),
5-
config;
3+
var yargs = require('yargs')
4+
.command('init [preset] [path]', 'initialise browserstack.json with preset and test runner path', function(yargs) {
5+
return yargs.option('preset', {
6+
type: 'string',
7+
default: 'default',
8+
description: 'name of preset json file(without extension)(present in node_modules/browserstack-runner/presets to be used while initiating'
9+
})
10+
.option('path', {
11+
type: 'string',
12+
default: '/path/to/test/runner',
13+
description: 'path to test runner to be inserted in browserstack.json'
14+
});
15+
})
16+
.option('browsers', {
17+
alias: 'b',
18+
type: 'array',
19+
description: 'list of space separatedbrowsers keys as described in json file'
20+
})
21+
.option('path', {
22+
type: 'string',
23+
description: 'path to test file'
24+
})
25+
.option('version', {
26+
alias: 'V',
27+
description: 'browserstack-runner version'
28+
})
29+
.option('pid', {
30+
type: 'string',
31+
description: 'path to pid file'
32+
})
33+
.option('verbose', {
34+
alias: 'v',
35+
description: 'verbose logging'
36+
}).argv;
637

7-
if (process.argv.indexOf('--verbose') !== -1) {
38+
if (yargs['verbose']) {
839
global.logLevel = process.env.LOG_LEVEL || 'debug';
940
} else {
1041
global.logLevel = 'info';
1142
}
43+
var path = require('path'),
44+
config;
1245

13-
if (todo === 'init') {
46+
if(yargs['_'].indexOf('init') !== -1) {
47+
module.exports.preset = yargs['preset'];
48+
module.exports.path = yargs['path'];
1449
require('./init.js');
1550
return;
16-
} else if (todo === '--version') {
17-
require('./version.js');
18-
return;
1951
}
2052

2153
var config_path = process.env.BROWSERSTACK_JSON || 'browserstack.json';
@@ -37,17 +69,34 @@ try {
3769
}
3870

3971
// extract a path to file to store tunnel pid
40-
var pid = process.argv.find(function (param) { return param.indexOf('--pid') !== -1; });
72+
if(yargs.hasOwnProperty('pid')) {
73+
if(yargs['pid'].trim().length > 0) {
74+
config.tunnel_pid_file = yargs['pid'].trim();
75+
} else {
76+
throw new Error('Empty pid file path');
77+
}
78+
}
4179

42-
if (pid) {
43-
var extracted_path = /--pid=([\w\/\.-]+)/g.exec(pid);
44-
if (extracted_path) {
45-
config.tunnel_pid_file = extracted_path[1];
80+
// filter browsers according to from command line arguments
81+
if(yargs['browsers']) {
82+
if(yargs['browsers'].length > 0) {
83+
config.browsers = config.browsers.filter( function(browser) {
84+
return yargs['browsers'].indexOf(browser['cli_key']) !== -1;
85+
});
4686
} else {
47-
console.error('Error while parsing flag --pid. Usage: --pid=/path/to/file');
87+
throw new Error('No browser keys specified. Usage --browsers <key1> <key2> ...');
88+
}
89+
if(config.browsers.length === 0) {
90+
throw new Error('Invalid browser keys');
91+
}
92+
if(config.browsers.length < yargs['browsers'].length) {
93+
console.warn('Some browser keys not present in config file.');
4894
}
4995
}
5096

97+
// test file path from cli arguments
98+
config.test_path = yargs['path'] || config.test_path;
99+
51100
var runner = require('./cli.js');
52101
runner.run(config, function(err) {
53102
if(err) {

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"mime": "1.6.0",
1616
"resolve": "1.1.7",
1717
"send": "0.16.2",
18-
"tunnel": "0.0.3"
18+
"tunnel": "0.0.3",
19+
"yargs": "12.0.1"
1920
},
2021
"devDependencies": {
2122
"jshint": "2.9.6",

presets/default.json

+16-8
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,56 @@
33
"browser": "firefox",
44
"browser_version": "latest",
55
"os": "OS X",
6-
"os_version": "Lion"
6+
"os_version": "Lion",
7+
"cli_key": 1
78
},
89
{
910
"browser": "safari",
1011
"browser_version": "latest",
1112
"os": "OS X",
12-
"os_version": "Mountain Lion"
13+
"os_version": "Mountain Lion",
14+
"cli_key": 2
1315
},
1416
{
1517
"browser": "chrome",
1618
"browser_version": "latest",
1719
"os": "OS X",
18-
"os_version": "Mountain Lion"
20+
"os_version": "Mountain Lion",
21+
"cli_key": 3
1922
},
2023
{
2124
"browser": "firefox",
2225
"browser_version": "latest",
2326
"os": "Windows",
24-
"os_version": "7"
27+
"os_version": "7",
28+
"cli_key": 4
2529
},
2630
{
2731
"browser": "chrome",
2832
"browser_version": "latest",
2933
"os": "Windows",
30-
"os_version": "7"
34+
"os_version": "7",
35+
"cli_key": 5
3136
},
3237
{
3338
"browser": "ie",
3439
"browser_version": "9.0",
3540
"os": "Windows",
36-
"os_version": "7"
41+
"os_version": "7",
42+
"cli_key": 6
3743
},
3844
{
3945
"browser": "ie",
4046
"browser_version": "10.0",
4147
"os": "Windows",
42-
"os_version": "8"
48+
"os_version": "8",
49+
"cli_key": 7
4350
},
4451
{
4552
"browser": "ie",
4653
"browser_version": "11.0",
4754
"os": "Windows",
48-
"os_version": "7"
55+
"os_version": "7",
56+
"cli_key": 8
4957
}
5058
]

tests/behaviour/runner.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ var assert = require('assert'),
77
path = require('path'),
88
http = require('http'),
99
browserstackRunner = require('../../bin/cli.js'),
10-
Tunnel = require('../../lib/local.js').Tunnel;
10+
Tunnel = require('../../lib/local.js').Tunnel,
11+
exec = require('child_process').exec,
12+
execSync = require('child_process').execSync;
1113

1214
var getBaseConfig = function() {
1315
return {
@@ -226,3 +228,39 @@ describe('Pass/Fail reporting', function() {
226228
});
227229
});
228230
});
231+
232+
describe('Command Line Interface Tests', function() {
233+
this.timeout(0);
234+
it('Should run with valid CLI arguments', function(done) {
235+
execSync('bin/runner.js init');
236+
exec('bin/runner.js --browsers 1 --path tests/behaviour/resources/qunit_sample.html', null, function(error, stdout, stderr) {
237+
assert.equal(error, null);
238+
done();
239+
});
240+
});
241+
it('Should raise errors if all invalid browser keys.', function(done) {
242+
exec('bin/runner.js --browsers 10 --path tests/behaviour/resources/qunit_sample.html', null, function(error, stdout, stderr) {
243+
assert.notEqual(error.message.match('Invalid'), null);
244+
done();
245+
});
246+
});
247+
it('Should raise error if invalid test path', function(done) {
248+
exec('bin/runner.js --browsers 1 --path invalid/path', function(error, stdout, stderr) {
249+
assert.notEqual(error, null);
250+
assert.notEqual(error.message.match('Invalid'), null);
251+
done();
252+
});
253+
});
254+
it('Should run tests on browsers present if some keys not present', function(done) {
255+
exec('bin/runner.js --browsers 1 10 --path tests/behaviour/resources/qunit_sample.html', null, function(error, stdout, stderr) {
256+
assert.equal(error, null);
257+
done();
258+
});
259+
});
260+
it('Should raise error if empty pid path with pid parameter', function(done) {
261+
exec('bin/runner.js --browsers 1 --path tests/behaviour/resources/qunit_sample.html --pid', null, function(error, stdout, stderr) {
262+
assert.notEqual(error, null);
263+
done();
264+
});
265+
});
266+
});

0 commit comments

Comments
 (0)