Function dataCy

  • Select an html element by its data-cy attribute.

    cy.dataCy('something').click() // -> cy.get("[data-cy='something']").click()
    

    Accepts children which will then become further selectors

    cy.dataCy('something', '.nested').click()
    // -> cy.get("[data-cy='something'] .nested").click()

    And is chainable such that it will use the previous element as its scope

    cy.dataCy('something').dataCy("other").click()
    // -> cy.get("[data-cy='something']").within(() => cy.get("[data-cy='other']").click())
    cy.get('.top').dataCy('something') // same idea as above

    Finally, it supports taking the first signature literally and passing in a subject directly. That's very likely an unnecessary step and convention/obvious errors would prevent it, but going for complete here

    cy.get(".first-selector").then(($first) =>
    cy.dataCy($first, "second-selector").should("contain", 0)
    );

    Note: this is not added to cypress commands by default. Pass to addCommands or a custom function this should be added as a cypress command.

    Parameters

    • subject: undefined | HTMLElement | JQueryWithSelector<HTMLElement>
    • selector: string
    • Optional children: null | string
    • Optional options: Partial<Loggable & Timeoutable & Withinable & Shadow>

    Returns Chainable<JQueryWithSelector<HTMLElement>>

  • Select an html element by its data-cy attribute.

    cy.dataCy('something').click() // -> cy.get("[data-cy='something']").click()
    

    Accepts children which will then become further selectors

    cy.dataCy('something', '.nested').click()
    // -> cy.get("[data-cy='something'] .nested").click()

    And is chainable such that it will use the previous element as its scope

    cy.dataCy('something').dataCy("other").click()
    // -> cy.get("[data-cy='something']").within(() => cy.get("[data-cy='other']").click())
    cy.get('.top').dataCy('something') // same idea as above

    Finally, it supports taking the first signature literally and passing in a subject directly. That's very likely an unnecessary step and convention/obvious errors would prevent it, but going for complete here

    cy.get(".first-selector").then(($first) =>
    cy.dataCy($first, "second-selector").should("contain", 0)
    );

    Note: this is not added to cypress commands by default. Pass to addCommands or a custom function this should be added as a cypress command.

    Parameters

    • selector: string
    • Optional children: null | string
    • Optional options: Partial<Loggable & Timeoutable & Withinable & Shadow>
    • Optional never: undefined

    Returns Chainable<JQueryWithSelector<HTMLElement>>