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

Commit

Permalink
feat(cli+config): allow defining multiple test suites in the config a…
Browse files Browse the repository at this point in the history
…nd running them separately from the command line.
  • Loading branch information
Sebastien Armand - sa250111 authored and juliemr committed Mar 11, 2014
1 parent 2624322 commit f9c4391
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 13 deletions.
33 changes: 33 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,39 @@ describe('angularjs homepage', function() {
});
```

It is possible to separate your tests in various test suites. The configuration becomes:

```javascript
// An example configuration file.
exports.config = {
// The address of a running selenium server.
seleniumAddress: 'http://localhost:4444/wd/hub',

// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'
},

// Spec patterns are relative to the location of the spec file. They may
// include glob patterns.
suites: {
homepage: 'tests/e2e/modules/homepage/**/*Spec.js',
search: ['tests/e2e/modules/contact_search/**/*Spec.js', 'tests/e2e/modules/venue_search/**/*Spec.js']
},

// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true, // Use colors in the command line report.
}
};
```

You can then easily switch from the command line between running one or the other
suite of tests:

protractor protractor.conf.js --suite hompage

Will only run the homepage section of the tests.

Further Reading
---------------
Expand Down
26 changes: 26 additions & 0 deletions lib/configParser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var path = require('path'),
glob = require('glob'),
util = require('util'),
_ = require('lodash'),
protractor = require('./protractor.js');

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

/**
* Returns the item if it's an array or puts the item in an array
* if it was not one already.
*/
var makeArray = function(item) {
return _.isArray(item) ? item : [item];
};

/**
* Resolve a list of file patterns into a list of individual file paths.
*
Expand Down Expand Up @@ -98,6 +107,23 @@ ConfigParser.resolveFilePatterns =
return resolvedFiles;
};

/**
* Returns only the specs that should run currently based on `config.suite`
*
* @return {Array} An array of globs locating the spec files
*/
ConfigParser.getSpecs = function(config) {
if (config.suite) {
return config.suites[config.suite];
}

var specs = config.specs || [];
_.forEach(config.suites, function(suite) {
specs = _.union(specs, makeArray(suite));
});

return specs;
}

/**
* Add the options in the parameter config to this runner instance.
Expand Down
23 changes: 12 additions & 11 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ var Runner = function(config) {
/**
* Execute the Runner's test cases through Jasmine.
*
* @private
* @private
* @param {Array} specs Array of Directory Path Strings
* @param done A callback for when tests are finished.
*/
Runner.prototype.runJasmine_ = function(specs, done) {
var minijn = require('minijasminenode'),
self = this;

require('../jasminewd');
webdriver.promise.controlFlow().execute(function() {
self.runTestPreparers_();
Expand All @@ -76,7 +76,7 @@ Runner.prototype.runJasmine_ = function(specs, done) {
/**
* Execute the Runner's test cases through Mocha.
*
* @private
* @private
* @param {Array} specs Array of Directory Path Strings
* @param done A callback for when tests are finished.
*/
Expand Down Expand Up @@ -127,7 +127,7 @@ Runner.prototype.runMocha_ = function(specs, done) {
/**
* Execute the Runner's test cases through Cucumber.
*
* @private
* @private
* @param {Array} specs Array of Directory Path Strings
* @param done A callback for when tests are finished.
*/
Expand Down Expand Up @@ -190,7 +190,7 @@ Runner.prototype.runCucumber_ = function(specs, done) {

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

/**
* Registrar for testPreparers - executed right before tests run.
* @public
* @public
* @param {string/Fn} filenameOrFn
*/
Runner.prototype.registerTestPreparer = function(filenameOrFn) {
Expand All @@ -224,7 +224,7 @@ Runner.prototype.registerTestPreparer = function(filenameOrFn) {

/**
* Executor of testPreparers
* @private
* @private
*/
Runner.prototype.runTestPreparers_ = function() {
this.runFilenamesOrFns_(this.preparers_);
Expand All @@ -240,7 +240,7 @@ Runner.prototype.runTestPreparers_ = function() {
* 2) if seleniumAddress is given, use that
* 3) if a sauceAccount is given, use that.
* 4) if a seleniumServerJar is specified, use that
* 5) try to find the seleniumServerJar in protractor/selenium
* 5) try to find the seleniumServerJar in protractor/selenium
*/
Runner.prototype.loadDriverProvider_ = function() {
var runnerPath;
Expand Down Expand Up @@ -284,7 +284,7 @@ Runner.prototype.getConfig = function() {

/**
* Sets up convenience globals for test specs
* @private
* @private
*/
Runner.prototype.setupGlobals_ = function(driver) {
var browser = protractor.wrapDriver(
Expand Down Expand Up @@ -320,8 +320,9 @@ Runner.prototype.run = function() {
// Determine included and excluded specs based on file pattern.
excludes = ConfigParser.resolveFilePatterns(
this.config_.exclude, true, this.config_.configDir);

specs = ConfigParser.resolveFilePatterns(
this.config_.specs, false, this.config_.configDir).filter(function(path) {
ConfigParser.getSpecs(this.config_), false, this.config_.configDir).filter(function(path) {
return excludes.indexOf(path) < 0;
});

Expand Down Expand Up @@ -365,7 +366,7 @@ Runner.prototype.run = function() {
throw new Error('config.framework (' + self.config_.framework +
') is not a valid framework.');
}

return deferred.promise;

// 3) Teardown
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"glob": ">=3.1.14",
"adm-zip": ">=0.4.2",
"optimist": "~0.6.0",
"q": "1.0.0"
"q": "1.0.0",
"lodash": "~2.4.1"
},
"devDependencies": {
"expect.js": "~0.2.0",
Expand All @@ -43,7 +44,7 @@
},
"main": "lib/protractor.js",
"scripts": {
"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"
"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"
},
"license": "MIT",
"version": "0.20.1",
Expand Down
5 changes: 5 additions & 0 deletions spec/suites/always_fail_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('This test suite', function(){
it('should never be ran through the --suite option', function(){
expect(true).toBe(false);
});
});
5 changes: 5 additions & 0 deletions spec/suites/ok_2_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('This test suite', function(){
it('should be ran through the --suite option', function(){
expect(true).toBe(true);
});
});
5 changes: 5 additions & 0 deletions spec/suites/ok_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('This test suite', function(){
it('should be ran through the --suite option', function(){
expect(true).toBe(true);
});
});
31 changes: 31 additions & 0 deletions spec/suitesConf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// The main suite of Protractor tests.
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',

// Spec patterns are relative to this directory.
suites: {
okspec: 'suites/ok_spec.js',
okmany: ['suites/ok_spec.js', 'suites/ok_2_spec.js'],
failingtest: 'suites/always_fail_spec.js'
}

// Exclude patterns are relative to this directory.
exclude: [
'basic/exclude*.js'
],

chromeOnly: false,

capabilities: {
'browserName': 'chrome'
},

baseUrl: 'http://localhost:8000',

params: {
login: {
user: 'Jane',
password: '1234'
}
}
};

0 comments on commit f9c4391

Please sign in to comment.