
- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境設定
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- Selenium - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼輸出
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 簡介
- Selenium WebDriver vs RC
- Selenium - 安裝
- Selenium - 第一個測試指令碼
- Selenium - 驅動程式會話
- Selenium - 瀏覽器選項
- Selenium - Chrome 選項
- Selenium - Edge 選項
- Selenium - Firefox 選項
- Selenium - Safari 選項
- Selenium - 雙擊
- Selenium - 右鍵單擊
- Python 中的 HTML 報告
- 處理編輯框
- Selenium - 單個元素
- Selenium - 多個元素
- Selenium Web 元素
- Selenium - 檔案上傳
- Selenium - 定位器策略
- Selenium - 相對定位器
- Selenium - 查詢器
- Selenium - 查詢所有連結
- Selenium - 使用者互動
- Selenium - WebElement 命令
- Selenium - 瀏覽器互動
- Selenium - 瀏覽器命令
- Selenium - 瀏覽器導航
- Selenium - 警報和彈出視窗
- Selenium - 處理表單
- Selenium - 視窗和標籤頁
- Selenium - 處理連結
- Selenium - 輸入框
- Selenium - 單選按鈕
- Selenium - 複選框
- Selenium - 下拉框
- Selenium - 處理 IFrame
- Selenium - 處理 Cookie
- Selenium - 日期時間選擇器
- Selenium - 動態 Web 表格
- Selenium - Actions 類
- Selenium - Action 類
- Selenium - 鍵盤事件
- Selenium - 鍵上/下
- Selenium - 複製和貼上
- Selenium - 處理特殊鍵
- Selenium - 滑鼠事件
- Selenium - 拖放
- Selenium - 筆事件
- Selenium - 滾動操作
- Selenium - 等待策略
- Selenium - 顯式/隱式等待
- Selenium - 支援特性
- Selenium - 多選
- Selenium - 等待支援
- Selenium - 選擇支援
- Selenium - 顏色支援
- Selenium - ThreadGuard
- Selenium - 錯誤和日誌記錄
- Selenium - 異常處理
- Selenium - 其他
- Selenium - 處理 Ajax 呼叫
- Selenium - JSON 資料檔案
- Selenium - CSV 資料檔案
- Selenium - Excel 資料檔案
- Selenium - 跨瀏覽器測試
- Selenium - 多瀏覽器測試
- Selenium - 多視窗測試
- Selenium - JavaScript 執行器
- Selenium - 無頭執行
- Selenium - 捕獲螢幕截圖
- Selenium - 捕獲影片
- Selenium - 頁面物件模型
- Selenium - 頁面工廠
- Selenium - 記錄和回放
- Selenium - 框架
- Selenium - 瀏覽上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架構
- Selenium - 元件
- Selenium - 配置
- Selenium - 建立測試指令碼
- Selenium - 測試執行
- Selenium - 端點
- Selenium - 自定義節點
- Selenium 報告工具
- Selenium - 報告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & 其他技術
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven & Jenkins
- Selenium - 資料庫測試
- Selenium - LogExpert 日誌記錄
- Selenium - Log4j 日誌記錄
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 測試
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium 其他概念
- Selenium - IE 驅動程式
- Selenium - 自動化框架
- Selenium - 基於關鍵字的框架
- Selenium - 資料驅動的框架
- Selenium - 混合驅動的框架
- Selenium - SSL 證書錯誤
- Selenium - 替代方案
- Selenium 有用資源
- Selenium - 問答
- Selenium - 快速指南
- Selenium - 有用資源
- Selenium - 自動化實踐
- Selenium - 討論
Selenium IDE - 模式匹配
Selenium IDE 可用於測試的模式匹配,特別是需要驗證場景的測試。與定位器一樣,模式是 Selenium 經常使用的引數型別。它允許使用者藉助特殊字元來描述模式。很多時候,我們想要驗證的文字是動態的,在這種情況下,模式匹配非常有用。
模式匹配通常與所有驗證點命令一起使用,例如 verify text、verify not text、verify title、verify value、verify、assert text、assert alert、assert not text、assert prompt、assert selected value、assert title、assert not selected value、assert selected label 等等。
模式匹配的不同方法
萬用字元
正則表示式
精確匹配
萬用字元
萬用字元用於根據萬用字元(即萬用字元表示式)匹配字串。在 Linux 或 Windows 中搜索特定檔案型別(例如 *.doc、*.jpg 等)時,它通常用於檔案匹配模式。
在 Selenium 中有多種使用萬用字元的方法,例如 * 用於匹配任意數量或順序的字元,?用於匹配唯一字元,以及 [] 用於匹配括號內提供的任意一個字元。它是一個字元類。例如,[0-9] 將匹配任何數字。Selenium 中預設提供使用萬用字元的模式匹配,並且可以選擇在 Selenium IDE 的測試步驟中提及關鍵字 glob。
讓我們以以下頁面為例,我們將使用萬用字元模式匹配驗證文字“Alerts”和標題“Selenium - Automation Practice”。

