Skip to content

Improve invoking native DOM methods #806

Closed
@loilo

Description

@loilo

Current behavior:

Cypress makes invoking methods on native DOM elements less expressive than it could be.

My use case: I want to call an <input>'s checkValidity method. According to the FAQ I basically need to do this:

cy.get('input')
  .should($input => {
    expect($input.get(0).checkValidity()).not.to.equal(false)
  })

This basically works, but Cypress' otherwise amazing expressivity takes a hard hit here since the logs naturally don't know anything about the invocation:

image

→ The assertion is shown, however the desired behaviour would be to also have the invocation listed in the log, as it does when running .invoke().

Desired behavior:

I'd like to advocate for a built-in way to invoke native DOM methods. Some quick thinking lead me to two possible solutions:

The obvious one. Provide an .invokeNative() method on the Cypress wrapper. However having two methods for basically the same thing may not be desirable.

The embracing-jQuery one. Extend the built-in jQuery copy with a native method that proxies native DOM calls. I've tried this myself, but you apparently have shielded the Cypress.$.fn away from curious developers. 🙃

The implementation would basically be this:

Cypress.$.fn.native = (method, ...args) => {
  return this.get(0)[method](...args)
}

which would allow to invoke native DOM functionality as easy as:

cy.get('input')
  .invoke('native', 'checkValidity')
  .should('not.equal', false)

I'd love to hear your feedback on this.

  • Operating System: macOS 10.12.6
  • Cypress Version: Beta 1.0.2
  • Browser Version: Chrome 61

Metadata

Metadata

Assignees

No one assigned

    Labels

    pkg/driverThis is due to an issue in the packages/driver directorystage: proposal 💡No work has been done of this issuetype: enhancementRequested enhancement of existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions