selenium_grid
Selenium - Grid
Section titled “Selenium - Grid”Selenium Grid 允许您在多台机器、操作系统和浏览器上并行运行您的 WebDriver 测试。这显著加快了测试执行时间,特别是对于大型测试套件,并有助于进行跨浏览器/跨平台测试。
Selenium Grid 4 是最新版本,引入了重新设计的架构,与之前版本相比,它更加健壮、可伸缩且更易于管理。
Selenium Grid 4 架构
Section titled “Selenium Grid 4 架构”Grid 4 由几个协同工作的组件组成:
- 路由器 (Router): 所有来自测试的传入
new session请求的入口点 (RemoteWebDriver)。它将请求定向到适当的组件。 - 分发器 (Distributor): 管理可用节点,并根据请求的能力(浏览器、版本、平台)将新会话请求分配给合适的节点。
- 会话映射 (Session Map): 跟踪活动会话,并将会话 ID 映射到执行会话的节点。
- 节点 (Node): 实际启动浏览器并由 WebDriver 控制的机器(物理机、虚拟机,或者可能是 Docker 容器)。一个 Grid 可以有多个节点。
- 事件总线 (Event Bus): 促进不同组件(路由器、分发器、会话映射、节点)之间的通信。
与 Grid 3 不同,您无需下载单独的 Hub 和 Node JAR 文件。同一个 Selenium Server JAR 文件(selenium-server-X.Y.Z.jar)包含所有组件。Grid 4 还内置了 Docker 支持。
Grid 4 运行模式
Section titled “Grid 4 运行模式”- 独立模式 (Standalone): 最简单的模式。在单个机器的单个进程中运行所有 Grid 组件(路由器、分发器、会话映射、节点)。适用于简单的本地并行执行。自动检测已安装的浏览器/驱动程序。
- 中心模式 (Hub and Node): 传统设置。一台机器运行核心 Grid 组件(路由器、分发器、会话映射、事件总线),作为“Hub”。其他机器仅运行 Node 组件并向 Hub 注册自己。
- 分布式模式 (Distributed): 每个 Grid 组件(路由器、分发器、会话映射、事件总线、节点)都可以作为单独的进程运行,可能在不同的机器上,提供最大的可伸缩性和弹性。
设置 Selenium Grid 4
Section titled “设置 Selenium Grid 4”步骤 1: 从 Selenium 官方网站下载 Selenium Server JAR 文件:https://www.selenium.dev/downloads/
步骤 2: 确保已安装必要的浏览器驱动程序(例如 chromedriver, geckodriver)并将其添加到系统 PATH 中,或者在启动节点/独立服务器时指定它们的位置。
在独立模式下运行
Section titled “在独立模式下运行”打开您的终端或命令提示符并运行:
java -jar selenium-server-X.Y.Z.jar standalone服务器将启动,检测可用的驱动程序/浏览器,并准备好在 http://localhost:4444 接受会话请求。
在中心模式下运行
Section titled “在中心模式下运行”1. 启动 Hub: 在指定为 Hub 的机器上,运行:
java -jar selenium-server-X.Y.Z.jar hubHub 将启动并监听节点的连接(默认端口 4444)。
2. 启动节点: 在每台将运行浏览器的机器上,运行:
# Ensure drivers (e.g., chromedriver) are in PATH or specify path:# java -Dwebdriver.chrome.driver=/path/to/chromedriver -jar selenium-server-X.Y.Z.jar node
java -jar selenium-server-X.Y.Z.jar node --hub http://<HUB_IP_ADDRESS>:4444将 <HUB_IP_ADDRESS> 替换为 Hub 机器的实际 IP 地址或主机名。节点将检测本地驱动程序/浏览器并向 Hub 注册。
您可以在 -jar 参数之前使用 -Dwebdriver.chrome.driver=/path/to/driver 来指定驱动程序位置,或者使用 TOML 文件配置节点以进行更高级的设置(例如,特定的浏览器版本,每个节点的最大并行会话数)。
访问 Grid UI
Section titled “访问 Grid UI”Grid(独立模式或 Hub)运行后,您可以在浏览器中访问其 UI,通常地址是 http://localhost:4444。该 UI 显示已注册的节点、可用的浏览器槽位和活动会话。
在 Grid 上运行测试
Section titled “在 Grid 上运行测试”要在 Grid 上运行您的测试,您需要做以下几点:
- 使用
RemoteWebDriver而不是特定的驱动程序类(如ChromeDriver)。 - 提供 Grid Hub/独立服务器的 URL。
- 使用浏览器 Options 类(例如
ChromeOptions,FirefoxOptions)指定所需的 Capabilities(浏览器名称、版本、平台)。
使用 Java、TestNG 和 RemoteWebDriver 的示例:
package com.example.gridtests;
import org.openqa.selenium.WebDriver;import org.openqa.selenium.remote.RemoteWebDriver;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.chrome.ChromeOptions;import org.openqa.selenium.firefox.FirefoxOptions;import org.testng.annotations.Parameters;import org.testng.annotations.Test;import org.testng.annotations.BeforeMethod;import org.testng.annotations.AfterMethod;
import java.net.MalformedURLException;import java.net.URL;
public class GridTest {
private WebDriver driver; private final String gridUrl = "http://localhost:4444"; // Or your Hub URL
@Parameters("browser") @BeforeMethod public void setUp(String browser) throws MalformedURLException { URL url = new URL(gridUrl);
if (browser.equalsIgnoreCase("chrome")) { ChromeOptions options = new ChromeOptions(); // Add any specific Chrome options if needed // options.addArguments("--headless"); driver = new RemoteWebDriver(url, options); System.out.println("Starting Chrome session on Grid..."); } else if (browser.equalsIgnoreCase("firefox")) { FirefoxOptions options = new FirefoxOptions(); // Add any specific Firefox options if needed driver = new RemoteWebDriver(url, options); System.out.println("Starting Firefox session on Grid..."); } else { throw new IllegalArgumentException("Unsupported browser: " + browser); } driver.manage().window().maximize(); }
@Test public void simpleGridTest() { driver.get("https://www.google.com"); System.out.println("Page Title: " + driver.getTitle()); // Add your test steps here... assert driver.getTitle().contains("Google"); }
@AfterMethod public void tearDown() { if (driver != null) { driver.quit(); System.out.println("Browser session closed."); } }}您通常会使用 TestNG XML 套件文件来运行此测试,该文件会传递 browser 参数,从而允许在 Grid 上注册的不同浏览器之间并行执行。
<!-- Example testng.xml --><!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"><suite name="Grid Suite" parallel="tests" thread-count="2"> <test name="Chrome Test"> <parameter name="browser" value="chrome"/> <classes> <class name="com.example.gridtests.GridTest"/> </classes> </test> <test name="Firefox Test"> <parameter name="browser" value="firefox"/> <classes> <class name="com.example.gridtests.GridTest"/> </classes> </test></suite>Selenium Grid 的优势
Section titled “Selenium Grid 的优势”- 并行执行 (Parallel Execution): 大幅度减少总测试执行时间。
- 跨浏览器/平台测试 (Cross-Browser/Platform Testing): 轻松在不同配置上进行测试。
- 可伸缩性 (Scalability): 随着测试需求的增长,添加更多节点机器。
- 集中管理 (Centralized Management): 从 Grid 节点管理浏览器版本和配置。
对于认真进行全面且高效的 Web 应用测试的任何团队来说,Selenium Grid 都是一个必不可少的工具。