Skip to content
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

Implicit should 'exist' assertion is not being applied on cy.get() when other assertion. #205

Closed
jennifer-shehane opened this issue Aug 30, 2016 · 18 comments · Fixed by #9239
Assignees
Labels
type: breaking change Requires a new major release version type: bug type: unexpected behavior User expected result, but got another

Comments

@jennifer-shehane
Copy link
Member

jennifer-shehane commented Aug 30, 2016

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
screen shot 2016-08-30 at 3 04 42 pm

@brian-mann
Copy link
Member

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.

@brian-mann brian-mann modified the milestones: 0.17.x, 0.17.3 Sep 8, 2016
@fvanwijk
Copy link
Contributor

fvanwijk commented Jan 31, 2017

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: ’someAlias', but never found it., while a regular should('not.exist') passes.

@jennifer-shehane jennifer-shehane removed this from the 0.17.x milestone Feb 3, 2017
@jennifer-shehane jennifer-shehane added the pkg/driver This is due to an issue in the packages/driver directory label Mar 21, 2017
@udisun
Copy link

udisun commented May 16, 2017

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

@brian-mann
Copy link
Member

@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.

@udisun
Copy link

udisun commented May 17, 2017

@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

@udisun
Copy link

udisun commented May 24, 2017

@brian-mann did you check my example project, do you have an explanation why the command isn't working?

thanks

@brian-mann
Copy link
Member

@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

@PKSpeleo

This comment has been minimized.

@dwelle
Copy link

dwelle commented Dec 15, 2018

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?

@jennifer-shehane
Copy link
Member Author

Hey @dwelle, yes you can open an issue and/or PR from our documentation repo. https://github.com/cypress-io/cypress-documentation

@kuceb
Copy link
Contributor

kuceb commented Jan 25, 2019

Hi, this is due to a bug fixed in this PR #3268

Edit: updated with correct PR

@jennifer-shehane
Copy link
Member Author

Hey @bkucera, I think you may have linked the wrong PR above in your comment. Did you mean this? #3268

@jennifer-shehane
Copy link
Member Author

jennifer-shehane commented Feb 3, 2020

An example on why this current implementation is a problem.

Example 1

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)

@jennifer-shehane
Copy link
Member Author

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 jennifer-shehane added the type: breaking change Requires a new major release version label Apr 8, 2020
@cypress-bot cypress-bot bot added stage: proposal 💡 No work has been done of this issue and removed stage: needs review The PR code is done & tested, needs review labels Jul 13, 2020
@shwarcu
Copy link

shwarcu commented Sep 9, 2020

@jennifer-shehane hi, is there any progress on that issue?

@jennifer-shehane
Copy link
Member Author

We're looking into the details of this issue again, to see if we can get this into one of our future breaking releases.

@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review stage: work in progress and removed stage: proposal 💡 No work has been done of this issue stage: needs review The PR code is done & tested, needs review labels Nov 18, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Nov 20, 2020

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.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Nov 24, 2020

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.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Nov 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: breaking change Requires a new major release version type: bug type: unexpected behavior User expected result, but got another
Projects
None yet
9 participants