From eb9d56755fa93401502e7608c7c3d0f16927c082 Mon Sep 17 00:00:00 2001 From: Leo Gallucci Date: Fri, 23 Jan 2015 11:06:00 -0300 Subject: [PATCH] feat(frameworks): add support for custom frameworks Usage: ```js exports.config = { framework: 'custom', frameworkPath: '/path/to/your/framework/index.js' } ``` --- docs/referenceConf.js | 10 +++++++++- lib/configParser.js | 3 ++- lib/frameworks/README.md | 21 +++++++++++++++++++++ lib/runner.js | 6 ++++++ scripts/test.js | 1 + spec/custom/framework.js | 9 +++++++++ spec/custom/smoke_spec.js | 5 +++++ spec/customFramework.js | 16 ++++++++++++++++ spec/unit/runner_test.js | 13 +++++++++++++ 9 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 spec/custom/framework.js create mode 100644 spec/custom/smoke_spec.js create mode 100644 spec/customFramework.js diff --git a/docs/referenceConf.js b/docs/referenceConf.js index ae5cd10ca..ae1bbe2af 100644 --- a/docs/referenceConf.js +++ b/docs/referenceConf.js @@ -244,7 +244,15 @@ exports.config = { // ----- The test framework -------------------------------------------------- // --------------------------------------------------------------------------- - // Test framework to use. This may be jasmine, jasmine2, cucumber, or mocha. + // Test framework to use. This may be one of: + // jasmine, jasmine2, cucumber, mocha or custom. + // + // When the framework is set to "custom" you'll need to additionally + // set frameworkPath with the path relative to the config file or absolute + // framework: 'custom', + // frameworkPath: './frameworks/my_custom_jasmine.js', + // See github.com/angular/protractor/blob/master/lib/frameworks/README.md + // to comply with the interface details of your custom implementation. // // Jasmine is fully supported as a test and assertion framework. // Mocha and Cucumber have limited beta support. You will need to include your diff --git a/lib/configParser.js b/lib/configParser.js index 8ff2ed1a1..574f001bb 100644 --- a/lib/configParser.js +++ b/lib/configParser.js @@ -146,7 +146,8 @@ ConfigParser.getSpecs = function(config) { ConfigParser.prototype.addConfig_ = function(additionalConfig, relativeTo) { // All filepaths should be kept relative to the current config location. // This will not affect absolute paths. - ['seleniumServerJar', 'chromeDriver', 'onPrepare', 'firefoxPath']. + ['seleniumServerJar', 'chromeDriver', 'onPrepare', 'firefoxPath', + 'frameworkPath']. forEach(function(name) { if (additionalConfig[name] && typeof additionalConfig[name] === 'string') { diff --git a/lib/frameworks/README.md b/lib/frameworks/README.md index 5e5613e74..370b6cdeb 100644 --- a/lib/frameworks/README.md +++ b/lib/frameworks/README.md @@ -36,3 +36,24 @@ Requirements duration: integer }] ``` + +Custom Frameworks +----------------- + +If you have created/adapted a custom framework and want it added to +Protractor core please send a PR so it can evaluated for addition as an +official supported framework. In the meantime you can instruct Protractor +to use your own framework via the config file: + +```js +exports.config = { + // set to "custom" instead of jasmine/jasmine2/mocha/cucumber. + framework: 'custom', + // path relative to the current config file + frameworkPath: './frameworks/my_custom_jasmine.js', +}; +``` + +More on this at [referenceConf](../../docs/referenceConf.js) "The test framework" section. + +**Disclaimer**: current framework interface can change without a major version bump. diff --git a/lib/runner.js b/lib/runner.js index 9a57ad1d5..bf5fe2234 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -286,6 +286,12 @@ Runner.prototype.run = function() { } else if (self.config_.framework === 'explorer') { // Private framework. Do not use. frameworkPath = './frameworks/explorer.js'; + } else if (self.config_.framework === 'custom') { + if (!self.config_.frameworkPath) { + throw new Error('When config.framework is custom, ' + + 'config.frameworkPath is required.'); + } + frameworkPath = self.config_.frameworkPath; } else { throw new Error('config.framework (' + self.config_.framework + ') is not a valid framework.'); diff --git a/scripts/test.js b/scripts/test.js index e4a9edb73..f77f28a59 100755 --- a/scripts/test.js +++ b/scripts/test.js @@ -27,6 +27,7 @@ var passingTests = [ 'node lib/cli.js spec/restartBrowserBetweenTestsConf.js', 'node lib/cli.js spec/getCapabilitiesConf.js', 'node lib/cli.js spec/controlLockConf.js', + 'node lib/cli.js spec/customFramework.js', 'node node_modules/.bin/jasmine JASMINE_CONFIG_PATH=scripts/unit_test.json' ]; diff --git a/spec/custom/framework.js b/spec/custom/framework.js new file mode 100644 index 000000000..31b14a23e --- /dev/null +++ b/spec/custom/framework.js @@ -0,0 +1,9 @@ +/** + * Jasmine framework dummy alias to prove Protractor supports + * external custom frameworks. + * + * @param {Runner} runner The current Protractor Runner. + * @param {Array} specs Array of Directory Path Strings. + * @return {q.Promise} Promise resolved with the test results + */ +exports.run = require('../../lib/frameworks/jasmine.js').run; diff --git a/spec/custom/smoke_spec.js b/spec/custom/smoke_spec.js new file mode 100644 index 000000000..b34800c7c --- /dev/null +++ b/spec/custom/smoke_spec.js @@ -0,0 +1,5 @@ +describe('smoke jasmine tests', function() { + it('should do some dummy test', function() { + expect(1).toBe(1); + }); +}); diff --git a/spec/customFramework.js b/spec/customFramework.js new file mode 100644 index 000000000..e2759e6e0 --- /dev/null +++ b/spec/customFramework.js @@ -0,0 +1,16 @@ +var env = require('./environment.js'); + +exports.config = { + seleniumAddress: env.seleniumAddress, + + framework: 'custom', + frameworkPath: './custom/framework.js', + + specs: [ + 'custom/smoke_spec.js' + ], + + capabilities: env.capabilities, + + baseUrl: env.baseUrl, +}; diff --git a/spec/unit/runner_test.js b/spec/unit/runner_test.js index 2fbb25704..e466b7796 100644 --- a/spec/unit/runner_test.js +++ b/spec/unit/runner_test.js @@ -46,4 +46,17 @@ describe('the Protractor runner', function() { runner.run(); }).toThrow(); }); + + it('should fail when no custom framework is defined', function(done) { + var config = { + mockSelenium: true, + specs: ['*.js'], + framework: 'custom' + }; + + var runner = new Runner(config); + runner.run().then(function() { + done.fail('expected error when no custom framework is defined'); + }, done); + }); });