0

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.

Ine Wilmann
  • 156
  • 6
HNhu
  • 99
  • 5

3 Answers3

3

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)
Ine Wilmann
  • 156
  • 6
  • My issue is: It will check all checkboxes inculde 'TC - ' ``` cy.log ($row.find('td:nth-child(3)').text().trim().startsWith('ST - ') ) // result: ST - 111 /n ST - 222 /n TC - 333 ``` – HNhu May 05 '23 at 08:40
1

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()
           }
        })
    }
})
Wandrille
  • 6,267
  • 3
  • 20
  • 43
0

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

zzcmaple
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '23 at 17:19