如何比较赛普拉斯中两个元素列表的文本值?

时间:2020-10-25 18:54:40

标签: variables compare each cypress let

我有这样的代码:

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倍相同的值吗?

2 个答案:

答案 0 :(得分:0)

您可以尝试一下,假设两个列表的长度相等-

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())
      }
   })
})

答案 1 :(得分:0)

我弄清楚了,不得不将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)
})