
- 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 - 類名定位器
一旦我們導航到網頁,我們必須與頁面上可用的網頁元素進行互動,例如點選連結/按鈕,在編輯框中輸入文字等,以完成我們的自動化測試用例。
為此,我們的首要任務是識別元素。我們可以使用元素的類名屬性來識別它。它是一個非常有用的定位器,與 xpath 相比,可以加快自動化測試的執行速度。
在 WebdriverIO 程式碼中,我們可以使用以下格式指定元素的類名屬性的值:
$('=.value of class attribute')
或者,我們可以將此表示式儲存在一個變數中,如下所示:
const p = $('=.value of class attribute')
讓我們識別下圖中突出顯示的文字並獲取其文字:

上圖中突出顯示的元素的類屬性值為 heading。
程式碼實現如下:
// test suite name describe('Tutorialspoint application', function(){ //test case it('Identify element with Class Name', function(){ // launch url browser.url('https://tutorialspoint.tw/about/about_careers.htm') //identify element with Class Name then obtain text console.log($(".heading").getText() + " - is the text.") }); });
使用以下命令執行配置檔案 - wdio.conf.js 檔案:
npx wdio run wdio.conf.js
有關如何建立配置檔案的詳細資訊在標題為 wdio.conf.js 檔案和標題為配置檔案生成的章節中進行了詳細討論。您的計算機上將出現以下螢幕:

命令成功執行後,元素的文字 - About Tutorialspoint 將列印到控制檯。
廣告