-
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
Misleading error message when trying to access (probably) detached DOM-Element #9176
Comments
Can you provide a reproducible example? We won't be able to open a PR to fix this without first writing a failing test that the PR fixes. Here are some tips for providing a Short, Self Contained, Correct, Example and our own Troubleshooting Cypress guide. |
This is the simplest example I could come up with: Test: describe('This test', () => {
it('will fail with a confusing error message', () => {
cy.visit('/page-1.html');
const willFail = cy.get('#failing').click().contains('not attached');
})
}) page-1.html: <html>
<head>
</head>
<h1>Page 1</h1>
<div id="failing">not attached</div>
<script>
const el = document.getElementById('failing');
el.addEventListener('click', function(e) {
el.remove();
});
</script>
</html> The error message the test will output is |
This is still a problem. @pakaufmann have you detected what was causing it? In my case it is not clear, 'cause DOM element is clearly on the page. |
I can recreate the error from the provided code: <html>
<h1>Page 1</h1>
<div id="failing">not attached</div>
<script>
const el = document.getElementById('failing');
el.addEventListener('click', function (e) {
el.remove();
});
</script>
</html>
it('will fail with a confusing error message', () => {
cy.visit('index.html')
cy.get('#failing').click().contains('not attached')
}) This is failing from this line in Cypress code: https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/ensures.js#L208:L208 |
@jayarjo The "real" case was a bit more complicated, where we replaced one part of the page dynamically with a response from the server. So maybe you have the same problem, where you initialize the page, do something asynchronously and replace some DOM-elements with new ones? Then, if you hold onto a reference of the old (now detached) element you get the error, while in the browser it looks like everything is still there. |
@pakaufmann I've same problem and mine is just as you've described. Any workaround for this? All I'm doing is checking if url contains a string after a click().
|
Small update: said issue is coming when I'm asserting on URL. Instead, if I assert for some other text on the new page, it is working as expected. Ex:
|
Have the same problem |
any workaround so far? |
I have the same issue. May be will try to find an element of the page after redirection and then assert the url. |
I'm also facing same issue . But this doesn't happen every time. Sometime it works fine. cy.get('#id').contains('##text##') |
I faced same but with promise it works fine |
The element doesn't necessarily need to be detached, it also happens with custom assertions:
If none of these match then it fails with:
|
100% agree that our error messaging isn't super helpful when the subject doesn't match what we were expecting. I've rewritten most of these for Cypress 12, and this sort of thing should occur way less often. #24628 has those changes, and see #7306 for discussion around how we've restructured retrying commands. |
.shadow saved my life and company...thx a lot for your passion and work!! |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
Some of our tests sometimes showed the following error message:
cy.contains() failed because it requires the subject be a global window object
.This wasn't very helpful at all, because the check looked something like this:
cy.get("foo").contains("bar")
The
cy.get("foo")
obviously is not a window-object.After digging around in the source code, I think the following line is the problem:
cypress/packages/driver/src/cy/ensures.js
Line 84 in 052892d
When all tests fail on the type, only the first error is shown, which in the "contain"-Case is the check if it is a window-object. It probably should show all errors, as then a user would also see the "cy.get("foo") is detached"-error. This would help in trying to debug applications which seem to fail at random.
The text was updated successfully, but these errors were encountered: