Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit 02cb819

Browse files
committed
feat(cli): allow passing params directly to your test
Adds a config object 'params' which is passed directly to instances of protractor. 'params' may contain nested objects, and can be changed via the command line as: --params.login.user 'Joe' --params.login.password 'abc' This change also switches to using optimist to parse command line flags for more flexibility and better usage documentation. Closes #32.
1 parent dbef262 commit 02cb819

File tree

7 files changed

+90
-97
lines changed

7 files changed

+90
-97
lines changed

lib/cli.js

+48-94
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,46 @@
1+
/**
2+
* The command line interface for interacting with the Protractor runner.
3+
* It takes care of parsing the config file and command line options.
4+
*/
5+
16
var util = require('util');
27
var path = require('path')
38
var fs = require('fs');
49
var runner = require('./runner.js');
5-
var glob = require('glob');
10+
var argv = require('optimist').
11+
usage('Usage: protractor [options] [config]').
12+
describe('version', 'Print Protractor version').
13+
describe('browser', 'Browsername, e.g. chrome or firefox').
14+
describe('seleniumAddress', 'A running seleium address to use').
15+
describe('seleniumServerJar', 'Location of the standalone selenium jar file').
16+
describe('seleniumPort', 'Optional port for the selenium standalone server').
17+
describe('baseUrl', 'URL to prepend to all relative paths').
18+
describe('rootElement', 'Element housing ng-app, if not html or body').
19+
describe('specs', 'Comma-separated list of files to test').
20+
describe('verbose', 'Print full spec names').
21+
describe('includeStackTrace', 'Print stack trace on error').
22+
describe('params', 'Param object to be passed to the tests').
23+
alias('browser', 'capabilities.browserName').
24+
alias('name', 'capabilities.name').
25+
alias('platform', 'capabilities.platform').
26+
alias('platform-version', 'capabilities.version').
27+
alias('tags', 'capabilities.tags').
28+
alias('build', 'capabilities.build').
29+
boolean('verbose').
30+
boolean('includeStackTrace').
31+
alias('verbose', 'jasmineNodeOpts.verbose').
32+
alias('includeStackTrace', 'jasmineNodeOpts.includeStackTrace').
33+
check(function(arg) {
34+
if (arg._.length > 1) {
35+
throw 'Error: more than one config file specified'
36+
}
37+
if (!(process.argv.length > 2)) {
38+
throw '';
39+
}
40+
}).
41+
argv;
642

7-
/**
8-
* The command line interface for interacting with the Protractor runner.
9-
* It takes care of parsing the config file and command line options.
10-
*/
1143

12-
var args = process.argv.slice(2);
1344
var configPath;
1445

1546

@@ -19,101 +50,24 @@ var printVersion = function () {
1950
process.exit(0);
2051
};
2152

