Cucumber tests. Get step regexp dynamically

306 views Asked by At

I want to get some statistics of how many times each step is used in cucumber test

So lets say I have a step definition like

this.When(/^"([^"]*)" is clicked$/, function (someElementId, callback){});

And 2 usages of this step (in one or more feature files)

"a" is clicked
"b" is clicked

I want to know that this step "([^"]*)" is clicked was used twice and get the times each execution took. I want to do it dynamically without touching actual step definitions.

I can get the step name like "a" was clicked in after step event, but I can't get a regexp. Ideally I would like to get smth like step.getRegexp():

 this.AfterStep(function (step, callback) {
   console.log(step.getName())// => prints "a" was clicked
   //need smth like step.getRegexp() to make "a" and "b" click identified as the same step
 }
1

There are 1 answers

3
thgaskell On

You could use the same regex to match the step's name.

let clickCounter = 0;

module.exports = function () {
  const LINK_IS_CLICKED_STEP_REGEXP = /^"([^"]*)" is clicked$/;

  this.When(LINK_IS_CLICKED_STEP_REGEXP, function (someElementId, callback){});

  this.AfterStep(function (step) {
    if (LINK_IS_CLICKED_STEP_REGEXP.test(step.getName())) {
      clickCounter++;
    }
  });
}