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

Commit 1b7675a

Browse files
jnizetjuliemr
authored andcommitted
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.
1 parent 6490285 commit 1b7675a

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

lib/cli.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,19 @@ var startJasmineTests = function() {
154154
// Export protractor to the global namespace to be used in tests.
155155
global.protractor = protractor;
156156

157-
// Set up the Jasmine WebDriver Adapter
157+
// Set up the Jasmine WebDriver Adapter.
158158
require('../jasminewd');
159159

160160
var options = config.jasmineNodeOpts;
161161
originalOnComplete = options.onComplete;
162162
options.onComplete = cleanUp;
163163

164+
// Let the configuration configure the protractor instance before running
165+
// the tests.
166+
if (config.onPrepare) {
167+
config.onPrepare();
168+
}
169+
164170
minijn.executeSpecs(options);
165171
});
166172
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"bin": "bin/protractor",
3232
"main": "lib/protractor.js",
3333
"scripts": {
34-
"test": "node lib/cli.js spec/basicConf.js; node lib/cli.js spec/altRootConf.js; node_modules/.bin/minijasminenode jasminewd/spec/adapterSpec.js"
34+
"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"
3535
},
3636
"version": "0.9.0"
3737
}

referenceConf.js

+9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ exports.config = {
6060
// body, but is necessary if ng-app is on a descendant of <body>
6161
rootElement: 'body',
6262

63+
// A callback function called once protractor is ready and available, and
64+
// before the specs are executed
65+
onPrepare: function() {
66+
// At this point, global 'protractor' object will be set up, and jasmine
67+
// will be available. For example, you can add a Jasmine reporter with:
68+
// jasmine.getEnv().addReporter(new jasmine.JUnitXmlReporter(
69+
// 'outputdir/', true, true));
70+
},
71+
6372
// ----- Options to be passed to minijasminenode -----
6473
jasmineNodeOpts: {
6574
// onComplete will be called just before the driver quits.

spec/onPrepare/onPrepare_spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
describe('tests that use the shortcuts set by the onPrepare in the config',
2+
function() {
3+
beforeEach(function() {
4+
// ptor is in the globals thanks to the onPrepare callback function in the
5+
// config.
6+
ptor.get('app/index.html#/form');
7+
});
8+
9+
it('should find an element using elem instead of findElement', function() {
10+
var greeting = ptor.elem(protractor.By.binding('{{greeting}}'));
11+
expect(greeting.getText()).toEqual('Hiya');
12+
});
13+
14+
it('should find an element using the global by function', function() {
15+
var greeting = ptor.elem(by.binding('{{greeting}}'));
16+
expect(greeting.getText()).toEqual('Hiya');
17+
});
18+
});

spec/onPrepareConf.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// The main suite of Protractor tests.
2+
exports.config = {
3+
seleniumServerJar: './selenium/selenium-server-standalone-2.35.0.jar',
4+
chromeDriver: './selenium/chromedriver',
5+
6+
seleniumAddress: 'http://localhost:4444/wd/hub',
7+
8+
// Spec patterns are relative to this directory.
9+
specs: [
10+
'onPrepare/*_spec.js'
11+
],
12+
13+
capabilities: {
14+
'browserName': 'chrome'
15+
},
16+
17+
baseUrl: 'http://localhost:8000',
18+
19+
onPrepare: function() {
20+
var ptor = protractor.getInstance();
21+
ptor.elem = ptor.findElement;
22+
ptor.elems = ptor.findElements;
23+
global.by = protractor.By;
24+
global.ptor = ptor;
25+
}
26+
};

0 commit comments

Comments
 (0)