I need to learn how to go from June to February with cypress, clicking month by month. My idea is to obtain the value of the month and if it is not the month February, continue clicking the next button
I was thinking of applying a conditional but it didn't work for me. If someone can fix the code or help me with any ideas, that would be fantastic.
it('Month iterate- conditional assertion', () => {
let searchMonth = 'february'
let monthLabel = '.Grid_header__yAoy_ > :nth-child(2)'
cy.visit('/')
cy.get('.Grid_header__yAoy_ > :nth-child(2)')
.then(($el) => {
monthLabel = $el.length
})
.each(($el) => {
cy.log($el.text())
if ($el.text() === searchMonth) {
cy.log($el.text() === searchMonth)
} else {
cy.get('.Grid_header__yAoy_ > :nth-child(3)').click()
}
})
})
I can advance 1 month
Don't use conditional expressions in your test, it will make the test much more complicated and likely create bugs.
Instead, make the test data definitive.
Remember that an action like
click()will probably make the page refresh, so you can end up with a flaky test because queries made earlier are no longer valid.You probably don't like all that repetition, but starting from a working test you can refactor