0

I'm trying to do a simple while loop within a Cypress test that exits when a variable is updated.

I know that the variable 'done' is being updated to tru at the time within the loop - but by the time it gets back up to the top of the loop, it's still set to false.

    let ii = 0;
    let done = true;
    cy.log('done outside the loop - its true: ' + done);
    done = false;
    cy.log('done outside the loop - its now false: ' + done);

    while((ii<4) && (!done)) {

        cy.log('done inside the loop:  ' + done);
        
        cy.wait(1000);
        ii++;
        cy.get('[data-cy="reportGenOtherDialog"]').then($dialog => {
            let reportGenOtherDialog = $dialog;
            cy.log(reportGenOtherDialog.text());

            if (reportGenOtherDialog.text() === 'Publish Successful.') {
                
                done = true;
                cy.log('this proves that it is getting to here and updating - but it will still be false the next go-around: ' + done);
            }
        })
        
    }

Any suggestions?

Thanks much.

Tim
  • 443
  • 2
  • 5
  • 13
  • The `then` callbacks won't get called while main event loop is busy going round and round the `while`. Use `await` instead. – Quentin Aug 07 '23 at 22:49
  • And no, you cannot use `await` in Cypress. – D.Nykl Aug 07 '23 at 23:15
  • I think you diagnosed my problem perfectly, as it is making an asynchronous call - thanks. Just trying to figure some of the actual implementation out. – Tim Aug 07 '23 at 23:20
  • You can't use await in cypress? This looks like what I need - I just haven't quite figured it out for what I'm trying to do.. https://medium.com/@NicholasBoll/cypress-io-using-async-and-await-4034e9bab207 – Tim Aug 07 '23 at 23:28

0 Answers0