While writing Cypress test I've encountered this issue that won't let me update the desired variable.
What I'm trying to achieve is to run a loop and update the questionId variable inside the loop for some API query.
The desired variable changes with each iteration, the problem is that it takes the initial value 0 but fails to update within the loop.
I've read multiple articles regarding Cypress async/sync procedures but nothing seems to help.
Here's the test snippet:
it('Should pass', function () {
cy.visit(`${Cypress.env('appUrl')}/url`)
let questionId: number = 0
for (let index = 0; index < 9; index++) {
cy.intercept({ method: 'GET', path: `${questionId}` }).as('questionData')
cy.log('next question id: ' + questionId)
cy.intercept({ method: 'POST', path: 'answers' }).as('answers')
cy.contains('button', 'Submit answer').click()
cy.wait('@answers')
.then((xhr: any) => {
expect(xhr.response.statusCode).to.eq(200)
questionId = xhr.response.body.data.next_question_id
cy.log('new question id: ' + questionId)
cy.contains('span', 'You are correct!').should('be.visible')
cy.contains('button', 'view solution').click()
cy.contains('button', 'continue').click()
})
}
})