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

Commit f9c4391

Browse files
Sebastien Armand - sa250111juliemr
Sebastien Armand - sa250111
authored andcommitted
feat(cli+config): allow defining multiple test suites in the config and running them separately from the command line.
1 parent 2624322 commit f9c4391

File tree

8 files changed

+120
-13
lines changed

8 files changed

+120
-13
lines changed

docs/getting-started.md

+33
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,39 @@ describe('angularjs homepage', function() {
197197
});
198198
```
199199

200+
It is possible to separate your tests in various test suites. The configuration becomes:
201+
202+
```javascript
203+
// An example configuration file.
204+
exports.config = {
205+
// The address of a running selenium server.
206+
seleniumAddress: 'http://localhost:4444/wd/hub',
207+
208+
// Capabilities to be passed to the webdriver instance.
209+
capabilities: {
210+
'browserName': 'chrome'
211+
},
212+
213+
// Spec patterns are relative to the location of the spec file. They may
214+
// include glob patterns.
215+
suites: {
216+
homepage: 'tests/e2e/modules/homepage/**/*Spec.js',
217+
search: ['tests/e2e/modules/contact_search/**/*Spec.js', 'tests/e2e/modules/venue_search/**/*Spec.js']
218+
},
219+
220+
// Options to be passed to Jasmine-node.
221+
jasmineNodeOpts: {
222+
showColors: true, // Use colors in the command line report.
223+
}
224+
};
225+
```
226+
227+
You can then easily switch from the command line between running one or the other
228+
suite of tests:
229+
230+
protractor protractor.conf.js --suite hompage
231+
232+
Will only run the homepage section of the tests.
200233

201234
Further Reading
202235
---------------

lib/configParser.js

+26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var path = require('path'),
22
glob = require('glob'),
33
util = require('util'),
4+
_ = require('lodash'),
45
protractor = require('./protractor.js');
56

67
// Coffee is required here to enable config files written in coffee-script.
@@ -67,6 +68,14 @@ var merge_ = function(into, from) {
6768
return into;
6869
};
6970

71+
/**
72+
* Returns the item if it's an array or puts the item in an array
73+
* if it was not one already.
74+
*/
75+
var makeArray = function(item) {
76+
return _.isArray(item) ? item : [item];
77+
};
78+
7079
/**
7180
* Resolve a list of file patterns into a list of individual file paths.
7281
*
@@ -98,6 +107,23 @@ ConfigParser.resolveFilePatterns =
98107
return resolvedFiles;
99108
};
100109

110+
/**
111+
* Returns only the specs that should run currently based on `config.suite`
112+
*
113+
* @return {Array} An array of globs locating the spec files
114+
*/
115+
ConfigParser.getSpecs = function(config) {
116+
if (config.suite) {
117+
return config.suites[config.suite];
118+
}
119+
120+
var specs = config.specs || [];
121+
_.forEach(config.suites, function(suite) {
122+
specs = _.union(specs, makeArray(suite));
123+
});
124+
125+
return specs;
126+
}
101127

102128
/**
103129
* Add the options in the parameter config to this runner instance.

lib/runner.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ var Runner = function(config) {
4646
/**
4747
* Execute the Runner's test cases through Jasmine.
4848
*
49-
* @private
49+
* @private
5050
* @param {Array} specs Array of Directory Path Strings
5151
* @param done A callback for when tests are finished.
5252
*/
5353
Runner.prototype.runJasmine_ = function(specs, done) {
5454
var minijn = require('minijasminenode'),
5555
self = this;
56-
56+
5757
require('../jasminewd');
5858
webdriver.promise.controlFlow().execute(function() {
5959
self.runTestPreparers_();
@@ -76,7 +76,7 @@ Runner.prototype.runJasmine_ = function(specs, done) {
7676
/**
7777
* Execute the Runner's test cases through Mocha.
7878
*
79-
* @private
79+
* @private
8080
* @param {Array} specs Array of Directory Path Strings
8181
* @param done A callback for when tests are finished.
8282
*/
@@ -127,7 +127,7 @@ Runner.prototype.runMocha_ = function(specs, done) {
127127
/**
128128
* Execute the Runner's test cases through Cucumber.
129129
*
130-
* @private
130+
* @private
131131
* @param {Array} specs Array of Directory Path Strings
132132
* @param done A callback for when tests are finished.
133133
*/
@@ -190,7 +190,7 @@ Runner.prototype.runCucumber_ = function(specs, done) {
190190

191191
/**
192192
* Internal helper for abstraction of polymorphic filenameOrFn properties.
193-
* @private
193+
* @private
194194
* @param {Array} source The Array that we'll be iterating through
195195
* as we evaluate whether to require or execute each item.
196196
*/
@@ -214,7 +214,7 @@ Runner.prototype.runFilenamesOrFns_ = function(source) {
214214

215215
/**
216216
* Registrar for testPreparers - executed right before tests run.
217-
* @public
217+
* @public
218218
* @param {string/Fn} filenameOrFn
219219
*/
220220
Runner.prototype.registerTestPreparer = function(filenameOrFn) {
@@ -224,7 +224,7 @@ Runner.prototype.registerTestPreparer = function(filenameOrFn) {
224224

225225
/**
226226
* Executor of testPreparers
227-
* @private
227+
* @private
228228
*/
229229
Runner.prototype.runTestPreparers_ = function() {
230230
this.runFilenamesOrFns_(this.preparers_);
@@ -240,7 +240,7 @@ Runner.prototype.runTestPreparers_ = function() {
240240
* 2) if seleniumAddress is given, use that
241241
* 3) if a sauceAccount is given, use that.
242242
* 4) if a seleniumServerJar is specified, use that
243-
* 5) try to find the seleniumServerJar in protractor/selenium
243+
* 5) try to find the seleniumServerJar in protractor/selenium
244244
*/
245245
Runner.prototype.loadDriverProvider_ = function() {
246246
var runnerPath;
@@ -284,7 +284,7 @@ Runner.prototype.getConfig = function() {
284284

285285
/**
286286
* Sets up convenience globals for test specs
287-
* @private
287+
* @private
288288
*/
289289
Runner.prototype.setupGlobals_ = function(driver) {
290290
var browser = protractor.wrapDriver(
@@ -320,8 +320,9 @@ Runner.prototype.run = function() {
320320
// Determine included and excluded specs based on file pattern.
321321
excludes = ConfigParser.resolveFilePatterns(
322322
this.config_.exclude, true, this.config_.configDir);
323+
323324
specs = ConfigParser.resolveFilePatterns(
324-
this.config_.specs, false, this.config_.configDir).filter(function(path) {
325+
ConfigParser.getSpecs(this.config_), false, this.config_.configDir).filter(function(path) {
325326
return excludes.indexOf(path) < 0;
326327
});
327328

@@ -365,7 +366,7 @@ Runner.prototype.run = function() {
365366
throw new Error('config.framework (' + self.config_.framework +
366367
') is not a valid framework.');
367368
}
368-
369+
369370
return deferred.promise;
370371

371372
// 3) Teardown

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"glob": ">=3.1.14",
1919
"adm-zip": ">=0.4.2",
2020
"optimist": "~0.6.0",
21-
"q": "1.0.0"
21+
"q": "1.0.0",
22+
"lodash": "~2.4.1"
2223
},
2324
"devDependencies": {
2425
"expect.js": "~0.2.0",
@@ -43,7 +44,7 @@
4344
},
4445
"main": "lib/protractor.js",
4546
"scripts": {
46-
"test": "node lib/cli.js spec/basicConf.js; node lib/cli.js spec/multiConf.js; node lib/cli.js spec/altRootConf.js; node lib/cli.js spec/onPrepareConf.js; node lib/cli.js spec/mochaConf.js; node lib/cli.js spec/cucumberConf.js; node lib/cli.js spec/withLoginConf.js; node_modules/.bin/minijasminenode jasminewd/spec/adapterSpec.js spec/unit/*.js docs/spec/*.js"
47+
"test": "node lib/cli.js spec/basicConf.js; node lib/cli.js spec/multiConf.js; node lib/cli.js spec/altRootConf.js; node lib/cli.js spec/onPrepareConf.js; node lib/cli.js spec/mochaConf.js; node lib/cli.js spec/cucumberConf.js; node lib/cli.js spec/withLoginConf.js; node_modules/.bin/minijasminenode jasminewd/spec/adapterSpec.js spec/unit/*.js docs/spec/*.js; node lib/cli.js spec/suitesConf.js --suite okmany; node lib/cli.js spec/suitesConf.js --suite okspec"
4748
},
4849
"license": "MIT",
4950
"version": "0.20.1",

spec/suites/always_fail_spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('This test suite', function(){
2+
it('should never be ran through the --suite option', function(){
3+
expect(true).toBe(false);
4+
});
5+
});

spec/suites/ok_2_spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('This test suite', function(){
2+
it('should be ran through the --suite option', function(){
3+
expect(true).toBe(true);
4+
});
5+
});

spec/suites/ok_spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('This test suite', function(){
2+
it('should be ran through the --suite option', function(){
3+
expect(true).toBe(true);
4+
});
5+
});

spec/suitesConf.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// The main suite of Protractor tests.
2+
exports.config = {
3+
seleniumAddress: 'http://localhost:4444/wd/hub',
4+
5+
// Spec patterns are relative to this directory.
6+
suites: {
7+
okspec: 'suites/ok_spec.js',
8+
okmany: ['suites/ok_spec.js', 'suites/ok_2_spec.js'],
9+
failingtest: 'suites/always_fail_spec.js'
10+
}
11+
12+
// Exclude patterns are relative to this directory.
13+
exclude: [
14+
'basic/exclude*.js'
15+
],
16+
17+
chromeOnly: false,
18+
19+
capabilities: {
20+
'browserName': 'chrome'
21+
},
22+
23+
baseUrl: 'http://localhost:8000',
24+
25+
params: {
26+
login: {
27+
user: 'Jane',
28+
password: '1234'
29+
}
30+
}
31+
};

0 commit comments

Comments
 (0)