How to go page by page in cypress?

Viewed 251

I have ajax table in my web and this table has many pages. My goal is to go through all the rows and columns, go to the next page and make the following procedure until the end of the page. I have the following code:

  for(let i = 0; i < 10; i++){

      cy.get('tr:has(td)').each(($tr, rowIndex) => {    // look at each row individually

        cy.wrap($tr)
          .find('td')                           // all cols inside this row
          .invoke('slice', 5, 8)                // filter to last 3 cols only
          .invoke('text')                       // get all the text in one lump
          .then(text => {
            if (text.trim() === '') {           // after trim(), is all text empty?
              console.log(`Row ${rowIndex+1} has three empty cols`) 
              cy.wrap($tr).find('td').eq(5).click({force: true})  // click the 6th col
              cy.wait(4000)
                   cy.get('.review-modal_textarea_3L5AQ').eq(0).should('be.visible').type(str)

cy.get('.pagination_pagination_2iOOV > :nth-child(4)').click({force: true}) //clicking to the next page

}

I managed to go through all rows and columns in one page, but having problem going to the next page (its going just one time). I think I have problem with loop. It simply doesn't working. I have i<10 because I have 10 pages, but the pages can be more (may there is a way to do it without for loop). How can I solve this problem?

1 Answers

I think you can do it by wrapping the per-page code in a function and calling it at the right time.

The trouble with for(let i = 0; i < 10; i++){ it's not letting the page change complete.

function handlePage() {  // naf function name, but for illustration
  cy.get('tr').each(($tr, rowIndex) => {    // look at each row individually

    cy.wrap($tr)
      .find('td')                           // all cols inside this row
      .invoke('slice', 5, 8)                // filter to last 3 cols only
      .invoke('text')                       // get all the text in one lump
      .then(text => {
        if (text.trim() === '') {           // after trim(), is all text empty?
          console.log(`Row ${rowIndex+1} has three empty cols`) 
          cy.wrap($tr).find('td').eq(5).click()  // click the 6th col
          cy.wait(4000)
          cy.get('.review-modal_textarea_3L5AQ').eq(0).should('be.visible').type(str)
        }
      })
  })
}

cy.visit(...)  // whatever gets 1st page

handlePage()   // 1st page

Cypress._.times(9, () => {    // 9 more pages

  cy.get('.pagination_pagination_2iOOV > :nth-child(4)').click({force: true}) 
  .then(() => {           // after page transition
    // may need a cy.wait() or test page number on screen
    handlePage()          
  })
})
Related