0

I am able to get this in the cucumber version 1.X but upgrading cucumber to higher version not able to use (this) and it says scenario.getName() is not a function.

I require this because my test depends on the Scenario Name as it involves common function for different test data.

This is how I get the scenario name in the Cucumber version 1.x

`module.exports= function Steps() {
this.BeforeScenario(function (scenario, callback) {
        var currentScenarioName = scenario.getName();
        console.log(scenario.getName());
        callback(null);
    });
};`

And the other question is How to embed Text to my cucumber reports from my step Definition instead of doing in the hooks After function

Saravanan
  • 23
  • 1
  • 5

2 Answers2

0

In Cucumber 3.x to get the scenario name, you can use the first parameter passed to Before hook, which is a pickled version of Scenario object. Here is the code:

const { defineSupportCode } = require('cucumber');

defineSupportCode(function({Before}) {
    Before((scenario)=> {
      console.log('before scenario', scenario.name);
    });
})

you can print this scenario object to learn what other info contained in this object.

Here is the document that mentioned the "testCase" which is equal to the above scenario Parameter.

BTW, seems you are using both Cucumber 1.x or 3.x. You may want try CukeTest, which has ability to edit both versions, and provide code generation and validation.

Please also share how you insert text to report in your current version, I only aware of "attach" method in the World object that can insert images to the report.

Lean Prop
  • 111
  • 6
0

To expand on Lean Prop's answer, I found that in Cucumber 4.x (using 4.1) that this code works:

const { defineSupportCode } = require('cucumber');

defineSupportCode(function({Before}) {
    Before((scenario)=> {
      console.log('scenario.pickle.name);
    });
})
  • While this code snippet may solve the question, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) helps to improve the quality of your response. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Stefan Crain Apr 24 '18 at 20:14