open-ecommerce.org
open-ecommerce.org

Reputation: 1814

With Cypress get fixture data using a command

I have the following fixture file call myfile

 [
  {
    "instance": "feature",
    "baseUrl": "http://1.1.1.1",
  },
  {
    "instance": "non-feature",
    "baseUrl": "http://1.1.1.1",
  }
]

and I have the following command to get the baseUrl

Cypress.Commands.add('getBaseUrl',() => {
  let baseUrl;
  cy.fixture('myfile').then(function (defaultdata) {
    this.defaultdata = defaultdata;
    var index = this.defaultdata.findIndex(obj => obj.instance == 
    Cypress.env('instance'));
    baseUrl =this.defaultdata[index].baseUrl;
    return baseUrl;
  })
})

But when i try to use it I get: log Object{5} instead of the value of baseUrl

describe('Tests', () => {
  it('is redirected on visit to /dashboard when not authenticated', function () {
      cy.log(cy.getBaseUrl());
    })
})

Probably some syncronic logic but i can't manage to make it. If i wrap baseUrl in the command i can see the value but no luck passing it trough.

Upvotes: 0

Views: 537

Answers (1)

open-ecommerce.org
open-ecommerce.org

Reputation: 1814

The problem was the way i was consuming the command. Like this work's fine:

describe('Tests', () => {
  it('is redirected on visit to /dashboard when not authenticated', function () {
    cy.getBaseUrl().then(data => {
      cy.log(data);
    })
    })
})

Upvotes: 1

Related Questions