Expect 失败,但测试正在 cy.on 确认和警报消息中通过

nim*_*199 1 cypress

我想测试确认消息的内容,但似乎即使我的测试失败,我的案例也会通过

it('Should Get Confirm Message', () => {
        cy.get('.button')
            .click();
        cy.on('window:confirm', str => {
            expect(2 + 2).to.equal(5);
        })
    })
Run Code Online (Sandbox Code Playgroud)

我的测试图片

Kar*_*erg 5

这实际上是很容易解决的。

只需将 Mochadone令牌传递到测试中,然后在事件处理程序中调用它即可。

it('Should Get Confirm Message', (done) => {

  // set up the event listener before the action
  cy.on('window:confirm', () => {
    expect(2 + 2).to.equal(5)
    done()                        // if it never calls done(), the test will fail
  })

  // now trigger the window.confirm message
  cy.get('.button').click()
})
Run Code Online (Sandbox Code Playgroud)

还有另一种模式使用 GlebBahmutov窗口确认弹出窗口中的存根,

// Cypress auto-accepts the confirmation
cy.contains('Ask user').click()
cy.contains('output', 'Yes').then(() => {
  cy.on('window:confirm', cy.stub().returns(false))
  cy.contains('Ask user').click()
  cy.contains('output', 'No')
})
Run Code Online (Sandbox Code Playgroud)