英文:
How to extract a link from a confirmation email in Mailslurp using cypress?
问题
但是常量 'const link' 返回了一个空值。
我原本期望收到链接以继续注册流程。
英文:
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
答案1
得分: 2
你可以使用类似于jsdom的库来解析HTML,以便正确获取所有链接,如下所示:
const dom = new JSDOM(email.body);
const link = dom.window.document.querySelector('a');
然而,你也可以通过正则表达式或DOM库来避免自己查找链接。我在我的公司使用Mailosaur来提取电子邮件中的链接,这样你就不必担心这个问题。
从他们的文档中这里:
it('获取密码重置邮件', () => {
cy.mailosaurGetMessage(serverId, {
sentTo: testEmail
}).then(email => {
expect(email.subject).to.equal('重置密码');
passwordResetLink = email.text.links[0].href;
})
})
因此,你的代码将如下所示:
it('提取确认链接并提取代码', () => {
cy.mailosaurGetMessage(serverId, {
sentTo: emailAddress
}).then(email => {
const link = email.text.links[0].href;
cy.visit(link);
});
});
其次,你也不需要创建收件箱或电子邮件地址并调用他们的API,你可以简单地拼接字符串来创建地址。希望这可以帮助你。
英文:
You could use a library like jsdom and parse the html so you correctly get back all links like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const dom = new JSDOM(email.body);
const link = dom.window.document.querySelector('a');
<!-- end snippet -->
However, you could also avoid having to find links yourself with regex or a dom library.
I use Mailosaur at my company for this, which extracts links from email for you so you don't have to worry about this.
From their documentation here:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
it('Gets a Password Reset email', () => {
cy.mailosaurGetMessage(serverId, {
sentTo: testEmail
}).then(email => {
expect(email.subject).to.equal('Reset your password');
passwordResetLink = email.text.links[0].href;
})
})
<!-- end snippet -->
So your code would look like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
it('extract the confirmation link and extract the code', () => {
cy.mailosaurGetMessage(serverId, {
sentTo: emailAddress
}).then(email => {
const link = email.text.links[0].href;
cy.visit(link);
});
});
<!-- end snippet -->
Secondly, you also don't need to create an inbox or an email address and call their API, you can just make up addresses as simple as concatenating a string.
I hope that helps you.
答案2
得分: 0
考虑到错误发生的位置,这很可能是由于正则表达式不匹配造成的。你的正则表达式匹配例如<a href="www.example.com">
,但不匹配<a href="www.example.com" target="_blank">
,或者链接具有任何id、类或其他属性。
尝试将你的正则表达式更改为const regexLink = /<a href="([^"]+)".*>/;
。你还需要将你的链接更改为const link = match[1];
,因为match[0]是整个匹配的字符串,而不是第一个捕获组。
英文:
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 to const link = match[1];
since match[0] is the entire string matched, not the first capturing group.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论