【问题标题】:Cypress - find nested properties in json file赛普拉斯 - 在 json 文件中查找嵌套属性
【发布时间】:2021-06-17 09:36:59
【问题描述】:

我正在努力应对以下情况。我通过网页创建一个新用户,并将其保存到 json 文件中。但是我在 cypress 中的测试无法在文件中搜索“深度”。

这就是我的 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",
}

【问题讨论】:

    标签: json cypress fixtures


    【解决方案1】:

    customers(另存为别名testdata)是一个数组,因此您需要对数组进行索引。

    如果你知道你想要哪个客户,例如选择第四个客户

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 2022-11-10
      相关资源
      最近更新 更多