Cypress: Value of Variable is not saved when go out IF statement

135 Views Asked by At

I have a table as image below. I try to check in checkboxes having 'ST - ' text then Delete them.

UI OF TABLE

Here my code:

        cy.get('td').invoke('text').then(text => {
        //1st IF
        if (text !== 'No data available') {
            let hasAuto = false;
            let tableReport = cy.get('#tableA')
            let rpBN = [];

            tableReport.find('tbody tr').each(($row) => {
                const thirdColumnText = $row.find('td:nth-child(3)').text().trim();
                rpBN.push(thirdColumnText)

                //2nd IF
                if (rpBN.some(item => thirdColumnText.includes('ST - '))) {
                    hasAuto = true;
                    const checkbox = $row.find('input[type="checkbox"]');
                    checkbox.trigger('click');
                }
              
            })
            cy.log('hasAuto = ' + hasAuto)

            //3rd IF
            if (hasAuto) {  
                //Click Delete
                cy.get('#deleteBtn').click()

                //Confirmation popup open -> click [Yes] button
                cy.get('#confimeP').contains('Yes').click()  
            }

        }
    })

But after out 2nd IF hasAuto become false and 3rd IF not run.

3

There are 3 best solutions below

0
Wandrille On BEST ANSWER

Because we are dealing with sync and async cypress methods, I would suggest instead to wrap your last if into a then:

cy.get('td').invoke('text').then(text => {
    //1st IF
    if (text !== 'No data available') {
        let hasAuto = false;
        const tableReport = cy.get('#tableA')
        const rpBN = [];

        tableReport.find('tbody tr').each(($row) => {
            const thirdColumnText = $row.find('td:nth-child(3)').text().trim();
            rpBN.push(thirdColumnText)

            //2nd IF
            if (rpBN.some(item => thirdColumnText.includes('ST - '))) {
                hasAuto = true;
                const checkbox = $row.find('input[type="checkbox"]');
                checkbox.trigger('click');
            }

        }).then(()=>{
           cy.log('hasAuto = ' + hasAuto)
           //3rd IF
           if (hasAuto) {
               //Click Delete
               cy.get('#deleteBtn').click()

               //Confirmation popup open -> click [Yes] button
               cy.get('#confimeP').contains('Yes').click()
           }
        })
    }
})
1
zzcmaple On

i think 'includes' key need same words as you specified keyword, so you can try use 'indexOf'

1
Ine Wilmann On

A good test would not need to use any if().

You make the table in the state shown, then the test will be something like


function isStudentRow($row) {
  return $row.find('td:nth-child(3)').text().trim().startsWith('ST - ')
}

cy.get('#tableA tbody tr').should('have.length', 3)

cy.get('#tableA tbody tr')
  .filter((index, row) => isStudentRow(Cypress.$(row)))
  .each($studentRow => {
    $studentRow.find('input[type="checkbox"]').trigger('click')
  })

cy.get('#deleteBtn').click()
cy.get('#confimeP').contains('Yes').click()

cy.get('#tableA tbody tr').should('have.length', 1)