I have the following code:
cy.get('input:checkbox').first().as('firstCheckbox').then(() => {
cy.get('@firstCheckbox').should('be.checked').click()
cy.get('.globalMessage-display > :nth-child(1) > .alert').should('be.visible')
cy.get('.globalMessage-display > :nth-child(1) > .alert').should('not.exist')
cy.get('@firstCheckbox').should('not.be.checked').click()
cy.get('.globalMessage-display > :nth-child(1) > .alert').should('be.visible')
cy.get('.globalMessage-display > :nth-child(1) > .alert').should('not.exist')
cy.get('@firstCheckbox').should('be.checked')
})
after cy.get('@firstCheckbox').should('be.checked').click()
the checkbox is moved to the last index position instead of the first.
According to my understanding of aliases this should not matter, because I reference it with ('@')
and it doesn't matter at which position it is. Unfortunately the next cy.get('@firstCheckbox')
accesses the new first element and not the one I originally referenced. I can't figure it out from the documentation either.
where is the error?
https://docs.cypress.io/guides/core-concepts/variables-and-aliases
https://docs.cypress.io/api/commands/as
Update for @agoff:
it seems to work better with your code but there is a new problem I notice with the new code. if you uncheck the checkbox then the class is reloaded with js causing it to briefly disappear from the dom. After that it can't find it anymore and the test aborts. I also tried with reload but it can't find the element anymore. How to deal with this?
the checked checkbox class has the name custom-control-input ng-untouched ng-pristine ng-valid
. after unchecking the class name changed to custom-control-input.ng-untouched.ng-valid.ng-dirty
for a second and then the element is removed and reloaded to the DOM with with the class name custom-control-input ng-untouched ng-pristine ng-valid
. the problem is here that the second cy.wrap($el)
tries to find the element custom-control-input.ng-untouched.ng-valid.ng-dirty
although the initial element with the class custom-control-input ng-untouched ng-pristine ng-valid
was searched and found. that's something else I don't understand.