Search code examples
protractorbddcucumberjs

Cucumber js - Tagged BeforeFeature


I wish to tag my BeforeFeature hooks in CucumberJS - Protractor tests such that

  1. the hook runs only for feature files with that tag
  2. the code inside the hook runs just once before the start of feature file and not before every scenario

e.g. - Login as a particular user in BeforeFeature only for a feature file 'abc.feature'

The existing implementation of BeforeFeature in cucumberjs is as below -

this.registerHandler('BeforeFeature', function (feature) {
        //do common action before feature file execution
    });

I am not sure if it takes tags as arguments. Is there any other way to specify tags at BeforeFeature level?


Solution

  • this.BeforeFeature(function(feature) {
        feature.getTags().forEach(function (tag) {
            if(tag.getName() === '@myTag') {
                //do common action before feature file execution
            }
        });
    });
    

    OR

    this.BeforeFeature(function(feature) {        
        if(feature.getName() === 'ABC') {//Gherkin file => Feature: ABC
            //do common action before feature file execution
        }
    });