
- 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 - 處理IFrames
- 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 由六個元件組成,分別是路由器 (Router)、分發器 (Distributor)、節點 (Nodes)、會話佇列 (Session Queue)、會話對映 (Session Map) 和事件匯流排 (Event Bus),這使得它能夠以多種方式部署。根據需求,我們可以以分散式模式分別啟動Selenium Grid 的每個元件,以集線器和節點模式將它們分別作為集線器和節點,或者以獨立模式一次性啟動所有元件。
Selenium Grid 中的獨立模式
在獨立模式下,Selenium Grid 的每個元件作為一個單元工作,可以使用單個命令和單個程序觸發。這是執行 Selenium Grid 的最便捷方式。伺服器透過 **https://:4444** 連線到 RemoteWebdriver 請求,並自動從系統路徑中找到所有驅動程式。在獨立模式下觸發 Selenium Grid 後,所有測試都應使用 **https://:4444**。
Selenium Grid 中獨立模式的優點
Selenium Grid 中獨立模式的優點如下:
建立和除錯使用 RemoteWebdriver 本地構建的測試。
在推送程式碼之前快速執行和單元測試。
可以輕鬆配置 CI/CD 工具。
Selenium Grid 中的集線器和節點模式
在 Selenium Grid 中,集線器包含路由器、會話對映、會話佇列和事件匯流排元件。伺服器會自動透過 **https://:4444** 連線到 RemoteWebdriver 請求。
節點在啟動時從系統路徑中找到所有驅動程式。多個節點可以在同一臺機器上執行。例如,如果同一臺機器上有兩個節點 A 和 B,則使用以下命令:
對於節點 A:
java -jar selenium-server-<version>.jar node --port 5555
對於節點 B:
java -jar selenium-server-<version>.jar node --port 6666
集線器和節點可以位於不同的機器上,它們透過 HTTP 和事件匯流排進行通訊。節點使用事件匯流排向集線器傳送註冊訊息。集線器收到訊息後,會使用 HTTP 與節點通訊以檢查其是否存在。
為了正確地將節點註冊到集線器,集線器機器和節點上需要有事件匯流排埠可用(預設埠:4442 和 4443)。這有助於建立集線器和節點之間的連線。如果集線器使用預設埠,則使用 --hub 標誌註冊節點,如下面的命令所示。
java -jar selenium-server-<version>.jar node --hub http://<hub-ip>:4444
如果集線器不使用預設埠,則使用 --publish-events 和 --subscribe-events 標誌,如下面的命令所示。
java -jar selenium-server-<version>.jar hub --publish-events tcp://<hub-ip>:8886 --subscribe-events tcp://<hub-ip>:8887 --port 8888
在上面的示例中,集線器使用埠 8886、8887 和 8888。註冊集線器後,節點使用相同的埠進行成功註冊,使用與集線器相同的埠,如下面的命令所示。
java -jar selenium-server-<version>.jar node --publish-events tcp://<hub-ip>:8886 --subscribe-events tcp://<hub-ip>:8887
Selenium Grid 中集線器和節點模式的優點
在 Selenium Grid 中,集線器和節點模式是最常用的模式。Selenium Grid 中集線器和節點模式的優點如下:
集線器和節點模式允許在 Selenium Grid 中整合具有不同平臺、瀏覽器及其版本的各種機器。
集線器和節點模式透過指向一個入口來執行 WebDriver 測試在各種環境中。
測試可以輕鬆地進行擴充套件和縮減,而不會影響整個 Grid。
Selenium Grid 中的分散式模式
在分散式模式下,Selenium Grid 的每個元件都應理想地位於不同的機器上,並且需要分別啟動它們。必須正確使用每個端口才能在所有元件之間進行正確的互動。
首先,事件匯流排允許 Grid 元件之間進行通訊。事件匯流排的預設埠是 4442、4443 和 5557。
java -jar selenium-server-<version>.jar event-bus --publish-events tcp://<event-bus-ip>:4442 --subscribe-events tcp://<event-bus-ip>:4443 --port 5557
然後,新的會話佇列將新會話新增到佇列中,該佇列將由分發器獲取。新會話佇列的預設埠是 5559。
java -jar selenium-server-<version>.jar sessionqueue --port 5559
會話對映將會話 ID 連線到執行會話的節點,並與事件匯流排進行通訊。新會話佇列的預設埠是 5556。
java -jar selenium-server-<version>.jar sessions --publish-events tcp://<event-bus-ip>:4442 --subscribe-events tcp://<event-bus-ip>:4443 --port 5556
分發器獲取新會話佇列以獲取對節點的新會話請求,前提是它們的 capabilities 相同。節點以與在集線器和節點模式下注冊節點相同的方式註冊到分發器。分發器的預設埠是 5553。分發器與新會話佇列、會話對映、事件匯流排和節點進行通訊。
java -jar selenium-server-<version>.jar distributor --publish-events tcp://<event-bus-ip>:4442 --subscribe-events tcp://<event-bus-ip>:4443 --sessions http://<sessions-ip>:5556 --sessionqueue http://<new-session-queue-ip>:5559 --port 5553 --bind-bus false
路由器將新的會話請求更改為佇列,並將正在執行的會話請求轉發到正在執行該會話的節點。路由器的預設埠是 4444。路由器與新會話佇列、會話對映和分發器進行通訊。
java -jar selenium-server-<version>.jar router --sessions http://<sessions-ip>:5556 --distributor http://<distributor-ip>:5553 --sessionqueue http://<new-session-queue-ip>:5559 --port 4444
節點的預設埠是 5555。
java -jar selenium-server-<version>.jar node --publish-events tcp://<event-bus-ip>:4442 --subscribe-events tcp://<event-bus-ip>:4443
Selenium Grid 分散式模式的優點
當需要在一個大型 Grid 中(只有一個集線器和許多分佈在多臺機器上的節點)設定大量節點時,使用 Selenium Grid 的分散式模式。在這種情況下,集線器和節點模式不是理想的選擇。
建立測試指令碼的先決條件
步驟 1 − 在系統中安裝 Java(8 以上版本),並使用命令:java -version 檢查是否已安裝。如果安裝成功,將顯示已安裝的 Java 版本。
步驟 2 − 開啟瀏覽器並輸入以下地址,檢查 Grid 狀態:
對於 UI 版本,輸入 https://:4444。
對於非 UI 版本,輸入 https://:4444/status。
兩種情況下,我們都會收到錯誤提示 - 無法訪問此網站。因為 Selenium Grid 尚未啟動。

在 Selenium Grid 中建立測試指令碼
在獨立模式下建立測試指令碼的步驟如下:
步驟 1 − 從以下連結下載 Selenium 獨立 Jar 包,並將其儲存到一個資料夾中:
https://github.com/SeleniumHQ/selenium/releases.
步驟 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(); } }
TestTwo.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 - 建立測試指令碼。我們從介紹 Selenium Grid 中的獨立模式、Hub 和 Node 以及分散式模式及其優勢開始,講解了建立測試指令碼的先決條件以及如何在 Selenium Grid 中建立測試指令碼。
這將使您深入瞭解 Selenium Grid - 建立測試指令碼。建議您多練習所學內容,並探索其他與 Selenium 相關的知識,以加深您的理解並拓寬視野。