- Cypress 教程
- Cypress - 首頁
- Cypress - 簡介
- Cypress - 架構和環境設定
- Cypress - 測試執行器
- Cypress - 構建第一個測試
- Cypress - 支援的瀏覽器
- Cypress - 基本命令
- Cypress - 變數
- Cypress - 別名
- Cypress - 定位器
- Cypress - 斷言
- Cypress - 文字驗證
- Cypress - 非同步行為
- Cypress - 處理XHR
- Cypress - jQuery
- Cypress - 複選框
- Cypress - 標籤頁
- Cypress - 下拉列表
- Cypress - 警報
- Cypress - 子視窗
- Cypress - 隱藏元素
- Cypress - 框架
- Cypress - Web表格
- Cypress - 滑鼠操作
- Cypress - Cookie
- Cypress - Get和Post請求
- Cypress - 檔案上傳
- Cypress - 資料驅動測試
- Cypress - 提示彈出視窗
- Cypress - 儀表盤
- Cypress - 截圖和影片
- Cypress - 除錯
- Cypress - 自定義命令
- Cypress - Fixture
- Cypress - 環境變數
- Cypress - Hook
- Cypress - JSON檔案配置
- Cypress - 報告
- Cypress - 外掛
- Cypress - GitHub
- Cypress 有用資源
- Cypress - 快速指南
- Cypress - 有用資源
- Cypress - 討論
Cypress - 構建第一個測試
一旦 Cypress 配置完成,專案中就會建立一個框架,該框架會自動在資源管理器中顯示。新的測試檔案(例如 FirstTest.spec.js)應在 integration 資料夾中建立,如下所示。
Cypress 資料夾結構
讓我們瞭解 Cypress 中的資料夾結構。下面解釋了 Cypress 資料夾中包含的因素:
fixtures - 測試資料以鍵值對的形式儲存在此處,用於測試。
integration - 框架的測試用例儲存在此處。
plugins - Cypress 事件(在測試執行前和後執行的事件)儲存在此處。
support - 可重用的方法或自定義命令建立在此處,測試用例可以直接使用這些方法或命令,而無需建立物件。
videos - 執行的測試步驟以影片的形式記錄並存儲在此處。
node_modules - 來自 npm 的專案依賴項儲存在此資料夾中。它是 Cypress 專案執行的核心。
cypress.json - 預設配置設定在此資料夾中。可以在這裡修改當前配置的值,這將覆蓋預設配置。
package.json - 專案的依賴項和指令碼儲存在此資料夾中。
基本測試的結構
Cypress 遵循 JavaScript 測試框架(Mocha、Jasmine 等)。要在 Cypress 中建立測試,我們必須遵守以下框架指南:
必須在 describe 函式中提供測試套件名稱。
測試套件中的測試用例名稱必須在同一函式中提供,或者您必須指定函式。
測試用例中的測試步驟必須在 it/specify 塊內實現。
基本測試實現
可以使用以下命令完成基本測試實現:
// test suite name
describe('Tutorialspoint Test', function () {
// Test case
it('Scenario 1', function (){
// test step for URL launching
cy.visit("https://www.google.com/");
});
});
上面使用的 cy 命令不需要物件呼叫。安裝 node 模組後,它預設可用。
測試執行
要從命令列執行,請執行以下命令:
./node_modules/.bin/cypress run
這裡,integration 資料夾中的所有檔案都會被觸發。
要從測試執行器執行,請執行以下命令:
./node_modules/.bin/cypress open
然後,點選我們想要觸發執行的規範檔案。
要從命令列觸發特定檔案的執行,請執行以下命令:
cypress run --spec "<spec file path>"
您的計算機上將出現以下螢幕: