describe("Share Link", () => { beforeEach(() => { cy.generateLink().then(response => { let url = response.meeting_shared_link.split("/"); cy.wrap(url[url.length - 1]).as("link"); }); describe("when I turn on link sharing", () => { // This works. Changing it to a before hook breaks it. beforeEach(() => { cy.get("@link").then(link => cy.toggleLinkSharing({ link: link }) ); }); }); Actualmente estoy generando un alias - @link - en mi primer beforeEach , y luego accedo a él en el siguiente enlace beforeEach que está anidado en una descripción.
Mi problema es que necesito que el último enlace sea un before , en lugar de un beforeEach .
Cuando lo modifico a un enlace anterior, no puede encontrar el alias "enlace". ¿Por qué?
Entiendo que los alias se borran entre cada prueba, de ahí la necesidad del primer anzuelo beforeEach , pero ¿por qué no está disponible dentro de un anzuelo before ?
Editar: . Creo que puede deberse a que el último definido before de que el gancho se beforeEach antes que el primero antes de cada uno, en cuyo caso, el alias aún no existía. Si este es el caso, no es intuitivo. El anzuelo anterior solo debe activarse después de antes de Cada uno, ya que está anidado en otra descripción.
Puede cambiar el beforeEach() externo a before() y conservar el alias entre las pruebas con beforeEach(function() { cy.wrap(this.link).as('link') })
Funciona porque this.link no se borra entre pruebas, aunque se borre el alias @link .
describe("Share Link", () => { before(() => { cy.wrap('my-url').as("link"); console.log('Outer before') }); describe("when I turn on link sharing", () => { beforeEach(function() { cy.wrap(this.link).as('link') }) // preserve the alias before(() => { cy.get("@link").then(link => { console.log('Inner before', link) }) }) it('1st test', () => { cy.get('@link').then(link => console.log('1st test', link)) }) it('2nd test', () => { cy.get('@link').then(link => console.log('2nd test', link)) }) }) })Salida de consola
Outer beforeEach Inner before my-url 1st test my-url 2nd test my-urlPuedes usar this.link y probar.
describe("Share Link", () => { beforeEach(() => { cy.generateLink().then(response => { let url = response.meeting_shared_link.split("/"); cy.wrap(url[url.length - 1]).as("link"); }); describe("when I turn on link sharing", () => { // This works. Changing it to a before hook breaks it. beforeEach(() => { cy.toggleLinkSharing(this.link) }); });Referencia aquí: https://docs.cypress.io/guides/core-concepts/variables-and-aliases#Sharing-Context