
- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境設定
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - 操作命令
- Selenium - 訪問器命令
- Selenium - 斷言命令
- 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 Grid - 自定義節點
Selenium Grid 4 的最新版本是在沒有利用 Selenium Grid 舊版本程式碼庫的情況下開發的。此最新版本的 Selenium Grid 4 版本具有一些高階功能,例如自定義節點。
最新版本的 Selenium Grid 允許在三種不同的 Selenium Grid 模式下觸發測試執行。它們被稱為獨立模式、集線器和節點模式以及分散式模式。
Selenium Grid 中的節點自定義
在使用 Selenium Grid 在不同瀏覽器及其平臺、裝置上執行並行測試或跨瀏覽器測試時,我們可能需要根據需求自定義節點。
在執行會話開始之前,我們可能需要新增一些先決條件步驟,或者在會話完成後執行任何整理活動。要執行節點自定義,需要執行以下步驟:
設計一個類,該類將擴充套件 -
org.openqa.selenium.grid.node.Node.
在新建的類中追加一個靜態方法(工廠方法),該方法應具有以下簽名:public static Node created(Config config)。此處,Node 與 org.openqa.selenium.grid.node.Node 型別相同,Config 與 - 型別相同。
org.openqa.selenium.grid.config.Config.
在工廠方法內部,應新增新類的實現邏輯。
要將新開發的實現合併到集線器中,請啟動節點並將前一個的完全限定類名傳送到引數:–node-implementation。
使用 Uber Jar 進行節點自定義
使用 uber jar 實現節點自定義的步驟如下所列:
在系統中安裝 Java(版本高於 8),並使用命令檢查它是否存在:java -version。如果安裝已成功完成,則將顯示已安裝的 Java 版本。
在系統中安裝 Maven,並使用命令檢查它是否存在:mvn -version。如果安裝已成功完成,則將顯示已安裝的 Maven 版本。
安裝任何 IDE,如 Eclipse、IntelliJ 等。
從連結將 Selenium Grid 依賴項新增到 pom.xml 中:
從連結將 Apache Maven Shade Plugin 新增到 pom.xml 中:
將自定義節點附加到當前專案。
構建 uber jar 以使用命令啟動節點:java -jar。
使用以下命令啟動節點:
java -jar custom_node-server.jar node \ --node-implementation org.seleniumhq.samples.DecoratedLoggingNode
使用普通 Jar 進行節點自定義
使用普通 jar 實現節點自定義的步驟如下所列:
在系統中安裝 Java(版本高於 8),並使用命令檢查它是否存在:java -version。如果安裝已成功完成,則將顯示已安裝的 Java 版本。
在系統中安裝 Maven,並使用命令檢查它是否存在:mvn -version。如果安裝已成功完成,則將顯示已安裝的 Maven 版本。
安裝任何 IDE,如 Eclipse、IntelliJ 等。
從連結將 Selenium Grid 依賴項新增到 pom.xml 中:https://mvnrepository.com/artifact/。
從連結將 Apache Maven Shade Plugin 新增到 pom.xml 中:https://maven.apache.org/plugins/。
將自定義節點附加到當前專案。
構建 uber jar 以使用命令啟動節點:java -jar。
使用以下命令啟動節點:
java -jar selenium-server-4.6.0.jar \ --ext custom_node-1.0-SNAPSHOT.jar node \ --node-implementation org.seleniumhq.samples.DecoratedLoggingNode
DecoratedLoggingNode.java 中的程式碼實現
package org.seleniumhq.samples; import java.io.IOException; import java.net.URI; import java.util.UUID; import java.util.function.Supplier; import org.openqa.selenium.Capabilities; import org.openqa.selenium.NoSuchSessionException; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.grid.config.Config; import org.openqa.selenium.grid.data.CreateSessionRequest; import org.openqa.selenium.grid.data.CreateSessionResponse; import org.openqa.selenium.grid.data.NodeId; import org.openqa.selenium.grid.data.NodeStatus; import org.openqa.selenium.grid.data.Session; import org.openqa.selenium.grid.log.LoggingOptions; import org.openqa.selenium.grid.node.HealthCheck; import org.openqa.selenium.grid.node.Node; import org.openqa.selenium.grid.node.local.LocalNodeFactory; import org.openqa.selenium.grid.security.Secret; import org.openqa.selenium.grid.security.SecretOptions; import org.openqa.selenium.grid.server.BaseServerOptions; import org.openqa.selenium.internal.Either; import org.openqa.selenium.io.TemporaryFilesystem; import org.openqa.selenium.remote.SessionId; import org.openqa.selenium.remote.http.HttpRequest; import org.openqa.selenium.remote.http.HttpResponse; import org.openqa.selenium.remote.tracing.Tracer; public class DecoratedLoggingNode extends Node { private Node node; protected DecoratedLoggingNode(Tracer tracer, NodeId nodeId, URI uri, Secret registrationSecret) { super(tracer, nodeId, uri, registrationSecret); } public static Node create(Config config) { LoggingOptions loggingOptions = new LoggingOptions(config); BaseServerOptions serverOptions = new BaseServerOptions(config); URI uri = serverOptions.getExternalUri(); SecretOptions secretOptions = new SecretOptions(config); // Refer to the foot notes for additional context on this line. Node node = LocalNodeFactory.create(config); DecoratedLoggingNode wrapper = new DecoratedLoggingNode(loggingOptions.getTracer(), node.getId(), uri, secretOptions.getRegistrationSecret()); wrapper.node = node; return wrapper; } @Override public Either<WebDriverException, CreateSessionResponse> newSession( CreateSessionRequest sessionRequest) { return perform(() -> node.newSession(sessionRequest), "newSession"); } @Override public HttpResponse executeWebDriverCommand(HttpRequest req) { return perform(() -> node.executeWebDriverCommand(req), "executeWebDriverCommand"); } @Override public Session getSession(SessionId id) throws NoSuchSessionException { return perform(() -> node.getSession(id), "getSession"); } @Override public HttpResponse uploadFile(HttpRequest req, SessionId id) { return perform(() -> node.uploadFile(req, id), "uploadFile"); } @Override public HttpResponse downloadFile(HttpRequest req, SessionId id) { return perform(() -> node.downloadFile(req, id), "downloadFile"); } @Override public TemporaryFilesystem getDownloadsFilesystem(UUID uuid) { return perform(() -> { try { return node.getDownloadsFilesystem(uuid); } catch (IOException e) { throw new RuntimeException(e); } }, "downloadsFilesystem"); } @Override public TemporaryFilesystem getUploadsFilesystem(SessionId id) throws IOException { return perform(() -> { try { return node.getUploadsFilesystem(id); } catch (IOException e) { throw new RuntimeException(e); } }, "uploadsFilesystem"); } @Override public void stop(SessionId id) throws NoSuchSessionException { perform(() -> node.stop(id), "stop"); } @Override public boolean isSessionOwner(SessionId id) { return perform(() -> node.isSessionOwner(id), "isSessionOwner"); } @Override public boolean isSupporting(Capabilities capabilities) { return perform(() -> node.isSupporting(capabilities), "isSupporting"); } @Override public NodeStatus getStatus() { return perform(() -> node.getStatus(), "getStatus"); } @Override public HealthCheck getHealthCheck() { return perform(() -> node.getHealthCheck(), "getHealthCheck"); } @Override public void drain() { perform(() -> node.drain(), "drain"); } @Override public boolean isReady() { return perform(() -> node.isReady(), "isReady"); } private void perform(Runnable function, String operation) { try { System.err.printf("[COMMENTATOR] Before %s()%n", operation); function.run(); } finally { System.err.printf("[COMMENTATOR] After %s()%n", operation); } } private <T> T perform(Supplier<T> function, String operation) { try { System.err.printf("[COMMENTATOR] Before %s()%n", operation); return function.get(); } finally { System.err.printf("[COMMENTATOR] After %s()%n", operation); } } }
DecoratedLoggingNode.java 的原始碼:https://www.selenium.dev/documentation/。
在上述實現中,以下程式碼:
Node node = LocalNodeFactory.create(config) 用於專門生成 LocalNode。org.openqa.selenium.grid.node.Node 有兩種面向使用者的實現。這些是建立自定義節點和收集有關節點資訊的絕佳起點。
org.openqa.selenium.grid.node.local.LocalNode - 用於指向長時間執行的節點,並且是啟動節點時獲取的預設邏輯。可以透過呼叫 LocalNodeFactory.create(config) 生成它。此處,LocalNodeFactory 是 org.openqa.selenium.grid.node.local 的一部分,Config 是 org.openqa.selenium.grid.config 的一部分。
org.openqa.selenium.grid.node.k8s.OneShotNode - 這是一個重要的參考邏輯,其中節點在一次測試會話後正確關閉自身。此類對任何預先存在的 Maven 工件不可用。可以透過呼叫 OneShotNode.create(config) 生成它。此處,OneShotNode 是 org.openqa.selenium.grid.node.k8s 的一部分,Config 是 org.openqa.selenium.grid.config 的一部分。
這結束了我們關於 Selenium Grid - 自定義節點教程的全面介紹。我們從描述如何在 Selenium Grid 中執行節點自定義、使用 uber jar 進行節點自定義以及在 Selenium Grid 中使用普通 jar 進行節點自定義開始。
這使您深入瞭解了 Selenium Grid 自定義節點。明智的做法是不斷練習您學到的知識並探索與 Selenium 相關的其他知識,以加深您的理解並拓寬您的視野。