22-
23-
if (!args.length) {
24-
util.puts('USAGE: protractor [configFile] [options]');
25-
util.puts('Options:');
26-
util.puts(' --version: Print Protractor version');
27-
util.puts(' --browser <string>: Browsername, e.g. chrome or firefox');
28-
util.puts(' --seleniumAddress <string>: A running selenium address to use');
29-
util.puts(' --seleniumServerJar <string>: Location of the standalone selenium server .jar file');
30-
util.puts(' --seleniumPort <string>: Optional port for the standalone selenium server');
31-
util.puts(' --baseUrl <string>: URL to prepend to all relative paths');
32-
util.puts(' --rootElement <string>: Element housing ng-app, if not html or body');
33-
util.puts(' --specs <list>: Comma separated list of files to test');
34-
util.puts(' --[no]includeStackTrace: Print stack trace on error');
35-
util.puts(' --verbose: Print full spec names');
36-
37-
process.exit(0);
53+
if (argv.version) {
54+
printVersion();
3855
}
3956

40-
var commandLineConfig = { capabilities: {}, jasmineNodeOpts: {}};
41-
42-
while(args.length) {
43-
var arg = args.shift();
44-
switch(arg) {
45-
case '--version':
46-
printVersion();
47-
break;
48-
case '--browser':
49-
commandLineConfig.capabilities.browserName = args.shift();
50-
break;
51-
case '--name':
52-
commandLineConfig.capabilities.name = args.shift();
53-
break;
54-
case '--platform':
55-
commandLineConfig.capabilities.platform = args.shift();
56-
break;
57-
case '--platform-version':
58-
commandLineConfig.capabilities.version = args.shift();
59-
break;
60-
case '--build':
61-
commandLineConfig.capabilities.build = args.shift();
62-
break;
63-
case '--tags':
64-
commandLineConfig.capabilities.tags = args.shift().split(',');
65-
break;
66-
case '--seleniumAddress':
67-
commandLineConfig.seleniumAddress = args.shift();
68-
break;
69-
case '--seleniumServerJar':
70-
commandLineConfig.seleniumServerJar = args.shift();
71-
break;
72-
case '--seleniumPort':
73-
commandLineConfig.seleniumPort = args.shift();
74-
break;
75-
case '--chromeDriver':
76-
commandLineConfig.chromeDriver = args.shift();
77-
break;
78-
case '--sauceUser':
79-
commandLineConfig.sauceUser = args.shift();
80-
break;
81-
case '--sauceKey':
82-
commandLineConfig.sauceKey = args.shift();
83-
break;
84-
case '--baseUrl':
85-
commandLineConfig.baseUrl = args.shift();
86-
break;
87-
case '--rootElement':
88-
commandLineConfig.rootElement = args.shift();
89-
break;
90-
case '--specs':
91-
commandLineSpecs = args.shift().split(',');
92-
commandLineSpecs.forEach(function(spec, index, arr) {
93-
arr[index] = path.resolve(process.cwd(), spec);
94-
});
95-
commandLineConfig.specs = commandLineSpecs;
96-
break;
97-
case '--includeStackTrace':
98-
commandLineConfig.jasmineNodeOpts.includeStackTrace = true;
99-
break;
100-
case '--noincludeStackTrace':
101-
commandLineConfig.jasmineNodeOpts.includeStackTrace = false;
102-
break;
103-
case '--verbose':
104-
commandLineConfig.jasmineNodeOpts.isVerbose = true;
105-
break;
106-
default:
107-
configPath = path.resolve(process.cwd(), arg);
108-
break;
109-
}
57+
if (argv.specs) {
58+
argv.specs = argv.specs.split(',');
59+
argv.specs.forEach(function(spec, index, arr) {
60+
arr[index] = path.resolve(process.cwd(), spec);
61+
});
11062
}
11163

112-
if (configPath) {
64+
var configFilename = argv._[0];
65+
if (configFilename) {
66+
configPath = path.resolve(process.cwd(), configFilename);
11367
runner.addConfig(require(configPath).config);
11468
runner.addConfig({specFileBase: path.dirname(configPath)})
11569
}
11670

117-
runner.addConfig(commandLineConfig);
71+
runner.addConfig(argv);
11872

11973
runner.runOnce();

lib/protractor.js

+7
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ var Protractor = function(webdriver, opt_baseUrl, opt_rootElement) {
9191
*/
9292
this.ignoreSynchronization = false;
9393

94+
/**
95+
* An object that holds custom test parameters.
96+
*
97+
* @type {Object}
98+
*/
99+
this.params = {};
100+
94101
this.moduleNames_ = [];
95102

96103
this.moduleScripts_ = [];

lib/runner.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var config = {
2626
'browserName': 'chrome'
2727
},
2828
rootElement: 'body',
29+
params: {},
2930
jasmineNodeOpts: {
3031
isVerbose: false,
3132
showColors: true,
@@ -190,10 +191,13 @@ var runJasmineTests = function() {
190191

191192
sessionId = session.getId();
192193

193-
protractor.setInstance(protractor.wrapDriver(
194+
var ptor = protractor.wrapDriver(
194195
driver,
195196
config.baseUrl,
196-
config.rootElement));
197+
config.rootElement)
198+
ptor.params = config.params;
199+
200+
protractor.setInstance(ptor);
197201

198202
// Export protractor to the global namespace to be used in tests.
199203
global.protractor = protractor;

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"minijasminenode": "~0.2.4",
1717
"saucelabs": "~0.1.0",
1818
"glob": ">=3.1.14",
19-
"adm-zip": ">=0.4.2"
19+
"adm-zip": ">=0.4.2",
20+
"optimist": "~0.6.0"
2021
},
2122
"devDependencies": {
2223
"expect.js": "~0.2.0",

referenceConf.js

+14
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ exports.config = {
5656
'browserName': 'chrome'
5757
},
5858

59+
// ----- More information for your tests ----
60+
//
5961
// A base URL for your application under test. Calls to protractor.get()
6062
// with relative paths will be prepended with this.
6163
baseUrl: 'http://localhost:8000',
@@ -74,6 +76,18 @@ exports.config = {
7476
// jasmine.getEnv().addReporter(new jasmine.JUnitXmlReporter(
7577
// 'outputdir/', true, true));
7678
},
79+
80+
// The params object will be passed directly to the protractor instance,
81+
// and can be accessed from your test. It is an arbitrary object and can
82+
// contain anything you my need in your test.
83+
// This can be changed via the command line as:
84+
// --params.login.user 'Joe'
85+
params: {
86+
login: {
87+
user: 'Jane',
88+
password: '1234'
89+
}
90+
}
7791

7892
// ----- Options to be passed to minijasminenode -----
7993
jasmineNodeOpts: {

spec/basicConf.js

+7
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,11 @@ exports.config = {
1515
},
1616

1717
baseUrl: 'http://localhost:8000',
18+
19+
params: {
20+
login: {
21+
user: 'Jane',
22+
password: '1234'
23+
}
24+
}
1825
};

spec/lib_spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ describe('protractor library', function() {
1414
expect(ptor.getTitle()).toEqual('My AngularJS App');
1515
});
1616

17+
it('should export custom parameters to the protractor instance', function() {
18+
expect(ptor.params.login).toBeDefined();
19+
expect(ptor.params.login.user).toEqual('Jane');
20+
expect(ptor.params.login.password).toEqual('1234');
21+
});
22+
1723
it('should allow a mix of using protractor and using the driver directly',
1824
function() {
1925
ptor.get('app/index.html');

0 commit comments

Comments
 (0)