Imagina que tengo el siguiente html simple:
const inEl = document.querySelector("input") const buttonEl = document.querySelector("button") inEl.oninput = (() => { buttonEl.remove() setTimeout(() => { document.body.appendChild(buttonEl) }, 100) }) .b { background: blue; } <input> <button>weee</button>Como puede ver, cuando alguien escribe la entrada, el botón se elimina temporalmente del DOM. Me gustaría agregar una prueba de ciprés que verifique que el botón NO se elimine del dom (por lo que debería fallar en el escenario anterior).
Parece bastante simple, pero debido a que Cypress es tan bueno esperando que aparezcan las cosas, no estoy totalmente seguro de cómo escribir esta prueba.
Parece que lo que necesito es una forma de generar un error si pasa un comando de Cypress. Algo como
cy.get("input").type("hello") cy.get("button").should("not.exist") //if this passes then throw an error!Cualquier ayuda sobre cómo hacer esta cosa aparentemente simple sería apreciada. ¡Gracias!
https://docs.cypress.io/guides/core-concepts/retry-ability#Disable-retry
Puede establecer el tiempo de espera en 0 para deshabilitar el reintento
para que puedas agregar
cy.get("button", { timeout: 0 }).should("not.exist")para asegurarse de que el botón no parpadee.
Una posibilidad es espiar el método remove
let spy; cy.get("button").then($button => { spy = cy.spy($button[0], 'remove') }) cy.get("input").type("hello") .should(() => expect(spy).to.not.have.been.called)Si intenta realizar comprobaciones de visibilidad o existencia, corre el riesgo de obtener resultados falsos, porque ese script se ejecutará con bastante rapidez.
Si quieres hacerlo, puedes controlar el reloj.
// This passes if the remove/append runs cy.clock() cy.visit(...) cy.get("input").type("h") // one letter only cy.tick(20) // 10ms is default delay in .type() cy.get('button').should('not.exist') // clock is frozen part way through setTimeout cy.tick(100) cy.get('button').should('exist') // clock has moved past setTimeout completion // This checks the remove/append does not run cy.clock() cy.visit(...) cy.get("input").type("h") // one letter only cy.tick(20) cy.get('button').should('exist') // fails here if the button is removed cy.tick(100) cy.get('button').should('exist')