如何在Cypress.io中多次运行测试

Web*_*ity 2 unit-testing cypress

我有一个测试用例来修复X出现1次的错误。我想多次运行同一测试,但是找不到任何说明如何自动重新启动测试以及在达到阈值时停止测试的文档。任何见解表示赞赏

Get*_*ald 23

您可以在循环内添加测试块。

Cypress与一些库捆绑在一起lodash可以使用以下方法来做到这一点:Cypress._.times方法:

Cypress._.times(10, () => {
  describe('Description', () => {
    it('runs 10 times', () => {
      //...
    });
  });
});
Run Code Online (Sandbox Code Playgroud)


Sam*_*Sam 11

您可以将循环放入外部空间,而不是将循环放入测试中,如下所示

var i = 0;
for (i = 0; i < 3 ; i++) {     
  describe('Verify "Login" is visible. Test: '+i, function() {
    it('finds the Login link in the header', function() {

      //Place code inside the loop that you want to repeat

    })
  })
}
Run Code Online (Sandbox Code Playgroud)

结果如下:

  • 验证“登录”是否可见。测试:0
  • 在标题中找到登录链接
  • 验证“登录”是否可见。测试:1
  • 在标题中找到登录链接
  • 验证“登录”是否可见。测试:2
  • 在标题中找到登录链接

  • 杰出的!有时我必须提醒自己,这只是 JavaScript,没有特殊的语言语法,允许这样的用例。 (2认同)
  • 您可以将 it() 函数本身封装在 for 循环内,而不是为每个测试创建新的描述 (2认同)

Web*_*ity 6

我完全隔开了,并且忘记了这些是普通的JS文件,因此我将测试包裹在for循环中。这似乎按我的预期工作。

describe('Verify "Login" is visible', function() {
  it('finds the Login link in the header', function() {
    var i = 0;
    for (i = 0; i < 5 ; i++) { 
      //Place code inside the loop that you want to repeat
      cy.visit('https://www.example.com/page1')
      cy.get('.navbar').contains('Login').should('be.visible')
      cy.visit('https://www.example.com/page2')
      cy.get('.navbar').contains('Login').should('be.visible')
    }      
  })
})
Run Code Online (Sandbox Code Playgroud)