
- 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 Grid - 配置
最新版本的 Selenium Grid 與舊版本 Selenium Grid 有很多不同之處。舊版本的 Selenium Grid 只有 Hub 和 Node 兩種模式,而最新版本的 Selenium Grid 支援三種模式:獨立模式、Hub 和 Node 模式以及分散式模式。
以前的 Selenium Grid 版本只有兩種模式:Hub 和 Node,而最新版本的 Selenium Grid 有六個元件,如路由器、分發器、節點、會話佇列、會話對映和事件匯流排。
配置 Selenium Grid 的先決條件
步驟 1 - 在系統中安裝 Java(版本高於 8)並使用以下命令檢查它是否存在:java -version。如果安裝已成功完成,則將顯示安裝的 Java 版本。
步驟 2 - 透過開啟瀏覽器並輸入以下內容來檢查 Grid 狀態 -
對於 UI 版本,鍵入https://:4444。
對於非 UI 版本,鍵入https://:4444/status。
在這兩種情況下,我們都會收到錯誤 - 無法訪問此站點。因為 Selenium Grid 尚未啟動。

Selenium Grid 中的獨立模式配置
在最新版本的 Selenium Grid 中,無需分別啟動 Hub 和 Node。因此,可以在同一臺機器上同時觸發 Hub 和 Node。使用單個獨立命令,Hub 和 Node 將同時啟動。因此,獨立模式有助於節省資源和時間。
配置獨立模式的步驟如下所列 -
步驟 1 - 從以下連結下載 Selenium 獨立 Jar 並將其儲存在資料夾中 -
https://github.com/SeleniumHQ/.
步驟 2 - 從 Selenium 獨立 Jar 所儲存的資料夾位置,從終端執行以下命令 -
java -jar selenium-server-<version>.jar standalone.
步驟 3 - 透過開啟瀏覽器並輸入https://:4444再次檢查 Grid 狀態。
錯誤 - 無法訪問此站點將不再存在,我們將獲得顯示不同瀏覽器的 Grid 狀態。這將證明 Selenium Grid 已在獨立模式下觸發。

