Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

354
Views
Cypress: extraiga el cuerpo del correo electrónico y obtenga un enlace de él

Usando Cypress para la automatización de pruebas, estoy tratando de encontrar una solución sobre cómo raspar el cuerpo de un correo electrónico, sacar el enlace, almacenarlo en una variable y luego visitar el enlace a través de cy.visit().

 /// <reference types="Cypress" /> const sender = "sender@companyemail.com"; const companyEmail = "company@company.com" const emailSubject = "[Action required] Activate your 14-day Dataddo trial"; describe("Sign-Up Email assertion and visit confirmation link", () => { it("Sign-Up and Look for an email with specific subject and link in email body", function () { cy.task("gmail:get-messages", { options: { from: sender, to: companyEmail, subject: emailSubject, include_body: true } }).then(emails => { assert.isNotNull( emails, "Expected to find at least one email, but none were found!" ); cy.log("Email has been found successfully") assert.isAtLeast( emails.length, 1, "Expected to find at least one email, but none were found!" ); cy.log(`Email length is: ${emails.length}`) cy.log(`Email Recipient is: ${companyEmail}`) const body = emails[0].body.html; // returns email body (see the mock-up below) /* TODO - Parse email body, get the confirmation link out of it - Store the link it a variable - Visit the link via cy.visit - Note that tokenid is going to be different with every run */ }) }) })

La maqueta del cuerpo del correo electrónico esperado está disponible a continuación.

 <html> <head> </head> <body> <p>This is SignUp Email with confirmation link</p> <p> <a href = "http://www.company.com/ls/click?upn=tokenid-11111-22222-333333-444444-555555-xxxxxx"><a> </p> </body> </html>

Gracias por tu ayuda.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede usar un archivo json para almacenar el enlace y luego simplemente visitarlo en la próxima prueba recuperándolo del json.

 cy.readFile(json).then((obj) => { cy.get('a').invoke('attr', 'href').then((href) => { obj.activationLink = href cy.writeFile(json, obj) }) }) it('visits the link', function() { cy.readFile(json).then((obj) => { cy.visit(obj.activationLink) }) })
about 4 years ago · Juan Pablo Isaza Report

0

Establezca la cadena en la variable del body en una <template> .

Utilice los comandos Cypress.$() (es decir, jQuery) para consultarlo.

 const template = document.createElement('template') template.innerHTML = body.trim() const link = Cypress.$(template.content) .find('a[href]') .attr('href') cy.visit(link) //or cy.wrap(link).as('link') // store to a variable

Tenga en cuenta que no puede usar los comandos de Cypress en <template> ya que no está adjunto al DOM actual.

about 4 years ago · Juan Pablo Isaza Report

0

Así es como resolví este problema (usando gmail-tester como veo que también has usado / aunque cualquier api que devuelva el cuerpo del correo electrónico funcionará)

Una vez que se devuelva el cuerpo, use el método cy.document() para 'escribir' el correo electrónico en el navegador cypress runner y luego simplemente busque el enlace de la manera cypress y péguelo en cy.visit() si es necesario.

Si necesita almacenarlo en una variable, simplemente declárelo antes de la prueba y utilícelo en lugar de acceder al paso que se presenta a continuación en cy.visit(); solo recuerde que al hacerlo, su prueba dependerá del otro.

En el siguiente ejemplo de la matriz de correos electrónicos, tomo el primer objeto y luego uso su parámetro de cuerpo y HTML anidado dentro de él.

 // let link [...] .then((emails) => { const body = emails[0].body.html; cy.document({ log: false }).invoke({ log: false }, 'write', body); }) .then(() => { cy.get('a') .contains('Verify My Email') .invoke('attr', 'href') .then((href) => { cy.visit(href); // link = href }); })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!