首页 > 解决方案 > 我们如何使用 Cypress.io Js 自动化框架测试警报及其显示的文本?

问题描述

我们如何测试使用 Cypress.io Js 自动化框架显示的警报和文本?我无法找出赛普拉斯文档中的相关示例,请告知。

describe('Test an alert and the text displaying', function() {
it('Verify alert and its text content', function(){
    cy.visit('http://www.seleniumeasy.com/test/javascript-alert-box-demo.html')     
    cy.get('button').contains('Click me!').click()
    cy.on ('window:alert', 'I am an alert box!')    

    })

})

标签: javascriptcypress

解决方案


按照 Richard Matsen 的建议,使用 cy.stub() 方法找出答案:

describe('Test an alert and the text displaying', function() {
it('Verify alert and its text content', function(){
    cy.visit('http://www.seleniumeasy.com/test/javascript-alert-box-demo.html')    

    const stub = cy.stub()  
    cy.on ('window:alert', stub)
    cy
    .get('button').contains('Click me!').click()
    .then(() => {
      expect(stub.getCall(0)).to.be.calledWith('I am an alert box!')      
    })  

    })

})

推荐阅读