I need a help about testing mailhog with cypress.
I am trying to click on "Forgot password" link in email body, any advice how to do it?
I need a help about testing mailhog with cypress.
I am trying to click on "Forgot password" link in email body, any advice how to do it?
Assuming you have an HTML-based web app, you can directly use the text to find and click the element.
cy.contains('Forgot password').click()
You can parse the body string to get the link, but it would be messy.
Better to use a DOMParser
cy.mhGetAllMails().mhFirst().mhGetBody().then(body => {
const parser = new DOMParser();
const doc = parser.parseFromString(body, 'text/html') // make a DOM
const anchor = doc.querySelector('a') // look for anchor tag
const href = anchor.href // get the link
cy.visit(href) // visit the link
})
Notes
You can't click on the link directly with .click() since the DOM created above is not the live one attached to Cypress. But you should be able to cy.visit(href) which does the same thing.
The only problem I foresee is a cross-origin error - if you get that, use the cy.origin() command Ref.
Please see @Mr.PrasadJ question How to access new tab by clicking on "href" if you need more details on cy.origin() usage with email body.