赛普拉斯 - 在 json 文件中查找嵌套属性

Cypress - find nested properties in json file

我正在为以下情况而苦苦挣扎。我通过网页创建了一个新用户,并将其保存到 json 文件中。但是我在 cypress 中的测试无法在文件中搜索 'deep'。

这是我的 json 文件的样子:

{
  "customers" : [ {
    "customerId" : "123456",
    ...
   } ],
  "error" : ""
}

这就是我在测试中加载这个 json 文件的方式:

beforeEach('Load fixture', function () {

    cy.fixture('registerNewCustomers').its('customers').as('testdata');
    }); 
    ...
    cy.get('@testdata').then((testdata) => {

            cy.get('#inputLoginID').type(testdata.customerId);

我的测试无法读取嵌套的 'customerId',但如果我将它向上移动一级到“客户”级别,我的测试就会找到它及其值。

{
  "customers" : [ {
    ...
    ...
   } ],
  "error" : "",
  "customerId" : "123456",
}

customers(保存为别名testdata)是一个数组,所以你需要索引数组。

如果您知道您想要哪个客户,例如选择第 4 个客户

cy.get('#inputLoginID').type(testdata[3].customerId)  

如果您刚刚添加了客户,因此想要最后一个,

cy.get('#inputLoginID').type(testdata[testdata.length -1].customerId)