Search code examples
variablescompareeachcypresslet

How to compare text values of two lists of elements in cypress?


I have code like this:

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') has 10 elements and I want to compare all of it one by one. But what i get is 10 times same value with firstUserPrices(it's last value form list) what am I doing wrong here?


Solution

  • I figured it out, had to put firstUserPrices into an array and push elements to that array

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