
- 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 - 斷言的 Expect 語句
要將 WebdriverIO 用作自動化測試工具,我們需要設定檢查點,這將幫助我們得出測試是否透過或失敗的結論。WebdriverIO 中提供了各種斷言,我們可以使用它們來驗證測試是否成功驗證了一個步驟。
在斷言中,我們可以將測試的預期結果與實際結果進行比較。如果兩者相同,則測試應透過,否則應失敗。WebdriverIO 中的 expect 語句可以應用於瀏覽器、模擬物件或元素。
我們必須新增一個名為 Chai 的 NodeJS 庫。Chai 庫包含用於斷言的 expect 語句。
我們必須在程式碼中新增以下語句以實現 Chai 斷言:
const e = require('chai').expect
應用於瀏覽器的斷言
這些斷言列在下面:
toHaveUrl
它檢查瀏覽器是否打開了特定頁面。語法如下:
expect(browser).toHaveUrl('https://tutorialspoint.tw/index.htm')
toHaveUrlContaining
它檢查頁面的 URL 是否包含特定值。
語法
語法如下:
expect(browser).toHaveUrlContaining('tutorialspoint')
toHaveUrl
它檢查頁面是否具有特定標題。
語法
語法如下:
expect(browser).toHaveTitle('Terms of Use - Tutorialspoint')
應用於元素的斷言
這些斷言列在下面:
toBeDisplayed
它檢查元素是否顯示。
語法
語法如下:
const e = $('#loc') expect(e).toBeDisplayed()
toExist
它檢查元素是否存在。
語法
語法如下:
const e = $('#loc') expect(e).toExist()
toBePresent
它檢查元素是否存在。
語法
語法如下:
const e = $('#loc') expect(e).toBePresent()
toBeExisting
它與 toExist 相同。
toBeFocussed
它檢查元素是否獲得焦點。
語法
語法如下:
const e = $('#loc') expect(e).toBeFocussed()
toHaveAttribute
它檢查元素屬性是否具有特定值。
語法
語法如下:
const e = $('#loc') expect(e).toHaveAttribute('name', 'search')
toHaveAttr
它與 toExist 相同。
toHaveAttributeContaining
它檢查元素屬性是否包含特定值。
語法
語法如下:
const e = $('#loc') expect(e).toHaveAttributeContaining('name', 'srch')
toHaveElementClass
它檢查元素是否具有特定類名。
語法
語法如下:
const e = $('#loc') expect(e).toHaveElementClass('name', { message: 'Not available!', })
toHaveElementClassContaining
它檢查元素類名是否包含特定值。
語法
語法如下:
const e = $('#loc') expect(e).toHaveElementClassContaining('nam')
toHaveElementProperty
它檢查元素是否具有特定屬性。
語法
語法如下:
const e = $('#loc') expect(e).toHaveElementProperty('width', 15) //verify negative scenario expect(e).not.toHaveElementProperty('width', 20)
toHaveValue
它檢查輸入元素是否具有特定值。
語法
語法如下:
const e = $('#loc') expect(e).toHaveValue('Selenium', { ignoreCase: false})
toHaveValueContaining
它檢查輸入元素是否包含特定值
語法
語法如下:
const e = $('#loc') expect(e).toHaveValueContaining('srch')
toBeClickable
它檢查元素是否可點選。
語法
語法如下:
const e = $('#loc') expect(e).toBeClickable()
toBeDisabled
它檢查元素是否被停用。
語法
語法如下:
const e = $('#loc') expect(e).toBeDisabled() //verify negative scenario expect(e).not.toBeEnabled()
toBeEnabled
它檢查元素是否已啟用。
語法
語法如下:
const e = $('#loc') expect(e).toBeEnabled()
toBeSelected
它與 toBeEnabled 相同。
toBeChecked
它與 toBeEnabled 相同。
toHaveHref
它檢查連結元素是否具有特定連結目標。
語法
語法如下:
const e = $('<a>') expect(e).toHaveHref('https://tutorialspoint.tw/index.htm')
toHaveLink
它與 toHaveHref 相同。
toHaveHrefContaining
它檢查連結元素是否包含特定連結目標。
語法
語法如下:
const e = $('<a>') expect(e).toHaveHrefContaining('tutorialspoint.com')
toHaveLinkContaining
它與 HaveHrefContaining 相同。
toHaveId
它檢查元素是否具有特定 id 屬性值。
語法
語法如下:
const e = $('#loc') expect(e).toHaveId('loc')
toHaveText
它檢查元素是否具有特定文字。
語法
語法如下:
const e = $('#loc') expect(e).toHaveText('Learning WebdriverIO')
toHaveTextContaining
它檢查元素是否包含特定文字。
語法
語法如下:
const e = $('#loc') expect(e).toHaveTextContaining('Learning WebdriverIO')
toBeDisplayedInViewpoint
它檢查元素是否在視口中。
語法
語法如下:
const e = $('#loc') expect(e).toBeDisplayedInViewpoint()
應用於模擬物件的斷言
這些斷言列在下面:
toBeRequested
它檢查模擬是否被呼叫。
語法
語法如下:
const m = browser.mock('**/api/list*') expect(m).toBeRequested()
toBeRequestedTimes
它檢查模擬是否被呼叫了預期次數。
語法
語法如下:
const m = browser.mock('**/api/list*') expect(m).toBeRequestedTimes(2)
首先,請按照“使用 WebdriverIO 的成功路徑流程”一章中步驟 1 到 5 的操作,步驟如下:
步驟 1 - 安裝 NodeJS。有關如何執行此安裝的詳細資訊,請參閱“使用 NodeJS 入門”一章。
步驟 2 - 安裝 NPM。有關如何執行此安裝的詳細資訊,請參閱“NPM 的安裝”一章。
步驟 3 - 安裝 VS Code。有關如何執行此安裝的詳細資訊,請參閱“VS Code 安裝”一章。
步驟 4 - 建立配置檔案。有關如何執行此安裝的詳細資訊,請參閱“配置檔案生成”一章。
步驟 5 - 建立一個規範檔案。有關如何執行此安裝的詳細資訊,請參閱“Mocha 安裝”一章。
步驟 6 - 在建立的 Mocha 規範檔案中新增以下程式碼。
// test suite name describe('Tutorialspoint application', function(){ //test case it('Assertion with expect', function(){ // launch url browser.url('https://tutorialspoint.tw/about/about_careers.htm') //identify element with link text then click $("=Terms of Use").click() browser.pause(1000) //verify page title with assertion expect(browser).toHaveTitleContaining('Terms of Use - Tuter') }); });
執行配置檔案 - wdio.conf.js 檔案,使用以下命令:
npx wdio run wdio.conf.js
有關如何建立配置檔案的詳細資訊,請參閱“wdio.conf.js 檔案”和“配置檔案生成”一章。
您的計算機上將顯示以下螢幕:

成功執行命令後,我們發現結果為 1 個失敗。因為預期結果是“使用條款 - Tuter”,而接收到的輸出是“使用條款 - Tutorialspoint”。
此外,WebdriverIO expect 語句突出顯示了預期結果和接收到的文字不匹配的文字部分。