Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...