我想测试确认消息的内容,但似乎即使我的测试失败,我的案例也会通过
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)
这实际上是很容易解决的。
只需将 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)
归档时间: |
|
查看次数: |
799 次 |
最近记录: |