Search code examples
cucumbercypresscypress-cucumber-preprocessor

Can a Background step relate to only certain test scenarios in a Cypress feature file?


Below is a feature in my Cypress test framework:

Background:
    Given User is on the Customer Management landing Page


Scenario: #6 Filter by Customer Name
    When User search for "Jane Doe" in the field Search Customer Name
    Then the search by Customer Name returns only the information related to that customer

Scenario #6 above searches for a specific user & validates their information is displayed on the UI.

The issue I'm facing is that sometimes the database which I'm getting data from removes all data, so if I run this test & there's no data returned (Jane Doe doesn't exist), then the test will fail.

What I want to do is add a step where I can do the following:

  • Check if "Jane Doe" user exists
  • If "Jane Doe" exists, do nothing.
  • If "Jane Doe" doesn't exist, create the user, so I can search for them in Scenario #6.

The problem I'm facing is that there are multiple scenario's in this feature file.

Does it make sense to add this step to the Background like so, even if the "Jane Doe" user is only used in one specific test scenario:

Background:
    Given "Jane Doe" user exists
    And User is on the Customer Management landing Page

The "Jane Doe" user is only related to one test scenario here.


Solution

  • If you have a need for two backgrounds then you have a very good reason to make a new feature file. So from what I've seen in your post

    Feature: Jane Doe features
    
    A set of features ... using Jane Doe as an exemplar.
    
    Background: 
      Given Jane Doe ...
      And User is on the Customer Management landing Page
    
    Scenario: Filter by Jane Doe
      ...
    

    Rule of thumb: Need two backgrounds, you need a new feature.