-
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
Cypress aliases are not refreshed before assertion #5153
Comments
Well, I wouldn't call it a fix, rather workaround which may not be valid for all scenarios as, e.g. in my tests I prefer pointing aliases at the elements which number is changing dynamically (and can have various parents). Also, if the alias is a reference pointing at some DOM elements then I believe it should reflect the current DOM state. What's more, there's some inconsistency in the behaviour - if I had removed the row instead of adding it, the alias would be working fine, is that right? If this cannot be easily fixed maybe it's worth to provide users an option to refresh the alias as an option for the |
It seems to be quite similar to that one #2971. Do you know how I can get a selector from alias? I was thinking about overwriting get() command and add to it an option to fetch the DOM using selector referenced by alias. |
So I have the following piece of code which fails because of the issue mentioned in the ticket (previously @errormessages alias points to 2 elements, however, at this point there's only one such element) (...)
// then
.get('@errorMessages')
.should('have.length', 1)
.and('have.text', expectedErrorMessage) According to your advice, I tried the workaround in the test which worked fine: // then
.get('@errorMessages').then(element => {
cy.get(element.prevObject.selector)
.should('have.length', 1)
.and('have.text', expectedErrorMessage)
}) So I tried to make it generic: Cypress.Commands.overwrite('get', (originalFn, selector, options) => {
if (selector.startsWith('@') && options && options.refereshAlias) {
cy.get(selector).then(element => originalFn(element.prevObject.selector, options));
}
return originalFn(selector, options);
}); However, now when I run the test, even without providing
which doesn't make sense, as error comes up from EDIT: Also, I've noticed that |
We currently don't requery the DOM for aliased elements, as noted above from https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Elements I would consider this a duplicate of the proposal in #2971 Can we close this issue in favor of that please? |
Sure thing, closing it now, thanks! |
Current behavior:
Cypress aliases refer to old DOM state when they're fetched/asserted in the test which leads to the assertions like below passing:
Apparently first assertion is passing because of the cached DOM, and second one passes because alias gets refreshed as first try fails silently
Desired behavior:
Aliases should refer to the actual DOM state when they're used in the test.
Steps to reproduce:
npm i
npm run start
npx cypress run
Versions
Cypress 3.4.1
Chromium 78
macOS 10.14.6
The text was updated successfully, but these errors were encountered: