【问题标题】:Cypress - scrape email body and get a link out of itCypress - 抓取电子邮件正文并从中获取链接
【发布时间】: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


【解决方案1】:

您可以使用 json 文件来存储链接,然后在下一个测试中通过从 json 中检索它来访问它。

cy.readFile(json).then((obj) => {
    cy.get('a').invoke('attr', 'href').then((href) => {
        obj.activationLink = href
        cy.writeFile(json, obj)
    })
})

it('visits the link', function() {
    cy.readFile(json).then((obj) => {
        cy.visit(obj.activationLink)
    })
})

【讨论】:

    【解决方案2】:

    &lt;template&gt; 中设置body 变量中的字符串。

    使用Cypress.$()(即jQuery)命令来查询它。

    const template = document.createElement('template')
    template.innerHTML = body.trim()
    
    const link = Cypress.$(template.content)
      .find('a[href]')
      .attr('href')
    
    cy.visit(link)
    
    //or
    
    cy.wrap(link).as('link')  // store to a variable
    

    注意您不能在 &lt;template&gt; 上使用 Cypress 命令,因为它未附加到当前 DOM。

    【讨论】:

      【解决方案3】:

      这就是我解决这个问题的方法(使用gmail-tester,我看到你也使用过/尽管任何返回电子邮件正文的 api 都可以解决问题)

      返回正文后 - 使用 cy.document() 方法将电子邮件“写入”到 cypress runner 浏览器 然后只需使用 cypress 方式获取链接,并在需要时将其粘贴到 cy.visit() 中。

      如果您需要将它存储到一个变量中,只需在测试之前声明它并使用它,而不是访问下面在 cy.visit() 中提供的步骤 - 请记住,这样做您的测试将相互依赖。

      在下面的电子邮件数组示例中,我使用第一个对象,然后使用它的主体参数和嵌套在其中的 HTML。

      // let link
      [...]
            .then((emails) => {
              const body = emails[0].body.html;
              cy.document({ log: false }).invoke({ log: false }, 'write', body);
            })
            .then(() => {
              cy.get('a')
                .contains('Verify My Email')
                .invoke('attr', 'href')
                .then((href) => {
                  cy.visit(href);
                 // link = href
                });
            })
      

      【讨论】:

        猜你喜欢
        • 2021-02-14
        • 2022-11-25
        • 2019-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-09
        • 1970-01-01
        相关资源
        最近更新 更多