I was trying to extract a link to proceed with user registration using Cypress and Mailslurp. For that, I wrote the following code:
let inboxId;
let emailAddress;
describe('sign up', () => {
beforeEach(() => {
cy.viewport(1920, 1080);
})
it('receive sign up link', () => {
cy.visit('/signup');
cy.createInbox().then(inbox => {
// verify a new inbox was created
assert.isDefined(inbox)
// save the inboxId for later checking the emails
inboxId = inbox.id
emailAddress = inbox.emailAddress;
cy.get('#email-input').type(emailAddress);
cy.get('.bg-gray-100 > .p-button').click();
})
})
it('extract the confirmation link and extract the code', () => {
cy.waitForLatestEmail(inboxId).then(email => {
const emailHTML = email.body;
const regexLink = /<a href="([^"]+)">/;
const match = emailHTML.match(regexLink);
const link = match[0];
cy.visit(link);
});
});
})
But the constant 'const link' returns a null value
I was expecting to receive the link to continue the register link
Considering the place where the error occurs this is most likely caused by the regex not matching. Your regex matches for example
<a href="www.example.com">
but not<a href="www.example.com" target="_blank">
or if the link has any id, classes or other properties.Try changing your regex to
const regexLink = /<a href="([^"]+)".*>/;
. You'll also need to change your link toconst link = match[1];
since match[0] is the entire string matched, not the first capturing group.