rozza
rozza

Reputation: 1016

How to get Scenario name from cucumber js?

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= ""

Upvotes: 7

Views: 9287

Answers (3)

jjose
jjose

Reputation: 123

I found the scenario name using below code

console.log(`The scenario is ${JSON.stringify(scenario.pickle.name)}.`);

Upvotes: 3

Peter
Peter

Reputation: 14098

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.

Upvotes: 4

rozza
rozza

Reputation: 1016

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}`);
});

Upvotes: 6

Related Questions