
- 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 - 網頁表格
- Cypress - 滑鼠操作
- Cypress - Cookie
- Cypress - Get 和 Post 請求
- Cypress - 檔案上傳
- Cypress - 資料驅動測試
- Cypress - 提示彈出視窗
- Cypress - 儀表盤
- Cypress - 截圖和影片
- Cypress - 除錯
- Cypress - 自定義命令
- Cypress - Fixtures
- Cypress - 環境變數
- Cypress - Hooks
- Cypress - JSON 檔案配置
- Cypress - 報告
- Cypress - 外掛
- Cypress - GitHub
- Cypress 有用資源
- Cypress - 快速指南
- Cypress - 有用資源
- Cypress - 討論
Cypress - JSON 檔案配置
Cypress 配置由一些鍵值對組成,這些鍵值對適用於框架中的所有測試。Cypress 預設配置在測試執行器視窗的“設定”選項卡 ->“配置”(展開)中提供。

如果我們進一步檢視同一視窗,我們將看到 Cypress 提供的多個配置的現有值,例如超時、環境變數、資料夾路徑等。
如下所示:

如果我們進一步檢視同一視窗,我們將看到 Cypress 提供的多個配置的現有值,例如超時、環境變數、資料夾路徑等。
如下所示:

覆蓋預設值
要覆蓋 cypress.json 檔案中的預設配置,我們必須指定鍵值對。

在 cypress.json 中實現
覆蓋 JSON 檔案預設值的實現如下:
{ "baseUrl" : "https://www.google.com/" }
這裡,鍵是 baseUrl,值是 https://www.google.com/。再次執行測試後,**更改將反映在全域性配置中**,如下所示:

實際測試的實現
覆蓋 JSON 檔案預設值的實際測試實現如下:
describe('Tutorialspoint', function () { // test case it('First Test', function (){ // launch application from configuration cy.visit("/") }); });
執行結果
輸出如下:

執行日誌顯示 baseUrl 已從 cypress.json 檔案中獲取,並且適用於框架中的所有測試。
覆蓋預設配置
我們可以從測試指令碼中覆蓋預設配置,這些配置適用於測試用例中的單個測試步驟,而不是整個框架。
這是藉助 Cypress 中的 config 命令實現的。
例如,如果我們想增加特定測試步驟的預設超時時間,則實現如下:
//set default time out to nine seconds from following steps in test Cypress.config('defaultCommandTimeout',9000) landPage.selectUser().click()
同時,如果在 cypress.json 檔案中將 defaultCommandTimeout 值設定為 7 秒,則 Cypress 將優先使用應用於測試步驟的超時時間(即 9 秒)。
最後,它優先考慮預設配置。
停用覆蓋預設配置
我們可以停用從 cypress.json 覆蓋預設配置的功能。
cypress.json 中的配置如下:
{ "defaultCommandTimeout" : "9000" }
要停用上述配置,請執行以下命令:
npx cypress open --config-file false
執行上述命令後,測試執行器視窗的“設定”選項卡將顯示 config 標誌設定為 false。
此外,defaultCommandTimeout 設定為 4 秒,這是由預設配置設定的,而不是由 cypress.json 中的 9 秒值覆蓋。
