javascript - 如何使用 Cypress 将变量的存储值获取到 URL 命令中?

标签 javascript scope cypress

我正在尝试使用 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

Grab a string to use in a .visit() call in cypress

最佳答案

我们最终编写了自己的迷你应用程序来处理这种情况,但同样的想法可以用于 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/

相关文章:

javascript - 如何防止对 react onClick 函数(在 useEffect 之外)进行状态更新?

javascript - 表格单元格的工具提示 ASP.NET

javascript - 对象文字和javascript中构造函数中具有值的类之间有什么区别?

javascript - 防止在 Javascript 中自动创建全局变量

javascript - ReactJS 在渲染函数中访问变量超出范围

比较两个值并在 Cypress 中做出决定

javascript - 如何将字符串列表从 flask python main 函数传递给 html 中的 javascript?

javascript - 从页面 CSS 中分离注入(inject)内容的样式

javascript - Cypress 和 cookies 弹出窗口 : how to get rid of it

reactjs - Cypress 组件测试、ReactJS 和 TailwindCSS