以下是需要遵循的步驟:
步驟 1 - 單擊安裝後在瀏覽器上可見的 Selenium IDE 擴充套件。
步驟 2 - Selenium IDE 應該啟動,並顯示帶有工具版本的歡迎訊息。在下圖中,安裝的版本為 3.17.2。
此外,它還提供了一些選項,讓我們可以選擇如何使用該工具,例如在新的專案中記錄新的測試、開啟現有專案、建立新專案和關閉 Selenium IDE。
步驟 3 - 單擊“建立新專案”連結,然後輸入專案名稱,例如在“請為您的新專案提供一個名稱”下輸入 Test7。最後,我們將單擊“確定”按鈕。
步驟 4 - 我們現在可以在 Selenium IDE 中準備好工作區了。
步驟 5 - 在“命令”欄位中輸入 open,在“目標”欄位中輸入 https://tutorialspoint.tw/selenium/ 以啟動應用程式。
步驟 6 - 在“命令”欄位中輸入 assert text,在“目標”欄位中輸入 xpath=/html/body/main/div/div/div[2]/h1,在“值”欄位中輸入 glob: Alert*。請注意,xpath=/html/body/main/div/div/div[2]/h1 是文字,glob: Alert* 是要匹配的模式值。
步驟 7 - 在“命令”欄位中輸入 assert text,在“目標”欄位中輸入 xpath=/html/body/div/header/div[2]/h1,在“值”欄位中輸入 glob: Selenium - Automation*。請注意,xpath=/html/body/div/header/div[2]/h1 是標題,glob: Selenium - Automation* 是要匹配的模式值。
步驟 8 - 在“命令”欄位中輸入 close 以關閉瀏覽器視窗。

步驟 9 - 單擊左窗格中出現的三個點,然後選擇“重新命名”選項。
步驟 10 - 在“重新命名測試用例”欄位下輸入名稱,例如 Test14,然後單擊“重新命名”。輸入的名稱將顯示在 Selenium IDE 的左側。
步驟 11 - 單擊頂部的“執行所有測試”,然後等待測試執行完成。在我們的示例中,我們將看到執行:1,失敗:0,以及一個綠色條,表示測試成功執行而沒有失敗。此外,訊息“Test14 完成成功”出現在日誌下。
在上面的示例中,我們使用萬用字元驗證了文字。
精確匹配
精確匹配用於按原樣匹配字串。例如,如果需要與字串值進行精確匹配,則可以使用精確模式。在精確匹配中,運算子 * 作為普通字元工作,而不是用於模式匹配的萬用字元。讓我們採用與上面討論的相同的示例,並使用精確模式技術。
以下是需要遵循的步驟:
步驟 1 - 遵循前面示例的步驟 1 到 5。
步驟 2 - 在“命令”欄位中輸入 assert text,在“目標”欄位中輸入 xpath=/html/body/main/div/div/div[2]/h1,在“值”欄位中輸入 exact: Alerts。請注意,xpath=/html/body/main/div/div/div[2]/h1 是文字,exact: Alerts 是要精確匹配的值。
步驟 3 - 在“命令”欄位中輸入 assert text,在“目標”欄位中輸入 xpath=/html/body/div/header/div[2]/h1,在“值”欄位中輸入 exact: Selenium - Automation Practice。請注意,xpath=/html/body/div/header/div[2]/h1 是標題,exact: Selenium - Automation Practice 是要精確匹配的值。
步驟 4 - 遵循前面示例的步驟 8 到 11。
正則表示式
正則表示式是可用模式匹配技術中最有用的。Selenium 支援 Javascript 支援的完整正則表示式模式集。因此,使用者不再受限於 *、?和 [] 萬用字元模式。
要使用 RegEx 模式,我們需要以“regexp:”或“regexpi:”為字首。“regexpi”字首不區分大小寫。glob: 和 exact: 模式是正則表示式模式的子集。使用 glob: 或 exact: 完成的所有操作都可以藉助 RegExp 來完成。
更多關於正則表示式的資訊,請訪問以下連結:
https://tutorialspoint.tw/javascript/javascript_regexp_object.htm
本教程對 Selenium IDE 的模式匹配進行了全面講解。我們首先介紹了不同的模式匹配方法,例如萬用字元、正則表示式和精確匹配,以及如何在 Selenium 中使用它們。
這將使您深入瞭解 Selenium IDE 中的模式匹配。建議您多加練習所學內容,並探索與 Selenium 相關的其他知識,以加深理解並拓寬視野。