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.

After using cy.wait() elements are still detached from the DOM.

See original GitHub issue

Current behavior

After using cy.wait() to wait for ajax calls to complete, then continuing on with assertions, elements are still detached from the DOM.

Desired behavior

The element should not be detached from the DOM.

Test code to reproduce

This is some sample code I’m able to provide. The test will usually fail at any .click(), .type() or at the .select() call

// selecting designer
cy.get("[data-cy=designer]")
  .should("be.empty")
  .type(cyDesigner)
  .wait("@XHR3");

// a dropdown appears
cy.get("[data-cy=targetElement]")
  .should("contain.text", cyType)
  .click()
  .wait(["@XHR4", "@XHR2"]);

// selecting type
cy.get("[data-cy=type]")
  .should("be.empty")
  .type(cyType)
  .wait("@XHR3");

// a dropdown appears
cy.get("[data-cy=targetElement]")
  .should("contain.text", cyType)
  .click()
  .wait(["@XHR4", "@XHR2"]);

// selecting rate
cy.get("[data-cy=rate]")
  .should("contain.text", cyRate)
  .select(cyRate)
  .wait(["@XHR1", "@XHR2"]);

Cypress Version

8.0.0

Other

Summary

So I’m testing an application for a company I work for. The main issue I’m having with Cypress is that periodically (seemingly at random) after using cy.wait() elements will still be detached from the DOM. One weird thing that I’ve noticed is that when comparing the screenshots Cypress takes of when I’m clicking, getting or typing and XHR calls: Cypress is going off of the “request” version of the page (before the Ajax call completes) but the ajax call has been properly waited for by Cypress (as shown by the .wait() command continuing).

Possible Reasons

  • Cypress isn’t waiting for the page to fully update with the XHR response data. (See new “Solutions” header)
  • A timing error within my app (for whatever reason).
  • Not an issue with there being too many XHR calls and Cypress simply isn’t handling them all.

Things I’ve tried:

  • Adding duct tape
    • Adding a full one second delay after each cy.wait() statement (this seems to work but there’s about a 1% chance of failing)
    • Increasing the defaultCommandTimeout in the cypress.json file from 4s to 10s (usually works)
  • Looked at and implemented the proper ways of using cy.wait() here when dealing with this error.
  • I’ve looked at a bunch of other issues, especially the main one
  • Tried to use Cypress.$, plugins like cy.waitUntil and community suggestions like cy.getAttached() or cy.getSettled()

Solution

  • Switching to cy.route and cy.server from cy.intercept solved all of the issues I’ve been getting. Case in point, its probably something within cy.intercept that is causing these errors.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
RobertMartinGMSLcommented, Aug 16, 2021

I’m also seeing this on .click() (as mentioned above) as well as .tick(). Note that this does not occur when reverting to cy.server() and cy.route(), but is does occur using cy.intercept (currently using cy v8+)

Seems that the cy.wait is no longer waiting for the event it is waiting on to complete 🤷 I worked around this using the suggestion above

Adding a full one second delay after each cy.wait() statement

But as mentioned, there is a low chance of this not working on our CI (where the tests run slower)

this seems to work but there’s about a 1% chance of failing)

NB: for anybody experiencing this problem only in the CI, I managed to recreate locally by throttling Chrome using dev tools (F12) to “Fast 3G”.

1reaction
jennifer-shehanecommented, Apr 13, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Solving The Element Is Detached From DOM Error In Cypress
If the application is still performing an action, tell the test to "wait" for the action to complete. Otherwise the application might suddenly ......
Read more >
Why is Cypress saying my element is detached after just ...
Getting this error means you've tried to interact with a "dead" DOM element - meaning it's been detached or completely removed from the...
Read more >
Do Not Get Too Detached - Cypress
Cypress throws an error because the DOM element with the text "Clementine Bauch" it was about to click is no longer linked to...
Read more >
How To Avoid Element Detached From DOM Error - YouTube
In this video, I go through a Cypress test that gets the dreaded " element is detached from the DOM " error. This...
Read more >
Fight The Element Detached From DOM Error Using jQuery ...
To solve this, we can use jQuery "click () " method instead of cy.click () command. The jQuery method does not have the...
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