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

Commit cf9a26f

Browse files
committed
feat(plugins): allow plugins.postTest to know what test just ran
Also reorganized plugin tests, as the number of configs was getting out of hand
1 parent 0f80696 commit cf9a26f

25 files changed

+124
-26
lines changed

docs/plugins.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ exports.postResults = function(config) {};
124124
*
125125
* @param {Object} config The plugin configuration object.
126126
* @param {boolean} passed True if the test passed.
127+
* @param {Object} testInfo information about the test which just ran.
127128
*
128129
* @return Object If an object is returned, it is merged with the Protractor
129130
* result object. May return a promise.
130131
*/
131-
exports.postTest = function(config, passed) {};
132+
exports.postTest = function(config, passed, testInfo) {};
132133

133134
/**
134135
* Used when reporting results.

lib/frameworks/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ exports.run = function(runner, specs)
1717
Requirements
1818
------------
1919

20-
- `runner.emit` must be called with `testPass` and `testFail` messages.
20+
- `runner.emit` must be called with `testPass` and `testFail` messages. These
21+
messages must be passed a `testInfo` object, with a `name` and `category`
22+
property. The `category` property could be the name of the `describe` block
23+
in jasmine/mocha, the `Feature` in cucumber, or the class name in something
24+
like jUnit. The `name` property could be the name of an `it` block in
25+
jasmine/mocha, the `Scenario` in cucumber, or the method name in something
26+
like jUnit.
2127

2228
- `runner.runTestPreparer` must be called before any tests are run.
2329

lib/frameworks/cucumber.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,28 @@ exports.run = function(runner, specs) {
7070
// Add a listener into cucumber so that protractor can find out which
7171
// steps passed/failed
7272
var addResultListener = function(formatter) {
73+
var feature = { getName: function() { return ''; } };
74+
var originalHandleBeforeFeatureEvent = formatter.handleBeforeFeatureEvent;
75+
formatter.handleBeforeFeatureEvent = function(event, callback) {
76+
feature = event.getPayloadItem('feature');
77+
if (typeof originalHandleAfterScenarioEvent == 'function') {
78+
originalHandleBeforeFeatureEvent.apply(formatter, arguments);
79+
} else {
80+
callback();
81+
}
82+
}
7383
var originalHandleAfterScenarioEvent = formatter.handleAfterScenarioEvent;
7484
formatter.handleAfterScenarioEvent = function(event, callback) {
75-
var scenario = event.getPayloadItem('scenario');
76-
stepResults.description = scenario.getName();
85+
var scenarioInfo = {
86+
name: event.getPayloadItem('scenario').getName(),
87+
category: feature.getName()
88+
};
89+
stepResults.description = scenarioInfo.name;
7790
if (scenarioFailed) {
7891
++failedCount;
79-
runner.emit('testFail');
92+
runner.emit('testFail', scenarioInfo);
8093
} else {
81-
runner.emit('testPass');
94+
runner.emit('testPass', scenarioInfo);
8295
}
8396

8497
testResult.push(stepResults);

lib/frameworks/jasmine.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ exports.run = function(runner, specs) {
3030
this.startTime = new Date();
3131
};
3232
RunnerReporter.prototype.reportSpecResults = function(spec) {
33+
var specInfo = {
34+
name: spec.suite.getFullName(),
35+
category: spec.description
36+
};
3337
if (spec.results().passedCount) {
34-
this.emitter.emit('testPass');
38+
this.emitter.emit('testPass', specInfo);
3539
} else if (spec.results().failedCount) {
36-
this.emitter.emit('testFail');
40+
this.emitter.emit('testFail', specInfo);
3741
}
3842

3943
var entry = {

lib/frameworks/jasmine2.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ RunnerReporter.prototype.specStarted = function() {
1717
};
1818

1919
RunnerReporter.prototype.specDone = function(result) {
20+
var specInfo = {
21+
name: result.description,
22+
category: result.fullName.slice(0, -result.description.length).trim()
23+
};
2024
if (result.status == 'passed') {
21-
this.emitter.emit('testPass');
25+
this.emitter.emit('testPass', specInfo);
2226
} else if (result.status == 'failed') {
23-
this.emitter.emit('testFail');
27+
this.emitter.emit('testFail', specInfo);
2428
this.failedCount++;
2529
}
2630

lib/frameworks/mocha.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ exports.run = function(runner, specs) {
6565
});
6666

6767
mochaRunner.on('pass', function(test) {
68-
runner.emit('testPass');
68+
var testInfo = {
69+
name: test.title,
70+
category: test.fullTitle().slice(0, -test.title.length).trim()
71+
};
72+
runner.emit('testPass', testInfo);
6973
testResult.push({
7074
description: test.title,
7175
assertions: [{
@@ -76,7 +80,11 @@ exports.run = function(runner, specs) {
7680
});
7781

7882
mochaRunner.on('fail', function(test) {
79-
runner.emit('testFail');
83+
var testInfo = {
84+
name: test.title,
85+
category: test.fullTitle().slice(0, -test.title.length).trim()
86+
};
87+
runner.emit('testFail', testInfo);
8088
testResult.push({
8189
description: test.title,
8290
assertions: [{

lib/runner.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,11 @@ Runner.prototype.run = function() {
316316
// an event emitter).
317317
var pluginPostTestPromises = [];
318318

319-
self.on('testPass', function() {
320-
pluginPostTestPromises.push(plugins.postTest(true));
319+
self.on('testPass', function(testInfo) {
320+
pluginPostTestPromises.push(plugins.postTest(true, testInfo));
321321
});
322-
self.on('testFail', function() {
323-
pluginPostTestPromises.push(plugins.postTest(false));
322+
self.on('testFail', function(testInfo) {
323+
pluginPostTestPromises.push(plugins.postTest(false, testInfo));
324324
});
325325

326326
return require(frameworkPath).run(self, self.config_.specs).

scripts/test.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ var passingTests = [
1919
'node lib/cli.js spec/suitesConf.js --suite okmany',
2020
'node lib/cli.js spec/suitesConf.js --suite okspec',
2121
'node lib/cli.js spec/suitesConf.js --suite okmany,okspec',
22-
'node lib/cli.js spec/pluginsBasicConf.js',
23-
'node lib/cli.js spec/pluginsFullConf.js',
22+
'node lib/cli.js spec/plugins/smokeConf.js',
23+
'node lib/cli.js spec/plugins/multiPluginConf.js',
24+
'node lib/cli.js spec/plugins/jasmine1PostTestConf.js',
25+
'node lib/cli.js spec/plugins/jasmine2PostTestConf.js',
26+
'node lib/cli.js spec/plugins/mochaPostTestConf.js',
27+
'node lib/cli.js spec/plugins/cucumberPostTestConf.js',
2428
'node lib/cli.js spec/interactionConf.js',
2529
'node lib/cli.js spec/directConnectConf.js',
2630
'node lib/cli.js spec/restartBrowserBetweenTestsConf.js',

spec/errorTest/pluginsFailingConf.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ exports.config = {
99

1010
// Spec patterns are relative to this directory.
1111
specs: [
12-
'../plugins/fail_spec.js'
12+
'../plugins/specs/fail_spec.js'
1313
],
1414

1515
capabilities: env.capabilities,
@@ -23,8 +23,8 @@ exports.config = {
2323

2424
// Plugin patterns are relative to this directory.
2525
plugins: [{
26-
path: '../plugins/basic_plugin.js'
26+
path: '../plugins/plugins/basic_plugin.js'
2727
}, {
28-
path: '../plugins/failing_plugin.js'
28+
path: '../plugins/plugins/failing_plugin.js'
2929
}]
3030
};

spec/plugins/cucumberPostTestConf.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.config = require('./postTestConfTemplate')('cucumber');

spec/plugins/features/simple.feature

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Feature: category
2+
This is spec does nothing
3+
Scenario: name

spec/plugins/jasmine1PostTestConf.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.config = require('./postTestConfTemplate')('jasmine');

spec/plugins/jasmine2PostTestConf.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.config = require('./postTestConfTemplate')('jasmine2');

spec/plugins/mochaPostTestConf.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.config = require('./postTestConfTemplate')('mocha');

spec/pluginsFullConf.js renamed to spec/plugins/multiPluginConf.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var env = require('./environment.js');
1+
var env = require('../environment.js');
22

33
// A small suite to make sure the full functionality of plugins work
44
exports.config = {
@@ -8,7 +8,7 @@ exports.config = {
88

99
// Spec patterns are relative to this directory.
1010
specs: [
11-
'plugins/bigger_spec.js'
11+
'specs/bigger_spec.js'
1212
],
1313

1414
capabilities: env.capabilities,
@@ -24,7 +24,7 @@ exports.config = {
2424
plugins: [{
2525
path: 'plugins/basic_plugin.js'
2626
}, {
27-
path: 'plugins/test_plugin.js'
27+
path: 'plugins/async_plugin.js'
2828
}, {
2929
inline: {
3030
setup: function() {
File renamed without changes.
File renamed without changes.
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
exports.postTest = function(config, passed, testInfo) {
2+
var nameCorrect = testInfo.name == 'name';
3+
var categoryCorrect = testInfo.category == 'category';
4+
return {
5+
failedCount: passed && nameCorrect && categoryCorrect ? 0 : 1,
6+
specResults: [{
7+
description: 'make sure postTest passed correct information',
8+
assertions: [{
9+
passed: passed,
10+
errorMsg: passed ? undefined : '`passed` should have been `true`, but' +
11+
' got `' + JSON.stringify(passed) + '`'
12+
}, {
13+
passed: nameCorrect,
14+
errorMsg: nameCorrect ? undefined : '`testInfo.name` should have been' +
15+
' `"name"`, but got `' + JSON.stringify(testInfo.name) + '`'
16+
}, {
17+
passed: categoryCorrect,
18+
errorMsg: categoryCorrect ? undefined : '`testInfo.category` should ' +
19+
'have been `"category"`, but got `' +
20+
JSON.stringify(testInfo.category) + '`'
21+
}],
22+
duration: 0
23+
}]
24+
};
25+
}

spec/plugins/postTestConfTemplate.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var env = require('../environment.js');
2+
3+
module.exports = function(framework) {
4+
return {
5+
mockSelenium: true,
6+
7+
framework: framework,
8+
9+
specs: [
10+
framework != 'cucumber' ? 'specs/simple_spec.js' :
11+
'features/simple.feature'
12+
],
13+
14+
capabilities: env.capabilities,
15+
16+
baseUrl: env.baseUrl,
17+
18+
plugins: [{
19+
path: 'plugins/post_test_plugin.js'
20+
}]
21+
};
22+
};

spec/pluginsBasicConf.js renamed to spec/plugins/smokeConf.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var env = require('./environment.js');
1+
var env = require('../environment.js');
22

33
// A small suite to make sure the basic functionality of plugins work
44
// Tests the (potential) edge case of exactly one plugin being used
@@ -9,7 +9,7 @@ exports.config = {
99

1010
// Spec patterns are relative to this directory.
1111
specs: [
12-
'plugins/basic_spec.js'
12+
'specs/smoke_spec.js'
1313
],
1414

1515
capabilities: env.capabilities,
File renamed without changes.
File renamed without changes.

spec/plugins/specs/simple_spec.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
describe('category', function() {
2+
it('name', function() {
3+
});
4+
});
File renamed without changes.

0 commit comments

Comments
 (0)