【问题标题】:How to repeat a test in Cypress multiple times?如何在赛普拉斯多次重复测试?
【发布时间】:2022-01-05 13:03:49
【问题描述】:

我的 Cypress 测试中有以下代码部分:

cy.window()
  .its('store')
  .invoke('getState')
  .then((state) => {
    expect(state.app.gameStarted).to.equal(true)
    expect(state.app.noteButtonValues).to.have.lengthOf(4);
    expect(state.app.noteButtonValues).to.include(state.app.correctAnswer)
    cy.get("button").contains(state.app.correctAnswer).click()

    cy.window()
    .its('store')
    .invoke('getState')
    .then((state) => {  
        expect(state.app.correctAnswered).to.equal(1)
        expect(state.app.totalAnswered).to.equal(1)
    })   
  })
})

我正在测试当用户点击按钮时,redux 状态也会更新。现在假设我想重复这部分点击 100 次左右。这怎么可能?

将它包装在一个 for 循环中是行不通的:

for (let i = 0; i < 100; i = i++) {
    cy.window()
    .its("store")
    .invoke("getState")
    .then((state) => {
        expect(state.app.gameStarted).to.equal(true);
        expect(state.app.noteButtonValues).to.have.lengthOf(4);
        expect(state.app.noteButtonValues).to.include(state.app.correctAnswer);
        cy.get("button").contains(state.app.correctAnswer).click();

        cy.window()
        .its("store")
        .invoke("getState")
        .then((state) => {
            expect(state.app.correctAnswered).to.equal(1);
            expect(state.app.totalAnswered).to.equal(1);
        });
    });
}

【问题讨论】:

    标签: reactjs redux automated-tests cypress


    【解决方案1】:

    使用lodash方法Cypress._.times()

    例子:

    Cypress._.times(100, (k) => {
      it(`typing hello ${k + 1} / 100`, () => {
        cy.log(k)
      })
    })
    

    【讨论】:

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