kfairns
kfairns

Reputation: 3067

CucumberJS 2.0.0 How to get the Scenario Name from the Before Scenario Hook

The Problem

I have been using the output from the Before hook to name my screenshot files so that we have the name of the Feature and Scenario that the screenshot came from.

The output that I was getting in 1.3.0 allowed me to do a scenario.getName() to do this, however, the formatting from 1.3.0 has changed in 2.0.0

Has anyone figured out how to do this?

The Code

// hooks.js
defineSupportCode(function ({registerHandler, Before}) {

  Before(function (scenario, callback) {
        global.scenarioDetails = function(){
           return scenario;
        }
        callback();
  });

});


//otherFile.js
let name = scenarioDetails().<somethingHereToGrabTheName>;

Output from console.log()

ScenarioResult {
 duration: 8043,
  failureException: null,
  scenario:
   Scenario {
     feature:
      Feature {
        description: undefined,
        keyword: 'Feature',
        line: 2,
        name: 'Hello World',
        tags: [Object],
        uri: 'Path/to/my.feature',
        scenarios: [Object] },
     keyword: 'Scenario',
     lines: [ 3 ],
     name: 'Google Search',
     tags: [ [Object] ],
     uri: 'Path/to/my.feature',
     line: 3,
     description: undefined,
     steps: [ [Object], [Object], [Object] ] },
  status: 'passed',
  stepResults:
   [ StepResult {
       attachments: [],
       duration: 1,
       step: [Object],
       stepDefinition: [Object],
       status: 'passed' },
     StepResult {
       attachments: [],
       duration: 8042,
       step: [Object],
       stepDefinition: [Object],
       status: 'passed' } ] }

Upvotes: 2

Views: 3192

Answers (1)

kfairns
kfairns

Reputation: 3067

Answer Found

This is how I located the names for features, scenarios and steps:

  • For features: feature.name
  • For scenarios: scenario.scenario.name
  • For steps: step.name

Upvotes: 2

Related Questions