Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
916 views
in Technique[技术] by (71.8m points)

automated tests - 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.

question from:https://stackoverflow.com/questions/66067615/with-cypress-get-fixture-data-using-a-command

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
    })
    })
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...