How to query values from elements on web page and pair them up

Viewed 24

I have a list of products on a web page some of which have discounts. I want to get the old price and new price as pairs so I can compare the values, calculate the discount etc. I'm having trouble joining the values and returning them out of the selectors.

The HTML is something like <span class="el1"></span><span class="el2"></span>.

My test is

cy.get('.el1').then($el1 => {
  cy.get('.el2').then($el2 => {
    return [$el1, $el2]
  })
})

// analyze pairs here
1 Answers

A .map() on the element lists will give you pairs.

Once you have the result, put it in an alias.

cy.get('.el1').then($el1 => {
  cy.get('.el2').then($el2 => {
    const pairs = [...$el1].map((e, i) => {
      return [+e.innerText, +[...$el2][i].innerText];
    })
    cy.wrap(pairs).as('pairs')
  })
})
cy.get('@pairs')
  .then(pairs => {
    // analyse the pairs
  })
Related