
- WebdriverIO 教程
- WebdriverIO - 首頁
- WebdriverIO - 簡介
- WebdriverIO - 前提條件
- WebdriverIO - 架構
- WebdriverIO - 使用 NodeJS 入門
- WebdriverIO - NPM 安裝
- WebdriverIO - VS Code 安裝
- WebdriverIO - Package.json
- WebdriverIO - Mocha 安裝
- Selenium 獨立伺服器安裝
- WebdriverIO - 配置檔案生成
- WebdriverIO - VS Code 智慧提示
- WebdriverIO - wdio.conf.js 檔案
- WebdriverIO - XPath 定位器
- WebdriverIO - CSS 定位器
- WebdriverIO - 連結文字定位器
- WebdriverIO - ID 定位器
- WebdriverIO - 標籤名稱定位器
- WebdriverIO - 類名定位器
- WebdriverIO - 名稱定位器
- 斷言的 Expect 語句
- WebdriverIO - 成功路徑流程
- WebdriverIO - 通用瀏覽器命令
- WebdriverIO - 處理瀏覽器大小
- WebdriverIO - 瀏覽器導航命令
- 處理複選框和下拉選單
- WebdriverIO - 滑鼠操作
- 處理子視窗/彈出視窗
- WebdriverIO - 隱藏元素
- WebdriverIO - 框架
- WebdriverIO - 拖放
- WebdriverIO - 雙擊
- WebdriverIO - Cookie
- WebdriverIO - 處理單選按鈕
- Web元素上的 Chai 斷言
- WebdriverIO - 多個視窗/標籤頁
- WebdriverIO - 滾動操作
- WebdriverIO - 警報
- WebdriverIO - 除錯程式碼
- WebdriverIO - 捕獲螢幕截圖
- WebdriverIO - JavaScript 執行器
- WebdriverIO - 等待
- WebdriverIO - 並行執行測試
- WebdriverIO - 資料驅動測試
- 從命令列引數執行測試
- 使用 Mocha 選項執行測試
- 從 Allure 生成 HTML 報告
- WebdriverIO 有用資源
- WebdriverIO - 快速指南
- WebdriverIO - 有用資源
- WebdriverIO - 討論
WebdriverIO - 配置檔案生成
WebdriverIO 測試由配置檔案控制。它通常被認為是 WebdriverIO 的核心。它包含要執行的測試用例的詳細資訊、執行測試的瀏覽器、全域性資訊 - 超時、報告、螢幕截圖等等。
在 WebdriverIO 中,我們不會執行單個測試。我們需要在測試執行器的幫助下觸發配置檔案。測試執行器掃描配置檔案中提供的資訊,然後相應地觸發測試。
要獲取測試執行器,我們必須安裝 WebdriverIO CLI 依賴項。要安裝它並將其儲存到 package.json 檔案中,我們必須執行以下命令:
npm i --save-dev @wdio/cli
成功執行此命令後,CLI 依賴項的版本將反映在 package.json 檔案中。您的計算機上將顯示以下螢幕:

要建立配置檔案,我們必須執行以下命令:
npx wdio config -y
成功執行此命令後,名為 wdio.conf.js 的配置檔案將在我們的專案中建立。此外,package.json 檔案現在應該在 devDependencies 欄位下包含更多依賴項。
您的計算機上將顯示以下螢幕:

除了上圖中標記的依賴項之外,我們還必須新增一個依賴項,以便 WebdriverIO 命令可以同步執行。
我們必須在 devDependencies 欄位下新增依賴項 - “@wdio/sync”: “<版本號>” 。然後執行以下命令:
npm install
要從測試執行器執行配置檔案,我們必須執行以下命令:
npx wdio run wdio.conf.js
建立 Mocha Spec 檔案
建立配置檔案後,我們將在 WebdriverIO 專案中找到一個生成的測試資料夾。有關如何建立配置檔案的詳細資訊在名為“配置檔案生成”的章節中進行了描述。
您的計算機上將顯示以下螢幕:

如果我們展開此資料夾,我們將找到兩個子資料夾 - pageobjects 和 specs,其中包含預設建立的 JavaScript 檔案。這些基本上是提供的示例測試,以指導第一次使用者習慣 Mocha 框架。
Mocha 是一個基於 JavaScript 的測試框架,它構建在 Nodejs 之上。它使非同步測試執行流程變得有趣且簡單。Mocha 測試可以序列執行。
它能夠生成準確且可自定義的報告。此外,未捕獲的異常可以輕鬆地與正確的測試用例進行標記。Mocha 的詳細資訊可以在以下連結中找到:
www.tutorialspoint.com/tesults/tesults_integrating_your_automated_tests.htm
根據 Mocha 測試框架,所有測試檔案都稱為 spec 檔案,並且它們應該位於 specs 資料夾中。
測試檔案中的塊
測試檔案應包含以下塊:
describe - 它在層次結構上高於 it 塊。測試檔案可以有多個 describe 塊。describe 塊表示一個測試套件。它有兩個引數 - 測試套件的描述和一個匿名函式。
it - 它在層次結構上低於 describe 塊。describe 可以有多個 it 塊。it 塊表示一個測試用例,並且在 describe 塊中應該是必須的。它有兩個引數 - 測試用例的描述和一個匿名函式。實際的 WebdriverIO 程式碼在 it 塊中實現。建立 Mocha 檔案的步驟
要建立 Mocha 檔案,讓我們按照以下步驟操作:
步驟 1 - 右鍵單擊 specs 資料夾(位於 test 資料夾中),然後選擇新建檔案。您的計算機上將顯示以下螢幕:

步驟 2 - 輸入檔名,例如 testcase1.js。
您的計算機上將顯示以下螢幕:

步驟 3 - 在此檔案中新增以下程式碼:
// test suite name describe('Tutorialspoint Application', function () { // test case name it('Get Page Title', function (){ // URL launching browser.url("https://tutorialspoint.tw/about/about_careers.htm") //print page title in console console.log(browser.getTitle()) }); });
在上面的程式碼中,瀏覽器是由 WebdriverIO 公開的全域性物件。
請注意 - 我們不能直接執行此單個檔案。我們將藉助配置檔案來執行它。