Cypress web 自动化 - alert 弹窗

前言

当页面上出现 alert 弹窗时候,Cypress 自动接受 alert, 运行代码的时候虽然看不到弹窗页面,但是依然可以对文本内容断言

Alert 弹窗

Cypress 自动接受 alert,但您仍然可以对文本内容进行断言,使用示例

// app code
$('button').on('click', (e) => {
  alert('hi')
  alert('there')
  alert('friend')
})

it('can assert on the alert text content', () => {
  const stub = cy.stub()

  cy.on('window:alert', stub)

  cy
    .get('button').click()
    .then(() => {
      expect(stub.getCall(0)).to.be.calledWith('hi')
      expect(stub.getCall(1)).to.be.calledWith('there')
      expect(stub.getCall(2)).to.be.calledWith('friend')
    })
})

百度搜索案例

百度 - 搜索设置 - 保存设置,弹出 alert


describe('baidu alert', function() {

    before( function() {
        cy.visit("https://www.baidu.com/")

        cy.get("#s-usersetting-top").trigger('mouseover')  // 鼠标悬停
        cy.contains("搜索设置").click()
    })


    it('assert alert text', () => {
      const stub = cy.stub()
      cy.on('window:alert', stub)

      cy
          .contains("保存设置").click()
          .then(() => {
          expect(stub.getCall(0)).to.be.calledWith('已经记录下您的使用偏好')
        })
    })

    })

运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据搜集者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值