cypress調研記錄


1. 可以實現數據驅動,但是數據驅動和json文件不通用

    json文件獲取數據:cy.fixture('login/login.json').as("login_json")
        ---json提供數據更適合全局變量

    數據驅動獲取數據:var testData = [1,2,3]
                        testData.forEach((event)=>{
                        // do something....
                        console.log(event)
                        })

這里規定讓這兩種調用分開

2. Page-Object實現:E:\cypress_sample\cypress\testpage\loginPageCy.js
    新增js文件,封裝Page頁面元素

3. 無法按照優先級運行
    當前方式可以修改文件名

4. 使用無頭瀏覽器:cypress run

5. 僅重運行失敗case,使用插件cypress-skip-and-only-ui

6. cypress打開的插件也是隱身窗口,沒有代理

7. 運行所有case:
    Jenkins 構建
        @echo off
        E:
        cd E:\cypress_sample
        cypress run --browser chrome --spec E:\cypress_sample\cypress\integration\**\* 
    可以實現執行冒煙用例和非冒煙用例選擇性運行

8. 重試-----不是很好,網絡不穩定的情況,無法解決問題,會報錯
    1. 斷言失敗測試
    2. 元素不存在重試
    3. 重試默認時間4s,配置項字段defaultCommandTimeOut

9. cypress的優點:
    1. 運行速度快
    2. 自動等待,相較於selenium的三種等待方式,代碼健壯性更好
    3. 自帶mock服務
    4. 截圖(時光穿梭)和視頻,方便定位問題
    5. 多並發--跨虛擬機並行運行測試腳本,並且收集測試結果(不收費)
    6. CI/CD
    7. 不需要依賴第三方插件
    8. 測試思路, Page-Object, 數據驅動
    9. API和UI完美結合,使測試更快

10. 使用cypress遇到的問題:
    1. 重試,沒法重運行整個case
        cypress-plugin-retries插件實現
            cypress.json
                "env":{
                    "RETRIES":2 
                }
        1. 重運行2次,需要注意的是,測試代碼中(it)失敗時,beforeEach和afterEach都重試
            還有很多情況下,鈎子不會重試,更多內容如
            https://www.npmjs.com/package/cypress-plugin-retries#how-it-works
    2. 按照優先級運行,
        1. 目前替代方案,使用冒煙用例的方式運行
        2. 在配置文件的env中用三個值分配標簽(P0,P1,P2),在case中寫入判斷,如果等於P0、P1、P2的標簽運行case,否則skip()
            if (Cypress.env("TagName")==="P0"){
                case: do something.....
            } else {
                this.skip()
                cy.log("不會執行這一步")
            }
    3. 僅支持JavaScript
    4. 並發收費--運維幫助
    5. 判斷元素是否存在----怎么判斷存在,我就執行A操作,否則執行B操作;
            if (Cypress.$("element").length > 0) {
                // do something....
            }
    6. 如何切換Windows
        1. remove target and click
            cy.get('.goods-img-wrap a').should("have.attr", "target", "_blank")
            cy.get('.goods-img-wrap a').invoke('removeAttr', 'target').click()

        2. get href and visit
            cy.get('.goods-img-wrap a').first().then(function ($a) {
                const href = $a.prop("href")
                cy.visit(href)
            })
    7. 針對刷新慢的問題,怎么處理
        1. 硬編碼,等待2s
            cy.wait(2000)
        2. 使用request,api請求成功后,再獲取price 
            // 啟動服務器以響應路由cy.route()
            cy.server() 
            cy.route("**/price**").as("priceApi")

            // 點擊事件觸發接口請求
            cy.get(".color-item").last().click()

            // 等待接口返回200,繼續后續操作
            cy.wait("@priceApi").then((xhr) => {
                expect(xhr.status).to.eq(200)
            })
    8. 配置測試報告
        1. Mochawesome報告
            1. 首先需要下載報告相關依賴macho、machowesome
                npm install --save-dev mocha@5.2.0 mochawesome mochawesome-merge mochawesome-report-generator
            2. 在cypress.json中加入如下配置
                "reporter": "mochawesome",
                "reporterOptions": {
                    "overwrite": false,
                    "html": false,
                    "json": true
                }
            3. 新增一個scripts/cypress.js 
                內容如下:
                const cypress = require('cypress')
                const fse = require('fs-extra')
                const { merge } = require('mochawesome-merge')
                const generator = require('mochawesome-report-generator')

                async function runTests() {
                    await fse.remove('mochawesome-report')
                    const { totalFailed } = await cypress.run()
                    const jsonReport = await merge()
                    await generator.create(jsonReport)
                    process.exit(totalFailed)
                }

                runTests()
            4. package.json中修改scripts
                "scripts":{
                    "cypress:run": "node scripts/cypress.js"
                }
            5. 運行cypress
                npm run cypress:run
                自動生成報告,在根目錄下:mochawesome-report
              

 

 

      
        2. junit-allure報告
            1. 在cypress.json中加入如下配置
                "reporter": "junit",
                "reporterOptions": {
                    "mochaFile": "results/my-test-output[hash].xml",   // 通過hash 標簽區分不同文件的用例結果
                    "toConsole": true
                }
            2. run cypress :   
                npm run cypress:run  會在根目錄下生成results數據
            3. 生成allure報告:  
                allure generate results --clean
            4. 打開報告:            
                allure open allure-report
    

 

 

    缺陷:報告沒有圖片
    
    9. 環境切換


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM