【问题标题】:In cypress, how can we access multiple users in the fixture file?在 cypress 中,我们如何访问夹具文件中的多个用户?
【发布时间】:2021-01-14 05:52:06
【问题描述】:

这是名为users.json的夹具文件中的数据 当我们从赛普拉斯访问这些数据时,我们如何访问,因为它有多个同名数据。

[
  {
    "id": 1,
    "name": "Jan Test",
    "username": "JTest",
    "email": "SJtest@testing.com",
  },    
  {
    "id": 2,
    "name": "Mark well",
    "username": "Mwell",
    "email": "mwell@makv.com",
  },
  {
    "id": 3,
    "name": "Geet rwar",
    "username": "Grwar",
    "email": "grwar@mail.com"
  }
]

【问题讨论】:

  • 数组中有3个元素,您可以一个接一个地访问它们,或者根据您定义的某些条件选择一个。你要什么数据?在您的问题中更具体。
  • @pavelsaman 您对问题的编辑假定user.json 中有数组分隔符,但这可能不正确。
  • @eric99:是的,我知道,但以前的版本甚至不是有效的 json,所以我希望这就是 OP 的意思。但我明白,也许我不应该更新这个。

标签: cypress fixtures


【解决方案1】:

我建议使用Array.prototype.find()。例如

cy.fixture('users.json')
    .then(array => {
        const userINeed = aray.find(element => element.id === 1)
    })

在此代码中 - array 是来自夹具的数据,尽管它需要位于夹具文件夹中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    相关资源
    最近更新 更多