
- 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 IDE 中使用 **store** 命令及其相關命令。我們使用 **echo** 命令在 Selenium IDE 的“日誌”選項卡中列印變數的儲存值。我們還可以訪問網頁上的元素並使用 **store Text** 命令獲取其文字,並使用 **store Value** 命令獲取元素的值。
示例
讓我們來看一下下面的頁面示例,我們將在 **First Name:** 標籤旁邊輸入文字 **Selenium**,在 **Last Name:** 標籤旁邊輸入文字 **Tutorialspoint**。此外,我們將使用 Selenium IDE 和其變數儲存功能獲取文字 **Selenium - Automation Practice Form**。

在 Selenium IDE 中儲存和訪問變數
在 Selenium IDE 中儲存和訪問變數的步驟如下所示:
**步驟 1** - 點選安裝後在瀏覽器上顯示的 Selenium IDE 擴充套件。
**步驟 2** - Selenium IDE 應該啟動,並顯示包含工具版本的歡迎訊息。在下圖中,安裝的版本為 3.17.2。
此外,它還提供了一些選項,例如在新專案中錄製新測試、開啟現有專案、建立新專案和關閉 Selenium IDE。
此外,還提供了一個帶有文字“Selenium IDE 專案頁面”的連結,點選該連結後,將跳轉到 Selenium 整合開發環境的文件頁面。
https://www.selenium.dev/selenium-ide/

**步驟 3** - 點選“建立新專案”連結,然後在“請為您的新專案提供一個名稱”下輸入專案名稱。最後,點選“確定”。

**步驟 4** - 我們將在 Selenium IDE 中準備好工作區,如下圖所示。

**步驟 5** - 在“命令”欄位中輸入 open,在“目標”欄位中輸入 https://tutorialspoint.tw/selenium/,以啟動應用程式。

**步驟 6** - 在“命令”欄位中輸入 store,在“目標”欄位中輸入 Selenium,在“值”欄位中輸入 i。請注意,Selenium 是我們想使用變數 i 輸入的值。

**步驟 7** - 在“命令”欄位中輸入 type,在“目標”欄位中輸入 id=firstname,在“值”欄位中輸入 ${i}。請注意,id=firstname 是輸入框的定位器值,${i} 用於引用我們想使用變數 i 輸入的值。
**步驟 8** - 在“命令”欄位中輸入 store,在“目標”欄位中輸入 Tutorialspoint,在“值”欄位中輸入 j。請注意,Tutorialspoint 是我們想使用變數 j 輸入的值。
**步驟 9** - 在“命令”欄位中輸入 type,在“目標”欄位中輸入 id=lastname,在“值”欄位中輸入 ${j}。請注意,id=lastname 是輸入框的定位器值,${j} 用於引用我們想使用變數 j 輸入的值。

**步驟 10** - 在“命令”欄位中輸入 store Text,在“目標”欄位中輸入 css=div > h1,在“值”欄位中輸入 k。請注意,css=div > h1 是我們將獲取的文字的定位器值,k 用於引用我們將儲存獲取文字的變數值。
**步驟 11** - 在“命令”欄位中輸入 echo,在“目標”欄位中輸入 ${k}。請注意,這將在 Selenium IDE 的“日誌”中列印我們在步驟 10 中訪問的網頁文字。

**步驟 12** - 在“命令”欄位中輸入 store value,在“目標”欄位中輸入 id=firstname,在“值”欄位中輸入 l。請注意,id=firstname 是輸入框的定位器值,l 用於引用我們將用來獲取輸入框中輸入值的變數。
**步驟 13** - 在“命令”欄位中輸入 echo,在“目標”欄位中輸入 ${l}。請注意,這將在 Selenium IDE 的“日誌”中列印在步驟 7 中輸入的值。

**步驟 14** - 在“命令”欄位中輸入 close 以關閉瀏覽器視窗。

**步驟 15** - 點選左窗格中顯示的三個點,然後選擇“重新命名”選項。

**步驟 16** - 在“重新命名測試用例”欄位中輸入一個名稱,例如 TestCase2,然後點選“重新命名”。輸入的名稱將顯示在 Selenium IDE 的左側。

**步驟 17** - 點選頂部的“執行所有測試”,然後等待測試執行完成。在我們的示例中,我們將看到“執行:1”、“失敗:0”和一個綠條,表示測試成功執行且沒有任何失敗。此外,“日誌”下還會顯示訊息“TestCase2 成功完成”。

在上例中,我們獲取了網頁上顯示的文字 **Selenium - Automation Practice Form**,並在“日誌”中檢索了在 **First Name:** 標籤旁邊的輸入框中輸入的值 **Selenium**。此外,我們在測試用例名稱 **TestCase2** 旁邊獲得了綠色的勾號,表示測試透過。
除此之外,所有測試步驟以及測試用例名稱都變為綠色,反映所有步驟都 PASS 了。日誌選項卡下反映的所有步驟都顯示為綠色的 OK。
因此,在 Selenium IDE 中,我們將獲得測試結果的步驟級資訊,並且只有在所有步驟都成功執行後,我們才能在測試用例級別獲得 PASS。
此外,我們可以推斷,在一個步驟中使用的變數可以使用語法`${<變數名>}`在後續步驟中訪問。在步驟6、7、8、9、10、11、12和13中,我們在測試中採用了這種方法。此外,我們還使用了store和與store相關的命令,例如store Text、store Value等來儲存變數。echo命令用於在“日誌”選項卡下列印這些值。

因此,在本教程中,我們討論瞭如何使用Selenium IDE儲存變數。