【问题标题】:With Cypress get fixture data using a command使用 Cypress 使用命令获取夹具数据
【发布时间】:2023-03-21 06:22:01
【问题描述】:

我有以下夹具文件调用 myfile

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

我有以下命令来获取 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;
  })
})

但是当我尝试使用它时,我得到:log Object{5} 而不是 baseUrl 的值

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

可能是一些同步逻辑,但我无法做到。如果我将 baseUrl 包装在命令中,我可以看到该值,但没有运气通过它。

【问题讨论】:

    标签: automated-tests cypress


    【解决方案1】:

    问题是我使用命令的方式。喜欢这个作品就好了:

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

    【讨论】:

      猜你喜欢
      • 2012-06-05
      • 1970-01-01
      • 2022-07-07
      • 2019-10-29
      • 2020-11-02
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      相关资源
      最近更新 更多