lundi 4 février 2019

cypress custom find command

i have a custom command that gets me my elements with the data-cy attribute.

Cypress.Commands.add("getById", (id) => {
    cy.get(`[data-cy=${id}]`)
})

everythings working fine.

now it would be nice if i had the same with find.

would be looking like this:

Cypress.Commands.add("findById", { prevSubject: true }, (subject, id) => {
    cy.wrap(subject).find(`[data-cy=${id}]`)
})

problem there is that cypress brings an error everytime i do sth like

cy.root().then((root) => {
if(root.findById("...")) {
...}
}

error is "root.findById" is not a function.

can you help me how to write that custom command right?

greetz simon

Aucun commentaire:

Enregistrer un commentaire