Cypress is referencing to the wrong aliased element
See original GitHub issueCurrent behavior
I have a test that has three aliases for three different elements. They are:
cy.contains('button', 'Start')
.as('startBtn')
cy.get('input[type="text"]')
.as('textField')
cy.contains('button', 'Reset')
.as('resetBtn')
As you can see, every alias refers to a different element.
The last assertion of my test is:
cy.get('@resetBtn')
.should('not.exist')
And this exact step fails with the following error:
AssertionError: Timed out retrying after 4000ms: Expected to find element: `input[type="text"]`, but never found it.
Desired behavior
Cypress should refer to the correct element through its alias, and the tests should pass.
Test code to reproduce
Here’s a draft PR that changes a sample app and updates its test, which fails with a failure that seems to be a Cypress issue: https://github.com/wlsf82/live-tat-code-cov/pull/3. And here’s the failing test result from GitHub Actions https://github.com/wlsf82/live-tat-code-cov/pull/3/checks?check_run_id=3148179864.
Cypress Version
I tried on versions 8.0.0 and 7.7.0, and the issue happens on both of them.
Other
I’m using a macOS Big Sur Version 11.5
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Variables and Aliases - Cypress Documentation
Aliases have other special characteristics when being used with DOM elements. After you alias DOM elements, you can then later access them for...
Read more >Error message when using aliases with cypressIO
1) Sometimes this.alias doesn't work, try using: cy. · 2) If the text is contained in an element below #title , you will...
Read more >Use Cypress Element Alias To Avoid The Element Detached ...
In this example, I show how to store the result of the long chain of Cypress commands in an alias. cy.get('#chain-example') .find('#items') ...
Read more >Cypress aliases are not refreshed before assertion #5153
Current behavior: Cypress aliases refer to old DOM state when they're fetched/asserted in the test which leads to the assertions like below ...
Read more >Cypress Tips and Tricks - Gleb Bahmutov
If a client-side error happens while the E2E Cypress test is running ... item element without caching a reference to the DOM element...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I believe this may be a duplicate of https://github.com/cypress-io/cypress/issues/7413 where there’s a situation where the
cy.contains
is not correctly viewed as a parent command.Can you try writing
cy.root().contains()
to replace thecy.contains()
in your case and verify whether this fixes the issue?By the way, I’ll read about
cy.root()
. I didn’t know about it. 🙇