Angular Cypress在单独的测试中触发cy.wait(1000)两次

Angular Cypress在单独的测试中触发cy.wait(1000)两次,angular,e2e-testing,cypress,Angular,E2e Testing,Cypress,在我的Angular Cypress E2E测试中,我想测试一个有点混乱的场景:填写一个注册表(localhost)并发送它,然后转到(假)邮箱并收集验证电子邮件。因为FakeSMTP需要一些时间,所以我想在访问它之前等待 it('should submit the registration', () => { cy.visit('https://localhost/register); ... cy.get('button[type=submit]').should('not

在我的Angular Cypress E2E测试中,我想测试一个有点混乱的场景:填写一个注册表(localhost)并发送它,然后转到(假)邮箱并收集验证电子邮件。因为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)
会触发两次?第一次提交(在最后一件事上),第二次实际在电子邮件收集中到期。

试试这个:)

cypress不推荐单独使用
cy.wait()
,请参见


在中,尊重良好实践(如果您的邮件实用程序可能的话)是使用其API来检索邮件,而不是通过接口进行检索

我认为Cypress在遇到新域时重置了第二个测试,请参见此注释

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

为了验证这一点,这个简单的测试反映了所发生的情况

description('changing domains',()=>{
它('转到example.com',()=>{
参观http://example.com')
})
它('changes to example.net',()=>{
等一下(10000)
参观http://example.net')
})
})
有两种方法(可能)可以避免这个问题,但不确定这两种方法是否对你有效

  • 在第二次测试的早期,使用一个虚拟调用转到新域
  • description('changing domains',()=>{
    它('转到example.com',()=>{
    参观http://example.com')
    })
    它('changes to example.net',()=>{
    参观http://example.net')
    等一下(10000)
    参观http://example.net')
    })
    })
    
  • 在第一次测试中等待
  • description('changing domains',()=>{
    它('转到example.com',()=>{
    参观http://example.com')
    等一下(10000)
    })
    它('changes to example.net',()=>{
    参观http://example.net')
    })
    })
    

    理想的解决方案是将FakeSMTP服务器从测试中删除,并将邮件发送设为陷阱,就像XHR帖子设为陷阱并用
    cy.route()
    存根一样,这样您就不必等待10秒,但我看不到任何示例,所以假设这还不可能。也许是本地事件到来的时候


    我看过这篇文章,它创建了一个自定义命令,使用递归轮询服务器,直到出现电子邮件为止。它本质上类似于cypress命令重试,因此您不会等待任意时间(正如@Jboucly所说,应该避免),而是以300毫秒的增量等待,直到电子邮件到达

    Cypress.Commands.add('getlastmail',email=>{
    函数requestEmail(){
    返回cy
    .请求({
    方法:“GET”,
    网址:'http://localhost:4003/last-电邮‘’,
    标题:{
    “内容类型”:“应用程序/json”,
    },
    qs:{
    电子邮件,
    },
    是的,
    })
    。然后({body})=>{
    如果(正文){
    返回体;
    }
    //若body为null,则表示未获取此地址的电子邮件。
    //我们递归调用requestEmail,直到收到一封电子邮件。
    //我们还将在每次调用之间等待300毫秒,以避免向服务器发送垃圾邮件
    cy.wait(300);
    返回requestEmail();
    });
    }
    返回requestEmail();
    });
    

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

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