The web has evolved. Finally, testing has too.css
cypress 最近很火的e2e(即end to end(端到端))測試框架,它基於node js,Jquery的一些用法他裏面也能夠通用。html
API詳解參考
node
1.1 VSCode安裝,參考這裏,直接根據平臺下載安裝。git
1.2 Cypress安裝,參考這裏github
describe('My first test case for cypress',function(){
it('visit baidu home page and search for testerhome:',function(){ cy.visit('http://www.baidu.com') //訪問url cy.title().should('contain','百度一下,你就知道') //驗證頁面 title 是否正確 cy.get('#kw') //根據 css 定位搜索輸入框 .type('testerhome') //輸入關鍵字 .should('have.value','testerhome') //驗證關鍵字自動是否展現正確 cy.get('#su').click() //根據 css 定位搜索按鈕並點擊 cy.url().should('include','wd=testerhome') //驗證目標url 是否正確包含關鍵字 cy.title().should('contain','testerhome_百度搜索') //驗證頁面 title 是否正確 cy.get('[id="1"]') .should('contain','TesterHome') // 驗證第一個結果中是否包含TesterHome cy.screenshot() }) })
代碼解釋:web
expect([]).to.be.a('Array') // 判斷類型 expect(a.b).to.exist // 判斷屬性是否存在 expect(1).to.be.oneOf([1,2,3]) // 判斷值是不是其中之一 expect('testing').to.match(/^test/) // 正則匹配 ...
從左向右分別顯示:成功數、失敗數、未運行、耗時,是否自動滾屏和從新運行按鈕json
注意在編寫用例時,每次保存會自動觸發測試,對於調試來講是比較方便的。api
1.點擊選擇器 ->2.點擊定位元素 ->3.複製生成代碼框架
在 cypress.json 中添加依賴:
"reporter": "junit",
"reporterOptions": {
"mochaFile": "results/my-test-output[hash].xml", // 經過hash 標籤區分不一樣文件的用例結果
"toConsole": true
}
執行 cypress run 的時候會自動生成xml文件
使用 allure 生成對應報告:
// 生成allure 報告
allure generate results --clean
// 打開報告
allure open allure-report
3.4 CI/CD
優勢: