【问题标题】:Cypress triggers cy.wait(1000) twice in separate tests赛普拉斯在单独的测试中触发 cy.wait(1000) 两次
【发布时间】:2020-01-13 16:07:22
【问题描述】:

在我的 Angular Cypress E2E 测试中,我想测试一个有点混乱的场景:填写注册表单 (localhost) 并将其发送,然后转到 (Fake-)Mailbox 并收集验证电子邮件。由于 FakeSMTP 需要一些时间,我想在访问之前等待。

it('should submit the registration', () => {
  cy.visit('https://localhost/register);
  ...
  cy.get('button[type=submit]').should('not.be.disabled').click();
});

// Collect the Mail
it('should collect the registration activation email', () => {

  /**
   * Wait 10 Seconds for E-Mail to arrive
   */
  cy.wait(10000); // --> this fires twice

  cy.visit('https://localhost:5080');
  ...
});

为什么cy.wait(10000) 会触发两次?第一次在提交中(在最后一件事),第二次在电子邮件集合中实际到期。

【问题讨论】:

  • 您是否尝试过像这样使用before() 钩子:before(() => { cy.wait(10000) })
  • 这似乎在第一次测试之前运行一次?这不是我想要的,因为电子邮件检查是第二个测试。

标签: angular e2e-testing cypress


【解决方案1】:

我认为 Cypress 在遇到新域时正在重置第二次测试,请参阅此评论 Change baseUrl during test

赛普拉斯更改父域以匹配 baseUrl,以避免在与父域不匹配的网站上导航时出现问题。赛普拉斯产品只是没有编码来处理更改 baseUrl 中间测试。

为了检查它,这个简单的测试反映了发生的情况

describe('changing domains', () => {

  it('goes to example.com', () => {
    cy.visit('http://example.com')
  })

  it('changes to example.net', () => {
    cy.wait(10000)
    cy.visit('http://example.net')
  })
})

有几种方法可以(可能)避免该问题,但不确定是否适合您。

  1. 在第二个测试的早期使用虚拟调用进入新域,
describe('changing domains', () => {

  it('goes to example.com', () => {
    cy.visit('http://example.com')
  })

  it('changes to example.net', () => {
    cy.visit('http://example.net')
    cy.wait(10000)
    cy.visit('http://example.net')
  })
})
  1. 等待第一次测试,
describe('changing domains', () => {

  it('goes to example.com', () => {
    cy.visit('http://example.com')
    cy.wait(10000)
  })

  it('changes to example.net', () => {
    cy.visit('http://example.net')
  })
})

理想的解决方案是将 FakeSMTP 服务器从测试中删除并以与 XHR 帖子被捕获和存根 cy.route() 相同的方式捕获邮件发送,然后您不必等待 10 秒,但我不需要'没有看到任何例子,所以假设它还不可能。也许会在原生事件到来的时候。


我查看了这篇帖子Testing an email workflow from end to end with Cypress,它使用递归创建了一个自定义命令来轮询服务器,直到电子邮件出现。它本质上就像一个 cypress 命令重试,所以你不会等待任意时间(正如@Jboucly 所说的应该避免),而是以 300 毫秒的增量等待,直到电子邮件到达。

Cypress.Commands.add('getLastEmail', email => {
  function requestEmail() {
    return cy
      .request({
        method: 'GET',
        url: 'http://localhost:4003/last-email',
        headers: {
          'content-type': 'application/json',
        },
        qs: {
          email,
        },
        json: true,
      })
      .then(({ body }) => {
        if (body) {
          return body;
        }

        // If body is null, it means that no email was fetched for this address.
        // We call requestEmail recursively until an email is fetched.
        // We also wait for 300ms between each call to avoid spamming our server with requests
        cy.wait(300);

        return requestEmail();
      });
  }

  return requestEmail();
});

它确实应该有一个递归深度限制,以防万一事情没有按预期进行并且电子邮件永远不会到达。

【讨论】:

    【解决方案2】:

    试试这个:)

    cy.visit('http=//localhost:5080').wait(10000);
    

    cypress 不强烈推荐单独使用cy.wait (),请参阅here

    doc 中,尊重良好做法(如果可能的话,使用您的邮件实用程序)是使用它的 API 来检索邮件,而不是通过接口来完成

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-12
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-14
      • 1970-01-01
      • 2021-11-06
      • 2021-10-20
      相关资源
      最近更新 更多