步驟 4 -
Base.java 中的程式碼實現
package BaseClass; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; public class Base { public WebDriver setBrowser(String browserName) throws MalformedURLException { WebDriver driver = null; DesiredCapabilities dc = new DesiredCapabilities(); if(browserName.equalsIgnoreCase("chrome")) { dc.setBrowserName("chrome"); } else if(browserName.equalsIgnoreCase("edge")) { dc.setBrowserName("MicrosoftEdge"); } // Initiate RemoteWebDriver driver = new RemoteWebDriver(new URL("https://:4444"),dc); return driver; } }
TestOne.java 中的程式碼實現
package Grid; import BaseClass.Base; import org.openqa.selenium.WebDriver; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.net.MalformedURLException; public class TestOne extends Base { public WebDriver driver = null; @Test public void testOne() { // launch application driver.get("https://tutorialspoint.tw/selenium/practice/links.php"); // get page title System.out.println("Page title is: " + driver.getTitle() + " obtained from testOne"); } @BeforeMethod public void setup() throws MalformedURLException { driver = setBrowser("chrome"); } @AfterMethod public void tearDown() { // quitting browser driver.quit(); } }
TestOne.java 中的程式碼實現
package Grid; import BaseClass.Base; import org.openqa.selenium.WebDriver; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.net.MalformedURLException; public class TestTwo extends Base { public WebDriver driver = null; @Test public void testTwo() { // launch application driver.get("https://tutorialspoint.tw/selenium/practice/links.php"); // get page title System.out.println("Page title is: " + driver.getTitle() + " obtained from testTwo"); } @BeforeMethod public void setup() throws MalformedURLException { driver = setBrowser("edge"); } @AfterMethod public void tearDown() { // quitting browser driver.quit(); } }
testng.xml 檔案中的配置。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name = "Grid Test"> <test thread-count = "5" name="Test"> <classes> <class name="Grid.TestOne" /> <class name="Grid.TestTwo"/> </classes> </test> </suite>
步驟 5 - 從 testng.xml 檔案執行測試。
它將顯示以下輸出 -
Page title is: Selenium Practice - Links obtained from testOne Page title is: Selenium Practice - Links obtained from testTwo =============================================== Grid Test Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 =============================================== Process finished with exit code 0
在上面的示例中,我們配置了 Selenium Grid 的獨立模式。
Selenium Grid 中的 Hub 和 Node 模式配置
在最新版本的 Selenium Grid 中,Hub 和 Node 模式需要在單獨的機器上觸發 Hub 和 Node。Hub 包含路由器、分發器、會話對映、新會話佇列和事件匯流排等元件。
配置 Hub 和 Node 模式的步驟如下所列 -
步驟 1 - 從以下連結下載 Selenium 獨立 Jar 並將其儲存在資料夾中 -
https://github.com/SeleniumHQ/selenium/releases.
步驟 2 - 從 Selenium 獨立 Jar 所儲存的資料夾位置,從終端執行以下命令 -
java -jar selenium-server-<version>.jar hub.
請注意 - 註冊 Hub 時在命令列日誌中收到的 Hub IP 地址。
步驟 3 - 透過開啟瀏覽器並輸入https://:4444檢查 Grid 狀態。
錯誤 - 無法訪問此站點將不再存在,我們將獲得顯示訊息 - Grid 尚未註冊任何節點的狀態。這是因為節點尚未註冊。

在同一臺機器上執行節點
按照上述步驟 1、2 和 3 配置 Hub 後,導航到 Selenium 獨立 Jar 所儲存的資料夾位置,並透過開啟另一個新的終端視窗執行以下命令 -
java -jar selenium-server-<version>.jar node.
這將有助於在同一臺機器上啟動節點。
透過開啟瀏覽器並輸入https://:4444再次檢查 Grid 狀態。
顯示的訊息 - Grid 尚未註冊任何節點將不存在,取而代之的是,節點將反映出來。

在不同的機器上執行節點
步驟 1 - 在另一臺機器上從以下連結下載 Selenium 獨立 Jar 並將其儲存在資料夾中 -
https://github.com/SeleniumHQ/selenium/releases.
步驟 2 - 導航到 Selenium 獨立 Jar 所儲存的資料夾位置,並透過開啟終端視窗執行以下命令 -
java -jar selenium-server-<version>.jar node --detect-drivers true --publish-events tcp://{hub IP Address}:4442 --subscribe-events tcp://{hub IP Address}:4443.
請注意 - Hub IP 地址將在步驟 3 中獲得的註冊 Hub 時收到的命令列日誌中提供。
這將有助於在不同的機器上啟動節點。
步驟 3 - 透過開啟瀏覽器並輸入https://:4444再次檢查 Grid 狀態。
顯示的訊息 - Grid 尚未註冊任何節點將不存在,取而代之的是,節點將反映出來。

步驟 4 - 執行為獨立模式編寫的相同測試,該測試來自 testng.xml 檔案。
它將顯示以下輸出 -
Page title is: Selenium Practice - Links obtained from testOne Page title is: Selenium Practice - Links obtained from testTwo =============================================== Grid Test Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 =============================================== Process finished with exit code 0
在上面的示例中,我們配置了 Selenium Grid 的 Hub 和 Node 模式。
Selenium Grid 中的分散式配置
當需要設定大量節點以構建大型 Grid 時,使用 Selenium Grid 的分散式模式,其中只有一個 Hub 和許多分佈在多臺機器上的節點。在這種情況下,Hub 和 Node 模式不是理想的選擇。
此外,在分散式模式下,每個元件(路由器、分發器、會話對映、新會話佇列和事件匯流排)都可以使用不同的命令單獨啟動。
配置分散式模式的步驟如下所列 -
步驟 1 - 從以下連結下載 Selenium 獨立 Jar 並將其儲存在資料夾中 -
https://github.com/SeleniumHQ/selenium/releases
步驟 2 - 從 Selenium 獨立 Jar 所儲存的資料夾位置,從終端執行以下命令以啟動事件匯流排 -
java -jar selenium-server-<version>.jar event-bus.
請注意在終端的命令列日誌中事件匯流排啟動時的IP地址。
步驟 3 − 在按照步驟 2 配置事件匯流排後,導航到儲存 Selenium 獨立 Jar 的資料夾位置,並透過開啟另一個新的終端視窗執行以下命令以啟動 Session Map −
java -jar selenium-server-<version>.jar sessions.
請注意在終端的命令列日誌中 Session Map 啟動時的IP地址。
步驟 4 − 在按照步驟 3 配置 Session Map 後,導航到儲存 Selenium 獨立 Jar 的資料夾位置,並透過開啟另一個新的終端視窗執行以下命令以啟動 Session Queue −
java -jar selenium-server-<version>.jar sessionqueue.
請注意在終端的命令列日誌中 Session Queue 啟動時的IP地址。
步驟 5 − 在按照步驟 4 配置 Session Queue 後,導航到儲存 Selenium 獨立 Jar 的資料夾位置,並透過開啟另一個新的終端視窗執行以下命令以啟動 Distributor −
java -jar selenium-server-<version>.jar distributor --sessions http://{IP Address of Session Map}:5556 --sessionqueue http://{IP Address of Session Queue}:5559 --bind-bus false.
請注意在終端的命令列日誌中 Distributor 啟動時的IP地址。
步驟 6 − 在按照步驟 5 配置 Distributor 後,導航到儲存 Selenium 獨立 Jar 的資料夾位置,並透過開啟另一個新的終端視窗執行以下命令以啟動 Router −
java -jar selenium-server-<version>.jar router --sessions http://{IP Address of Session Map}:5556 --distributor http://{IP Address of Distributor}:5553 --sessionqueue http://{IP Address of Session Queue}:5559.
請注意在終端的命令列日誌中 Router 啟動時的IP地址。
在按照步驟 6 配置 Router 後,導航到儲存 Selenium 獨立 Jar 的資料夾位置,並透過開啟另一個新的終端視窗執行以下命令 −
java -jar selenium-server-<version>.jar node.
這將有助於在同一臺機器上啟動節點。
在不同的機器上執行節點
在按照步驟 6 配置 Router 後,導航到儲存 Selenium 獨立 Jar 的資料夾位置,並透過開啟一個新的終端視窗執行以下命令 −
java -jar selenium-server-<version>.jar node --detect-drivers true --publish-events tcp://{hub IP Address}:4442 --subscribe-events tcp://{hub IP Address}:4443.
我們在 Hub 和 Node 配置期間註冊 Hub 時收到的 Hub IP 地址。
這將有助於在不同的機器上啟動節點。
這總結了我們關於 Selenium Grid - 配置教程的全面概述。我們從描述配置 Selenium Grid 的先決條件開始,並逐步介紹了 Selenium Grid 中獨立、Hub 和 Node 以及分散式模式配置的步驟。
這使您具備了 Selenium Grid 配置的深入知識。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並擴充套件您的視野。