自动化测试,让你上班拥有一杯咖啡的时间 | Day 30 - 学习cypress intercept 与後记

此系列文章会同步发文到个人部落格,有兴趣的读者可以前往观看喔。

终於来到铁人赛第30天!谢谢观看我的文章和留言的所有人,第一次参加铁人赛,每天早上都要提醒自己发文,比赛过程中遇到两个连假,深怕自己会撑不到30天,感谢自己无数个深夜和清晨的努力,才能够顺利的完成比赛。

今天要跟大家分享 cy.intercept(),在 Cypress 6.0.0 後就不再使用 cy.server()cy.route()

使用 cy.intercept() 在网路层管理 HTTP 请求的行为。

  • 对 network requests 和 responses 进行 stub 或 spy
  • 在将 HTTP request 送到 server 之前可以修改 body, herders, URL 等等

method

可以用 GET, POST, PUT, PATCH, DELETE 等等

cy.intercept('/users')
// matches this: GET http://localhost/users
// ...and this, too: POST http://localhost/users

cy.intercept('GET', '/users')
// matches this: GET http://localhost/users
// ...but not this: POST http://localhost/users

url

可以用完整的 URL 或正规表示法

cy.intercept('https://prod.cypress.io/users')
// match any request that exactly matches the URL

cy.intercept('/users?_limit=*')
// match any request that satisfies a glob pattern

cy.intercept(/\/users\?_limit=(3|5)$/)
// match any request that satisfies a regex pattern
  1. 语法

    // spying only
    cy.intercept(url)
    cy.intercept(method, url)
    cy.intercept(routeMatcher)
    
  2. 动手写程序

    describe('测试intercept', function() {
        it('url用法', function() {
            cy.intercept('https://www.thsrc.com.tw/Event/LanguageSettings.json')
        })
        it('method用法', function() {
            cy.intercept('GET', 'https://www.thsrc.com.tw/Event/LanguageSettings.json', {
                statusCode: 200,
            })
        })
    })
    

    https://ithelp.ithome.com.tw/upload/images/20211014/2014088347mwwbPmQh.png
    https://ithelp.ithome.com.tw/upload/images/20211014/201408837Isx0F8bA5.png

最後跟大家分享导入 Cypress 的价值,有机会你也可以试试看自动化测试。

  1. 因官网的文件丰富,学习曲线较低
  2. 可以累积过往的测试案例
  3. 可以记录测试结果,并将测试的过程录制下来,输出成可以量化的报告
  4. 可以整合 CI/CD 自动化测试
  5. 帮助测试人员找到系统的 bug

参考资料


<<:  Day 29 : 案例分享(9) 活动 - 线上报名、线上缴费及线上会议

>>:  表格元件共用攻略

[DAY-02] 没有规则 就是唯一规则

有句话是这样说的 好的管理就是不用管理 如果大家都自动自发的 那鼔能量会很令人敬佩 唷唷唷~~~ ...

Kotlin Android 第13天,从 0 到 ML - Activity 和 Activity 生命周期

前言: ConstraintLayout讲完了,画面画好了那是用在那呢? 那就是要放在 Activi...

第27天:实作档案上传功能(4)

昨天我们建立一个File资料表要来储存档案实体路径 CREATE TABLE [dbo].[File...

铁人赛 Day29 -- 使用 JQuery 制作简易的loading画面

前言 一个网站要让他更完整,势必就需要当使用者进入网站时,先进行读取,但由於技术成分不足,只好先做...

Day10 有图有真相

Chart function 身为一个键盘柯南,最重要的技能之一就是储存和下载分析後的结果。另外c...