https://cypress.io logo
How can I extract a value from an email HTML witho...
# i-need-help
f
Hey again. I'm working on a test for Multi factor authentication (I know). And what I need to do is: 1. Configure the 2fa with an email account. 2. Receive the email, get the code. 3. Go back to the screen where I'm setting it up and enter the code. The problem that I have is that I can't leave the 2fa screen between sending the email and entering the code. So, when I bring the Email to the screen to extract the code, I leave the input page, and there is no way to go back there. (I go back to the start of the setup process). So what I'm trying to do, is get the email, and parse the content in the back, extract the code, without bringing it up to the screen, so I can get the code en Input it without leaving the screen i'm at. My code is:
Copy code
recurse(
  () = cy.task('getLastEmail'),
  Cypress._.isObject,
  {
    timeout: 60000,
    delay: 5000,
  },
)
.its('html')
.then((html) => {
  cy.document({ log: false }).invoke({ log: false}, 'write', html)
})
That gets the email, brings it up to the screen, and I can extract anything using regular cypress methods I assume this part
.invoke({ log: false}, 'write', html)
is what needs to change, but I'm not sure how can I do that. Just parsing the HTML and extracting the document but without actually showing the email in the browser, so I can remain in the screen I was at. Any ideas?
g
instead of writing email html into the page, make it into jQuery, find the code and use
f
that worked, thanks
m
Hi, can I get the code so I can look it up in the future and understand it? I have not faced this scenario/flow yet but I can face it in future when testing sign up functionality, so your code can be a big help.
f
Sure here it goes, instead of
cy.document({ log: false }).invoke({ log: false}, 'write', html)
you can do:
Copy code
.its('html')
.then((html) => {
  var email = document.createElement( 'html' );
  email.innerHTML = html
  // <add your code for the HTML doc here>
  // example
  verificationCode = email.getElementByTagName('em').item('innerText).textContent
})
m
Thank you
3 Views