如何比较柏木中两个元素列表的文本值



我有这样的代码:

let firstUserPrices
cy.get('.fw-price').each($value => {
    firstUserPrices = $value.text()
})
let secondUserPrices
cy.get('.fw-price').each($value => {
    secondUserPrices = $value.text()
    expect(firstUserPrices, 'PRICES').to.equal(secondUserPrices)
})

cy.get('.fw-price'(有10个元素,我想逐一比较所有元素。但我得到的值是firstUserPrices(它是最后一个值表单列表(的10倍,我做错了什么?

您可以尝试这样做,假设两个列表的长度相等-

cy.get('list1').then((list1) => {
   cy.get('list2').then((list2) => {
      for (var i = 0, i < list1.length, i++) {
         expect(list1.eq(i).text()).to.equal(list2.eq(i).text())
      }
   })
})

我想明白了,必须将firstUserPrices放入一个数组中,并将元素推送到该数组

let firstUserPrices = []
cy.get('[data-t="my-price"] span span').each($value => {
    firstUserPrices.push($value.text()) 
})
//do some stuff here
cy.get('[data-t="my-price"] span span').each(($value,index) => {
    const secondUserPrices = $value.text()
    expect(firstUserPrices[index], 'PRICES').to.not.equal(secondUserPrices)
})

来自https://glebbahmutov.com/cypress-examples/6.5.0/commands/assertions.html#comparing-阵列

const arr = ['Apples', 'Bananas', 'Grapes']
// assert that cy.wrap yields the same array reference
// as we passed into it
cy.wrap(arr).should('equal', arr)
// assert the yielded array has the expected items inside
cy.wrap(arr)
  .invoke('reverse')
  .should('deep.equal', ['Grapes', 'Bananas', 'Apples'])

最新更新