我试图提取一个链接以使用 Cypress 和 Mailslurp 继续进行用户注册。为此,我编写了以下代码:
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);
});
});
})
但常量“const link”返回空值
我期待收到继续注册链接的链接
您可以使用像 jsdom 这样的库并解析 html,以便您正确地返回所有链接,如下所示:
const dom = new JSDOM(email.body);
const link = dom.window.document.querySelector('a');
但是,您也可以避免自己使用正则表达式或 dom 库查找链接。
我在公司使用 Mailosaur 来实现此目的,它可以为您从电子邮件中提取链接,因此您不必担心这一点。
从他们的文档中here:
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;
})
})
所以你的代码看起来像这样:
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);
});
});
其次,您也不需要创建收件箱或电子邮件地址并调用他们的 API,您可以像连接字符串一样简单地创建地址。
我希望这对你有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)