Search code examples
cucumber

How can I prevent Cucumber to run @before in all glue files?


this question is related to [https://stackoverflow.com/questions/32434284/why-does-cucumber-run-before-in-all-glue-code-files] This was the solution at that time: Solved my problem by grouping the glue "before" methods with tags and then adding the depending tags to the featurefiles

I got the same problem and want to solve it, but got no idea what this solution wants me to do.

Has someone an idea and can explain it to me, or has an example, or a link? Bye Jochen


Solution

  • @Before hook purpose is to execute set of steps before each scenario. Here you can achieve in this way.

    Scenario:

    @Regression
    Scenario: Test the login functionality
    Given launch url
    And enter credentials 
    Then click on login
    

    Before Hook: Now you can put a condition in @Before method to control what you want to execute.

    @Before
    public void setup(Scenario scenario) {
        if(scenario.getSourceTagNames().contains("@Regression")) {
            //perform steps what you need.
        }
       }
    

    In case, you want to put multiple conditions then it should be this way.

    @Before
    public void setup(Scenario scenario) {
        Collection<String> scenarioTags = scenario.getSourceTagNames();
          if(scenarioTags.contains("@Regression") || scenarioTags.contains("@Smoke")) {
                //perform steps what you need.
            }
          }