首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Cypress.io Js自动化框架测试警报及其显示的文本?

如何使用Cypress.io Js自动化框架测试警报及其显示的文本?
EN

Stack Overflow用户
提问于 2018-08-11 08:03:01
回答 3查看 18.5K关注 0票数 17

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

代码语言:javascript
复制
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!')    

    })

})
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-08-11 08:19:56

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

代码语言:javascript
复制
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!')      
    })  

    })

})
票数 20
EN

Stack Overflow用户

发布于 2019-02-27 02:31:07

这是一种更简单、更直观的方法:

代码语言:javascript
复制
cy.on('window:alert', (str) => {
  expect(str).to.equal(`This is an alert box!`)
})

我发现做这件事的stub()方法太混乱,不直观,而且容易出错。

票数 13
EN

Stack Overflow用户

发布于 2019-07-16 23:54:11

如果你碰巧使用了这个:alert.js,那么也许我可以帮你省去一些麻烦。尝试类似这样的操作来查找未注册到DOM的元素:

代码语言:javascript
复制
// for example, a button in the modal that needs clicking

// the event that fires the alert
 cy.get('<some element>').click()

 cy.window().then(win => {
        const el = win.Alert.$(`<whatever element you're looking for>`)
        cy.wrap(el)
          .find('button')
          .contains('Confirm')
          .click()
      })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51795306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档