-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Requery aliased DOM elements #2971
Comments
Interesting proposal! I know this is a couple years old at this point, but it's well written and I wanted to give a through response before I close it. As part of the resolution of #7306, I've basically rewritten how aliases (and commands in general) work under the hood, and one consequence of that is that you can get the above behavior pretty easily. Here are some examples of how it works in Cypress 12.
Basically - "queries" are always rerun, building up a "subject chain" of functions that can be rerun at any time (eg, whenever an alias is read, or whenever determining the subject of a future command). But non-query commands (like .then(), .wrap(), .click()) break the subject chain, making the subject into a specific set of elements. So if you want to maintain an alias reference to "the list as it existed at the beginning of my test", you can query for the |
Current behavior:
An alias onto a DOM selection is a snapshot. So
cy.get('@listElements').should('have.length', 2)
will yield the same result at any point in the test even if list elements are added or removed.This makes aliases difficult to use in some cases.
Desired behavior:
An alias can be overwritten easily though. So you could write a command that overwrites a specific alias and use that instead of
get
(seeupdatelistElements
command). But this breaks the readability and flow of the test a bit.The
get
command does have all the information it would need to update the alias. The yield contains theprevObject
property that can be traversed so that together with theselector
property the alias can be updated.This is illustrated in the
updateAlias
command that wraps acy.get
but updates the alias at the same time.People could simply use the
updateAlias
command but it might also be nice to have this functionality in a different way.Maybe a live alias could be created like so:
cy.get('li').as('listElements', {live: true})
that would always update the alias on aget
command.Or maybe the get could be use like this:
cy.get('@listElements', {update: true})
.A test script to illustrating the above
Versions
Cypress 3.1.1
The text was updated successfully, but these errors were encountered: