
- WebdriverIO 教程
- WebdriverIO - 首頁
- WebdriverIO - 簡介
- WebdriverIO - 預備條件
- WebdriverIO - 架構
- WebdriverIO - NodeJS入門
- WebdriverIO - NPM安裝
- WebdriverIO - VS Code安裝
- WebdriverIO - package.json
- WebdriverIO - Mocha安裝
- Selenium 獨立伺服器安裝
- WebdriverIO - 配置檔案生成
- WebdriverIO - VS Code IntelliSense
- WebdriverIO - wdio.conf.js 檔案
- WebdriverIO - XPath 定位器
- WebdriverIO - CSS 定位器
- WebdriverIO - 連結文字定位器
- WebdriverIO - ID 定位器
- WebdriverIO - 標籤名定位器
- WebdriverIO - 類名定位器
- WebdriverIO - 名稱定位器
- 斷言的 Expect 語句
- WebdriverIO - 正常流程
- WebdriverIO - 常用瀏覽器命令
- WebdriverIO - 處理瀏覽器尺寸
- WebdriverIO - 瀏覽器導航命令
- 處理複選框和下拉選單
- WebdriverIO - 滑鼠操作
- 處理子視窗/彈出視窗
- WebdriverIO - 隱藏元素
- WebdriverIO - Frame
- 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 實現資料驅動測試。當需要使用不同的資料組合多次執行相同的測試用例時,需要進行資料驅動測試。在這裡,我們將瞭解如何使用外部 JSON 檔案來儲存資料。
在 WebdriverIO 專案中,所有測試檔案都建立在 specs 資料夾中。specs 資料夾位於 test 資料夾內。我們將建立另一個資料夾,例如 test 資料夾內的 testData。
testData 資料夾將包含以鍵值對形式儲存不同資料集的 JSON 檔案。此外,如果我們在 spec 資料夾中有三個測試檔案,並且想要對所有這些檔案進行資料驅動測試,則需要建立三個 JSON 檔案。
這些 JSON 檔案中的每一個都應專門用於 spec 資料夾中的每個測試檔案。我們將建立一個 JSON 檔案,例如 testData 資料夾中的 test1.json。
現在,在此檔案中新增以下資料:
[ { "email":"test@gmail.com", "password":"12" }, { "email":"test12@gmail.com", "password":"34" } ]
您的計算機上將出現以下螢幕:

我們將解析此 JSON 檔案並將其轉換為字串格式。這是透過新增以下庫來完成的:
const s =require('fs')
然後,為了解析 JSON 檔案,我們將使用 readFileSync 方法並將 JSON 檔案檔案的相對路徑作為引數傳遞給此方法。最後,將其儲存在一個物件中,例如 c。此物件將包含所有資料。
let c = JSON.parse(s.readFileSync('test/testData/test1.json'))
然後,我們將使用迴圈在兩組資料上迭代相同的測試用例。此迴圈必須在程式碼塊之前實現,並且應該傳遞在 JSON 檔案中宣告的資料鍵。
使用上述資料集,我們將驗證 LinkedIn 應用程式的登入頁面。在輸入少於 6 個字元的電子郵件和密碼後單擊“登入”按鈕時,將出現錯誤訊息 - “您提供的密碼必須至少包含 6 個字元”。
您的計算機上將出現以下螢幕:

首先,請按照標題為“使用 WebdriverIO 的正常流程”一章中的步驟 1 到步驟 5 進行操作,步驟如下:
步驟 1 - 安裝 NodeJS。有關如何執行此安裝的詳細資訊,請參閱標題為“NodeJS 入門”的章節。
步驟 2 - 安裝 NPM。有關如何執行此安裝的詳細資訊,請參閱標題為“NPM 安裝”的章節。
步驟 3 - 安裝 VS Code。有關如何執行此安裝的詳細資訊,請參閱標題為“VS Code 安裝”的章節。
步驟 4 - 建立配置檔案。有關如何執行此安裝的詳細資訊,請參閱標題為“配置檔案生成”的章節。
步驟 5 - 建立 spec 檔案。有關如何執行此安裝的詳細資訊,請參閱標題為“Mocha 安裝”的章節。
步驟 6 - 在建立的 Mocha spec 檔案中新增以下程式碼。
//import chai library const c = require('chai').expect //library for parsing JSON file const s =require('fs') let h = JSON.parse(s.readFileSync('test/testData/test1.json')) // test suite name describe('Tutorialspoint application', function(){ //iterate the test case h.forEach( ({email,password}) =>{ //test case it('Data Driven testing', function(){ // launch url browser.url('https://www.linkedin.com/login') //identify the email field then enter key - email $("#username").setValue(email) //identify password field then enter key - password $("#password").setValue(password) //identify Sign in button then click $("button[type='submit']").click() //verify error message const e = $('#error-for-password') console.log(e.getText() + ' - Error Text') //verify Alert text with Chai assertion c(e.getText()).to.equal("The password you provided must have at least 6 characters.") }); }); });
使用以下命令執行配置檔案 - wdio.conf.js 檔案:
npx wdio run wdio.conf.js
有關如何建立配置檔案的詳細資訊,請參閱標題為“wdio.conf.js 檔案”和“配置檔案生成”的章節。
您的計算機上將出現以下螢幕:

成功執行命令後,錯誤文字“您提供的密碼必須至少包含 6 個字元”將在控制檯中列印兩次。
此外,它還顯示訊息“2 透過”,因為在一個程式碼塊中定義的相同測試用例已使用兩組不同的資料執行了兩次。