Using cy.get() on an element should implicitly assert existence of the element. But in my examples below, you can see that I have no element foobarbaz, the Command Log correctly logs 0 elements found, yet my explicitly written assertions (not.be.visible and not.have.class) chained off of the gotten element pass.
cy.get() should always implicitly assert existence unless there is an explicit assertion down the chain on that element asserting that it should not.exist.
Example Test Code
it("dom element doesn't have attributes", function() {
cy.get("foobarbaz").should("not.be.visible");
cy.get("foobarbaz").should("not.have.class", "foo");
});
Example Command Log

This is because internally Cypress applies a default assertion should("exist") only if there are _no other assertions_.
Since you added additional assertions the implicit should("exist") was not applied. You're using negated assertions and those pass because cypress did not find the element, and a non existent element is both not visible and does not have the class.
Cypress needs to be updated to always apply the default should("exist") assertion even if there are other assertions so long as there is not a should("not.exist"). If there is it should reverse its behavior and wait for the element not to exist before applying assertions.
I like to have cy.get('@someAlias').should('not.exist') to have the same behavior, because according to the docs an alias requeries the element.
Currently for a non-existing selection my example returns Timed out retrying: Expected to find element: 鈥檚omeAlias', but never found it., while a regular should('not.exist') passes.
I'm getting some weird behavior with this, when trying this assertion:
cy.visit('/')
.get('#signup').should('not.exist')
the assertion fails and prints:
expected #signup to exist in the DOM
it look like a bug as the .get function still tries to assert exist
version: 0.19.2
@udisun It seems unlikely that is the entirety of your code. Can you put together a reproducible repo?
There is likely another problem in play because code that simple definitely works.
@brian-mann i've created a very small repo with the example of not.exist failing , the weird thing is that it only fails on the ui (cypress open and run).
https://github.com/udisun/cypress-first-steps
@brian-mann did you check my example project, do you have an explanation why the command isn't working?
thanks
@udisun I looked at your repo and you did not provide a proper query selector to the cy.get which is why it was failing.
https://github.com/udisun/cypress-first-steps/blob/master/cypress/integration/app_spec.js#L13
There is no element called signup-guide
Same problem(
I think this is large enough issue to warrant doc changes, particularly in https://docs.cypress.io/guides/core-concepts/introduction-to-cypress.html#Default-Assertions and https://docs.cypress.io/api/commands/should.html#Syntax. Should I open a PR?
Hey @dwelle, yes you can open an issue and/or PR from our documentation repo. https://github.com/cypress-io/cypress-documentation
Hi, this is due to a bug fixed in this PR #3268
Edit: updated with correct PR
Hey @Bkucera, I think you may have linked the wrong PR above in your comment. Did you mean this? https://github.com/cypress-io/cypress/pull/3268
An example on why this current implementation is a problem.
If you have an element that did exist and then sets the elements visibility to hidden. Both of these assertions pass, so you essentially cannot test this behavior with the current implementation of Cypress.
cy.get(el).should(not.be.visible)
cy.get(el).should(be.visible)
Another use case from #6958 - notice I'm not even visiting a website and this passes because the checkbox doesn't event exist.
it('testing', () => {
cy.get('input').should('not.be.checked')
})
@jennifer-shehane hi, is there any progress on that issue?
We're looking into the details of this issue again, to see if we can get this into one of our future breaking releases.
The code for this is done in cypress-io/cypress#9239, but has yet to be released.
We'll update this issue and reference the changelog when it's released.
Released in 6.0.0.
This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v6.0.0, please open a new issue.
Most helpful comment
I like to have
cy.get('@someAlias').should('not.exist')to have the same behavior, because according to the docs an alias requeries the element.Currently for a non-existing selection my example returns
Timed out retrying: Expected to find element: 鈥檚omeAlias', but never found it., while a regularshould('not.exist')passes.