前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Cypress web自动化38-alert 弹窗

Cypress web自动化38-alert 弹窗

作者头像
上海-悠悠
发布2020-06-17 15:18:54
1.4K0
发布2020-06-17 15:18:54
举报

前言

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

Alert 弹窗

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

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

代码语言:javascript
复制
/**
 * Created by dell on 2020/6/9.
 * 作者:上海-悠悠 交流QQ群:939110556
 */

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('已经记录下您的使用偏好')
        })
    })

    })

运行结果

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-06-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • Alert 弹窗
  • 百度搜索案例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档