Yoooo
Yoooo

Reputation: 101

How to take text from elements to use it later in cypress?

I need to take 2 lists of text values on page to use each of them in different cheking. I do it by xpath

    cy.xpath('//tbody/tr[position()<10]/td[6]/span').then(items => {
        cy.wrap(items).as('multipleList')
    })

    cy.xpath('//tbody/tr[position()<21][position()>15]/td[6]/span').then(items => {
        cy.wrap(items).as('commaList')
    })

    cy.get('@multipleList').each((qwer, index) => {..........})

    cy.get('@commaList').each((qwer, index) => {..........})

But after using the first list (@multipleList), it shows error that the second list (@commaList) is empty.

If I swap them, then @commaList is being executed OK, but @multipleList shows the same error.

Upvotes: 1

Views: 100

Answers (2)

Fody
Fody

Reputation: 31904

Try a combined selection and iteration only once

cy.xpath('//tbody/tr').as('list')

cy.get('@list').each(($rows, rowIndex) => {

  if (rowIndex < 9) {
    const $span = $rows[index].find('td:eq(6) span')
    ...
  }

  if (rowIndex > 14 && rowIndex < 20) {
    const $span = $rows[index].find('td:eq(6) span')
    ...
  }
})

Upvotes: 1

With the test as you've shown, it's not possible.

If you have an error it's the processing in .each() causing page changes (2nd list becomes invalid).

Try to reorder the commands

cy.xpath('//tbody/tr[position()<10]/td[6]/span').as('multipleList')

cy.get('@multipleList').each((qwer, index) => {..........})


cy.xpath('//tbody/tr[position()<21][position()>15]/td[6]/span').as('commaList')

cy.get('@commaList').each((qwer, index) => {..........})

Upvotes: 0

Related Questions