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

Commit

Permalink
feat(cli): add an onPrepare callback to the config
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jnizet authored and juliemr committed Sep 24, 2013
1 parent 6490285 commit 1b7675a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
9 changes: 9 additions & 0 deletions referenceConf.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ exports.config = {
// body, but is necessary if ng-app is on a descendant of <body>
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.
Expand Down
18 changes: 18 additions & 0 deletions spec/onPrepare/onPrepare_spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
26 changes: 26 additions & 0 deletions spec/onPrepareConf.js
Original file line number Diff line number Diff line change
@@ -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;
}
};

0 comments on commit 1b7675a

Please sign in to comment.