How to run Cypress BDD Feature using TAGS in the Terminal without closing the test/browser for each Feature

I have a few feature files in my project and I need to execute only the specific cucumber tags (@Regression) from the feature file using Terminal. I could able to run the feature file using the tags. But the test/Browser window gets closed and open for each feature file. In this case, I have to write a login script in all the feature files to avoid this problem.

Expectation: Test/Browser should not be closed each time and Login should happen only at the beginning of the script execution.

Can someone help me to overcome this problem?


Solution 1:

Explanation

That you have to run the login for each Scenario in your Feature respectively is the expected behavior, since each test should be as independent as possible in itself.

In order not to have to add a login step for each Scenario again and again, there are so-called Backgrounds in Cucumber. Backgrounds describe steps that apply as a precondition for all Scenarios in a Feature.

Backgrounds behave like normal Scenarios, so for example you can create a Background in each of your Features with a Given step for the login so that it is automatically executed before each scenario.

Example

Each Feature would receive the following Background, which is then automatically executed once before each Scenario:

@SomeTag
Feature: Some Feature
    Background: User is logged in
        Given the user is logged in

    Scenario: Some first scenario
        Given ...
        When ...
        Then ...

    Scenario: Some second scenario
        Given ...
        When ...
        Then ...

The implementation of the step definition is then the same as for steps for your normal Scenarios and can be reused in all Features:

import { defineStep, Given } from 'cypress-cucumber-preprocessor/steps';

Given('the user is logged in', () => {
  // logic for login
});

// or more generic using defineStep
defineStep('the user is logged in', () => {
  // logic for login 
});

Regarding the logic for the login it is often suitable to use Cypress Custom Commands (Example Login for Azure AD)