我正在尝试使用 Cypress 和 mailslurp 编写端到端测试。我可以从入职电子邮件中检索激活 URL,并将该值存储在一个名为 verificationLink 的变量中。现在我有了 URL,我想在 cy.visit(URL) 中使用它,但我不知道如何从 verificationLink 中获取存储的值。
我知道以下代码不正确,但它让您了解我正在尝试做什么。
cy.waitForLatestEmail('inbox.id').then(email => {
console.log(email)
const verificationLink = /my-regex-code-to-get-the-link,e.g. \/([0-9]{6})$\//.exec(email.body);
console.log(verificationLink)
cy.visit(verificationLink)
})
我已尝试按照以下链接中的说明/建议进行操作,但没有成功。有谁知道如何做到这一点?
https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Closures
最佳答案
我们最终编写了自己的迷你应用程序来处理这种情况,但同样的想法可以用于 mailSlurp。
cy.latestEmail(inboxState).then((body) => {
console.log(`Body is ${JSON.stringify(body)}`) //Transform the body of the latest email into a JSON string.
userEmail = body.email //Save the email of the user based on the "email" property in the JSON
console.log(userEmail)
//Match the verification link embedded in the email
const verificationLink = /http(s?):\/\/(MY_URL_ADDRESS_HERE\/account\/verifySms\?key=(\w+)/gm.exec(userEmail.content) //where array content returned will include the entire HTML content of the email
console.log(verificationLink) //verificationLink = the array
assert.isTrue(verificationLink.length > 0, 'verification link not found')
verificationURL = verificationLink[0]
cy.log(verificationURL)
关于javascript - 如何使用 Cypress 将变量的存储值获取到 URL 命令中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62677287/