aldrine00
aldrine00

Reputation: 97

Change value of variable and check whether the value have changed

I want to check whether the text element changes on x seconds and apply the for loop and conditional structure to verify if the change is applied. If the text is still not changed it will refresh the page and check again

Cypress.Commands.add('checkApprovedStatus', (targetData) =>{
    cy.get('div[class^=ui-table-scrollable-body]').contains('tr', targetData).first().parent().within(function(){
        cy.get('td').eq(10).children('span[class^=customer-badge]').then(($status) =>
        {
            //let currStatus = $status.text()
            //cy.log(currStatus)
            
           for(let count = 0; count <= 5; count++)
            {
                let currStatus = $status.text()
                cy.log(currStatus)
                
                if (currStatus === 'approved')
                {
                    //if (currStatus.contains('Approved', {matchCase:false}))
                    //{
                    cy.log("End to end testing completed. All checks have passed!!")
                    break
                    //}
                 }
                else
                {
                    cy.reload()
                    cy.wait(5000)
                    $status.trigger('change')
                } 
            }
        })
    })
})

Upvotes: 2

Views: 702

Answers (1)

Ackroydd
Ackroydd

Reputation: 1610

For loops generally crash and burn in Cypress, but you can use a recursive function to simulate the loop.

When the loop (reload/trigger change) fails to find the status, throw an error to fail the test, or just return.

const checkApprovedStatus = (targetData, attempt = 0) => {

  if (attempt === 5) {
    // used up all attempts, can either fail the test
    throw 'Was never approved'

    // or just return without failing
    return
  }

  cy.get('div[class^=ui-table-scrollable-body]')
    .contains('tr', targetData).first().parent()
    .within(() => {
      cy.get('td').eq(10).children('span[class^=customer-badge]')
        .then($status => {
          if ($status.text() === 'approved') {
            cy.log("All checks have passed!!")
          } else {
            cy.reload()
            cy.wait(5000)
            $status.trigger('change')
            checkApprovedStatus(targetData, ++attempt) 
          } 
        })
    })
})

Upvotes: 1

Related Questions