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

cy.wrap($el) times out with error: obj.is is not a function #3549

Closed
andezzat opened this Issue Feb 21, 2019 · 6 comments

Comments

2 participants
@andezzat
Copy link
Contributor

andezzat commented Feb 21, 2019

Current behavior:

If you attempt to grab the children of an element & loop through them and start making should assertions after wrapping them, it seems that 'contain' assertions do not work unless you issue a different assertion or command on the wrapped element beforehand.

Command:  wrap
Error:    CypressError: Timed out retrying: obj.is is not a function

Desired behavior:

You should be able to just do a cy.wrap(childEl).should('contain', 'some text') with no prior commands or assertions.

Steps to reproduce:

Spec

describe('should fail', () => {
  it('fails when asserting a wrapped child el', () => cy
    .visit('index.html')
    .get('.my-el')
    .children()
    .then(children => cy.wrap(children[0]).should('contain', 'This should fail...')));

  it('passes when asserting the el itself', () => cy
    .visit('index.html')
    .get('.my-el h1')
    .then($el => cy.wrap($el).should('contain', 'This should fail...')));

  it('passes when asserting the wrapped child el AFTER scrollIntoView', () => cy
    .visit('index.html')
    .get('.my-el')
    .children()
    .then(children => cy.wrap(children[0]).scrollIntoView().should('contain', 'This should fail...')));
});

Page

<html lang="en">
  <head>
    <title>Hello World</title>
  </head>
  <body>
    <div class="my-el">
      <h1>This should fail...</h1>
      <p>words</p>
    </div>
  </body>
</html>

Versions

Electron 59
Cypress 3.1.5
macOS 10.14.2

@jennifer-shehane

This comment has been minimized.

Copy link
Member

jennifer-shehane commented Feb 21, 2019

Hey, this issue does not occur with using only cy.wrap().should()

Works

it('wrap should', function () {
  cy.visit('https://example.cypress.io')
  cy.get('h1').then(($el) => {
    cy.wrap($el).should('contain', 'Kitchen Sink')
  })
})

Could you reduce your test code to the smallest amount possible to recreate the error?

@andezzat

This comment has been minimized.

Copy link
Contributor Author

andezzat commented Feb 21, 2019

@jennifer-shehane Fair call.
I've reduced the test case to the barebone now.

@jennifer-shehane

This comment has been minimized.

Copy link
Member

jennifer-shehane commented Feb 21, 2019

Hey @andezzat, thanks for updating your example.

I have created a simpler example to demonstrate this behavior below.

it('should not error when asserting on non jQuery el', () => {
  cy.visit('https://example.cypress.io')
  cy.get('.navbar').then(($nav) => {
    cy.wrap($nav[0]).should('contain', 'cypress')
  })
})

screen shot 2019-02-21 at 7 23 13 pm

Cypress is assuming that the element yielded from the wrap here is a jQuery element, but you have yielded a simple JavaScript DOM element. This should work fine, but we try to call a function that does not exist.

I've opened a PR here to fix this: #3553

Workaround

For now, update your tests so that the element yielded from cy.wrap() is a jQuery element, so wrap the normal element with Cypress.$():

describe('should fail', () => {
  it('fails when asserting a wrapped child el', () => cy
    .visit('index.html')
    .get('.my-el')
    .children()
    .then(children => cy.wrap(Cypress.$(children[0])).should('contain', 'This should fail...')));

  it('passes when asserting the el itself', () => cy
    .visit('index.html')
    .get('.my-el h1')
    .then($el => cy.wrap($el).should('contain', 'This should fail...')));

  it('passes when asserting the wrapped child el AFTER scrollIntoView', () => cy
    .visit('index.html')
    .get('.my-el')
    .children()
    .then(children => cy.wrap(Cypress.$(children[0])).scrollIntoView().should('contain', 'This should fail...')));
});
@andezzat

This comment has been minimized.

Copy link
Contributor Author

andezzat commented Feb 21, 2019

@jennifer-shehane Ahh, this makes sense to me now!
I thought something might be different between els yielded from cy.get and those from cy.children.
Thanks for showing me a workaround too, it should suffice for the time being!

@cypress-bot

This comment has been minimized.

Copy link

cypress-bot bot commented Feb 22, 2019

The code for this is done in cypress-io/cypress#3553, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

chrisbreiding added a commit that referenced this issue Feb 22, 2019

convert dom element to jQuery element in order to properly check cont…
…ains assertion (#3553)

- close #3549 
- obj.is and obj.find is not a function when used on regular el - needs
to be jQuery el

**Tests**
- [x] Write test

I can't figure out where the tests for these chai overrides are. Any help?
@cypress-bot

This comment has been minimized.

Copy link

cypress-bot bot commented Mar 15, 2019

Released in 3.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.