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

Open
jennifer-shehane opened this Issue Aug 30, 2016 · 10 comments

Comments

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

This comment has been minimized.

Copy link
Member

brian-mann commented Sep 8, 2016

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

This comment has been minimized.

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

@udisun

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

brian-mann commented May 16, 2017

@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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

brian-mann commented Sep 12, 2017

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

Copy link

PKSpeleo commented Sep 13, 2018

Same problem(

@dwelle

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

jennifer-shehane commented Jan 3, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment