Raj
Raj

Reputation: 131

how to store fixture file data in an object

i have a fixture file that i asynchronously read multiple times at different steps of my test scenario. I would like to read it once and store it as an object that i could use multiple times. below is my current set up-

 Scenario: Organisation admin can successfully add a passenger to a route
                    Given I click add a passenger button on the passenger modal
                    When I fill out the passenger data and set the exclusion dates from "routeAndPassengerBookingData"
                    And click the confirm button on add passenger modal
                    Then the pre-booking information is correct as per "routeAndPassengerBookingData"

i read the file like so in step defs-

 cy.fixture('routeAndPassengerBookingData').then((data) => {
          data. do stuff
          });
        });
    

fixture file-

 {
      "route": {
        "name": "Reykjavík",
        "schedule": {
          "relativeStartDate": {
            "difference": "1",
            "period": "day"
          },
          "relativeEndDate": {
            "difference": 6,
            "period": "month"
          },
          "daysOfWeek": ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"],
          "excludeDatesFromRange": [
            { "position": 1, "from": "START" },
            { "position": 2, "from": "END" }
          ]
        }
      }
    }

i would like to pass in object key instead of file name in the feature file steps to access the data. any help highly appreciated! thanks in advance!

Upvotes: 0

Views: 785

Answers (1)

TesterDick
TesterDick

Reputation: 10525

It's difficult with Cucumber tests, but a couple of suggestions:

  • store as an alias

    cy.fixture('routeAndPassengerBookingData').as('bookingData')
    
  • store as an environment variable

    cy.fixture('routeAndPassengerBookingData').then((data) => {
      Cypress.env('bookingData', data)
    })
    
  • store on the Cypress global

    cy.fixture('routeAndPassengerBookingData').then((data) => {
      Cypress.bookingData =  data
    })
    

But honestly, you're wasting time on this - Cypress already ensures it's only read once during cy.fixture('routeAndPassengerBookingData').

Any further calls to cy.fixture with the same key are read from the cache.

Upvotes: 1

Related Questions