如何将 json 数据存储到赛普拉斯中的变量

How to store json data to a variable in Cypress

我正在尝试从 json 文件中获取员工 ID,格式如下,但它返回的错误如下 Cannot read property 'name' of null

如何检索用户名为“Devin Miller”的员工 ID?请帮忙

{
  "data": [
    {
      "role": "Software Engineer",
      "organization": "Google",
      "name": "Remi Arsenault",
      "emplyeeID":"526321",
      "project": "Stadia"
    },
    {
      "role": "Data Analyst",
      "organization": "Google",
      "emplyeeID": "526322",
      "name": "Devin Miller",
      "project": "Stadia"
    }
  ]
}

下面是我的代码

export function GetUserData(employeeName){     
    cy
      .fixture('users')
      .its('data')
      .then((users) => {     
        Users = users.find(user => user.name === employeeName);
      })
  }

export var Users = null;

现在,如果我尝试执行以下操作,则会返回错误

var employeeID = Users["Remi Arsenault"].emplyeeID

我认为您对异步代码与 snyc 代码有一些不好的期望。

此外,为了访问夹具数据,您可以尝试使用此方法代替 .its('data) :

cy.fixture('users').then((usersJson) => {
    let foundUser = usersJson.data.find(user => user.name === 'Remi Arsenault');
    let emloyeeID = foundUser.employeeID
    ... ['whatever else you want to do can go here inside the then callback.']
});