From e9e021a0496327e1ae1c9fbe3cf916acae28d7b3 Mon Sep 17 00:00:00 2001 From: gd46 Date: Sat, 7 Oct 2017 19:00:22 -0400 Subject: [PATCH] (refactor) Update testCase to include pickle object --- docs/support_files/hooks.md | 40 +++++++++++++++++++++++++++++++++ features/hook_parameter.feature | 10 +++++++++ src/runtime/test_case_runner.js | 4 +++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/docs/support_files/hooks.md b/docs/support_files/hooks.md index e96c7430e8..b1d8be61f1 100644 --- a/docs/support_files/hooks.md +++ b/docs/support_files/hooks.md @@ -32,6 +32,46 @@ defineSupportCode(function({After, Before}) { }); ``` +Before and After hooks return a testCase which contains a result(pass or fail), sourceLocation(test file uri and line number), and pickle(info on the current scenario) + +```javascript +var {defineSupportCode, Status} = require('cucumber'); + +defineSupportCode(function({Before}) { + // Synchronous + Before(function (testCase) { + + /* + * Check if scenarios has failed + * testCase.result.status === Status.FAILED + * + * testCase.pickle + * contains tags, name, locations, language, steps of the current scenarios + * + * testCase.pickle.name + * contains current scenario name + * + * testCase.pickle.tags + * contains an object array with the current scenarios tags + * + * testCase.sourceLocation + * contains uri and line number, uri is path to feature file, line number will give you current scenario + * + */ + + /* + * How to check current scenario tags + */ + + let tags = testCase.pickle.tags + _.forEach(tags, (tag.name) => { + // Provides you with the name of each tag + }); + + }); +}); +``` + ## Tagged hooks Hooks can be conditionally selected for execution based on the tags of the scenario. diff --git a/features/hook_parameter.feature b/features/hook_parameter.feature index 445bcbb596..11b2923ff5 100644 --- a/features/hook_parameter.feature +++ b/features/hook_parameter.feature @@ -23,6 +23,8 @@ Feature: Hook Parameters defineSupportCode(({Before}) => { Before(function(testCase) { console.log(testCase.sourceLocation.uri + ":" + testCase.sourceLocation.line) + console.log('tags: ', testCase.pickle.tags); + console.log('name: ', testCase.pickle.name); }) }) """ @@ -30,6 +32,8 @@ Feature: Hook Parameters Then the output contains the text: """ features/my_feature.feature:2 + tags: [] + name: a scenario """ @spawn @@ -65,6 +69,8 @@ Feature: Hook Parameters message += "did not fail" } console.log(message) + console.log('tags: ', testCase.pickle.tags); + console.log('name: ', testCase.pickle.name); }) }) """ @@ -73,8 +79,12 @@ Feature: Hook Parameters And the output contains the text: """ features/my_feature.feature:2 did not fail + tags: [] + name: a scenario """ And the output contains the text: """ features/my_feature.feature:5 failed + tags: [] + name: another scenario """ diff --git a/src/runtime/test_case_runner.js b/src/runtime/test_case_runner.js index d97f73845e..8341296b5f 100644 --- a/src/runtime/test_case_runner.js +++ b/src/runtime/test_case_runner.js @@ -154,11 +154,13 @@ export default class TestCaseRunner { this.emitPrepared() this.emit('test-case-started', {}) await this.runHooks(this.beforeHookDefinitions, { - sourceLocation: this.testCaseSourceLocation + sourceLocation: this.testCaseSourceLocation, + pickle: this.testCase.pickle }) await this.runSteps() await this.runHooks(this.afterHookDefinitions, { sourceLocation: this.testCaseSourceLocation, + pickle: this.testCase.pickle, result: this.result }) this.emit('test-case-finished', { result: this.result })