【问题标题】:How can I get the stored value of a variable into a URL command using Cypress?如何使用 Cypress 将变量的存储值获取到 URL 命令中?
【发布时间】:2020-10-21 22:16:12
【问题描述】:

我正在尝试使用 Cypress 和 mailslurp 编写端到端测试。我可以从入职电子邮件中检索激活 URL,并将值存储在名为 verifyLink 的变量中。现在我有了 URL,我想在 cy.visit(URL) 中使用它,但我不知道如何从 verifyLink 中获取存储的值。

我知道下面的代码是不正确的,但它让你知道我想要做什么。

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

【问题讨论】:

  • cy.visit 放入回调中,您可以在哪里访问该值?
  • 感谢@jonrsharpe,您是正确的(我的错误),但问题是我无法在 cy.visit() 调用中返回变量。我从 cypress 收到一个错误:cy.visit() 必须使用 url 或包含 url 作为其第一个参数的选项对象来调用。
  • regex.exec 的结果不是字符串...

标签: javascript scope cypress


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2020-09-10
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    相关资源
    最近更新 更多