From 1b7675aab7846bee54117876887bfec07ce31745 Mon Sep 17 00:00:00 2001 From: JB Nizet Date: Sat, 21 Sep 2013 13:13:19 +0200 Subject: [PATCH] feat(cli): add an onPrepare callback to the config This onPrepare callback is useful when you want to do something with protractor before running the specs. For example, you might want to monkey-patch protractor with custom functions used by all the specs, or add the protractor instance to the globals. An example usage is shown in the spec/onPrepareConf.js file and its associated spec. --- lib/cli.js | 8 +++++++- package.json | 2 +- referenceConf.js | 9 +++++++++ spec/onPrepare/onPrepare_spec.js | 18 ++++++++++++++++++ spec/onPrepareConf.js | 26 ++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 spec/onPrepare/onPrepare_spec.js create mode 100644 spec/onPrepareConf.js diff --git a/lib/cli.js b/lib/cli.js index 684cf34b2..223569a5a 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -154,13 +154,19 @@ var startJasmineTests = function() { // Export protractor to the global namespace to be used in tests. global.protractor = protractor; - // Set up the Jasmine WebDriver Adapter + // Set up the Jasmine WebDriver Adapter. require('../jasminewd'); var options = config.jasmineNodeOpts; originalOnComplete = options.onComplete; options.onComplete = cleanUp; + // Let the configuration configure the protractor instance before running + // the tests. + if (config.onPrepare) { + config.onPrepare(); + } + minijn.executeSpecs(options); }); } diff --git a/package.json b/package.json index a119fe0fd..f7c4a7af6 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "bin": "bin/protractor", "main": "lib/protractor.js", "scripts": { - "test": "node lib/cli.js spec/basicConf.js; node lib/cli.js spec/altRootConf.js; node_modules/.bin/minijasminenode jasminewd/spec/adapterSpec.js" + "test": "node lib/cli.js spec/basicConf.js; node lib/cli.js spec/altRootConf.js; node lib/cli.js spec/onPrepareConf.js; node_modules/.bin/minijasminenode jasminewd/spec/adapterSpec.js" }, "version": "0.9.0" } diff --git a/referenceConf.js b/referenceConf.js index 9b18f66c3..e22a3541c 100644 --- a/referenceConf.js +++ b/referenceConf.js @@ -60,6 +60,15 @@ exports.config = { // body, but is necessary if ng-app is on a descendant of rootElement: 'body', + // A callback function called once protractor is ready and available, and + // before the specs are executed + onPrepare: function() { + // At this point, global 'protractor' object will be set up, and jasmine + // will be available. For example, you can add a Jasmine reporter with: + // jasmine.getEnv().addReporter(new jasmine.JUnitXmlReporter( + // 'outputdir/', true, true)); + }, + // ----- Options to be passed to minijasminenode ----- jasmineNodeOpts: { // onComplete will be called just before the driver quits. diff --git a/spec/onPrepare/onPrepare_spec.js b/spec/onPrepare/onPrepare_spec.js new file mode 100644 index 000000000..39cb31b86 --- /dev/null +++ b/spec/onPrepare/onPrepare_spec.js @@ -0,0 +1,18 @@ +describe('tests that use the shortcuts set by the onPrepare in the config', + function() { + beforeEach(function() { + // ptor is in the globals thanks to the onPrepare callback function in the + // config. + ptor.get('app/index.html#/form'); + }); + + it('should find an element using elem instead of findElement', function() { + var greeting = ptor.elem(protractor.By.binding('{{greeting}}')); + expect(greeting.getText()).toEqual('Hiya'); + }); + + it('should find an element using the global by function', function() { + var greeting = ptor.elem(by.binding('{{greeting}}')); + expect(greeting.getText()).toEqual('Hiya'); + }); +}); diff --git a/spec/onPrepareConf.js b/spec/onPrepareConf.js new file mode 100644 index 000000000..55f11c7e6 --- /dev/null +++ b/spec/onPrepareConf.js @@ -0,0 +1,26 @@ +// The main suite of Protractor tests. +exports.config = { + seleniumServerJar: './selenium/selenium-server-standalone-2.35.0.jar', + chromeDriver: './selenium/chromedriver', + + seleniumAddress: 'http://localhost:4444/wd/hub', + + // Spec patterns are relative to this directory. + specs: [ + 'onPrepare/*_spec.js' + ], + + capabilities: { + 'browserName': 'chrome' + }, + + baseUrl: 'http://localhost:8000', + + onPrepare: function() { + var ptor = protractor.getInstance(); + ptor.elem = ptor.findElement; + ptor.elems = ptor.findElements; + global.by = protractor.By; + global.ptor = ptor; + } +};