question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cypress is referencing to the wrong aliased element

See original GitHub issue

Current 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:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jennifer-shehanecommented, Jul 29, 2021

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 the cy.contains() in your case and verify whether this fixes the issue?

0reactions
wlsf82commented, Jul 29, 2021

By the way, I’ll read about cy.root(). I didn’t know about it. 🙇

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found