7

In case of error I want to be able to log the scenario name. I'm using cucumber.js and node.

Feature file

Scenario: As me I can go to google

Given that I have a computer

When I go to google

Then I see wondrous stuff

I have tried the code below, but the name comes back as an empty string.

When(/^I go to google$/, (scenario) => {
// do something
var scenarioName = scenario.name;
});

By stepping through the code I can see that it's a function. It has :

[[FunctionLocation]] = Object

[[Scopes]] = Scopes(6)

length = 2

name= ""

Community
  • 1
  • 1
rozza
  • 927
  • 2
  • 11
  • 24
  • For debugging: add console.log(util.inspect(scenario)) and see what scenario object contains. – teroi Aug 08 '18 at 12:39
  • 1
    By stepping through I can see that it's a function. If I do your console.log line it outputs [Function] but that's it. I've put the properties of the function that I see when stepping through the code in the original question. – rozza Aug 08 '18 at 13:02

3 Answers3

6

It seems like you can't do this in a Given or When, it has to be done in the Before or After hook:

Before((scenario) => {
const scenarioName = scenario.name;
console.log(`${scenarioName}`);
});
rozza
  • 927
  • 2
  • 11
  • 24
  • Once you get the name in Before hook, store them in the World object so that it can be retrieved in Given or When function statement. – Lean Prop Aug 29 '18 at 06:18
  • Looks like there is no longer a name attribute - it is now in scenario.pickle.name (and if you want to use it to create a filename you'll want to do something like `.toLowerCase().replace(/\s/g, '-')` – tschumann Aug 18 '20 at 02:14
  • 1
    As other responses suggested, the name is within `scenario.pickle.name` – fandasson Jan 19 '22 at 15:51
4

I could get the scenario name using this code:

Before((scenario: any) => {
    console.log(`Starting scenario ${scenario.sourceLocation.uri}:${scenario.sourceLocation.line} (${scenario.pickle.name})`);
});

As you can see, there's more in there than just the scenario name. The entire test, with all the steps, tags, etc is in the object. I'm using CucumberJS 5.0.3.

Peter
  • 13,733
  • 11
  • 75
  • 122
2

I found the scenario name using below code

console.log(`The scenario is ${JSON.stringify(scenario.pickle.name)}.`);
jjose
  • 113
  • 1
  • 1
  • 5