一.啟動Cypress
進入cypress安裝目錄,輸入yarn run cypress open 啟動cypress

二.啟動測試項目
進入到項目文件夾啟動測試項目

三.編寫測試腳本
在生成的integration文件夾下編寫測試用例testLogin.js
1 //testLogin.js 2 ///<reference types="cypress"/> 3 4 describe('登錄', function(){ 5 //此用戶名密碼服務器默認 6 const username = 'jane.lane' 7 const password = 'password123' 8 9 context('HTML 表單登錄測試',function(){ 10 11 it('登陸成功,跳轉到dashboard頁',function(){ 12 cy.visit('http://localhost:7077/login') 13 cy.get('input[name=username]').type(username) 14 cy.get('input[name=password]').type(password) 15 cy.get('form').submit() 16 17 //斷言,驗證登陸成功則跳轉dashbard頁面 18 //斷言,驗證用戶名存在 19 cy.url().should('include', '/dashboard') 20 cy.get('h1').should('contain','jane.lane') 21 22 } 23 ) 24 } 25 ) 26 } 27 )

四.在Cypress中運行測試腳本
在啟動的Cypress中點擊添加的測試腳本,運行。

五.查看斷言結果及相關頁面

