【发布时间】:2022-06-16 23:39:01
【问题描述】:
使用 Cypress 进行测试自动化,我正在尝试找到一种解决方案,了解如何抓取电子邮件正文、从中获取链接、将其存储在变量中,然后通过 cy.visit() 访问链接。
/// <reference types="Cypress" />
const sender = "sender@companyemail.com";
const companyEmail = "company@company.com"
const emailSubject = "[Action required] Activate your 14-day Dataddo trial";
describe("Sign-Up Email assertion and visit confirmation link", () => {
it("Sign-Up and Look for an email with specific subject and link in email body", function () {
cy.task("gmail:get-messages", {
options: {
from: sender,
to: companyEmail,
subject: emailSubject,
include_body: true
}
}).then(emails => {
assert.isNotNull(
emails,
"Expected to find at least one email, but none were found!"
);
cy.log("Email has been found successfully")
assert.isAtLeast(
emails.length,
1,
"Expected to find at least one email, but none were found!"
);
cy.log(`Email length is: ${emails.length}`)
cy.log(`Email Recipient is: ${companyEmail}`)
const body = emails[0].body.html; // returns email body (see the mock-up below)
/*
TODO
- Parse email body, get the confirmation link out of it
- Store the link it a variable
- Visit the link via cy.visit
- Note that tokenid is going to be different with every run
*/
})
})
})
预期电子邮件正文的模型如下所示。
<html>
<head>
</head>
<body>
<p>This is SignUp Email with confirmation link</p>
<p>
<a href = "http://www.company.com/ls/click?upn=tokenid-11111-22222-333333-444444-555555-xxxxxx"><a>
</p>
</body>
</html>
感谢您的帮助。
【问题讨论】:
-
电子邮件能给你什么?也许您可以使用
.get方法立即在已检查的正文中搜索,而不使用.html方法? -
我的意思是你得到 Array 或 NodesArray?
-
const body 预计会返回第二部分(mock-up email body)
标签: javascript jquery cypress