TOML 配置选项

使用 Toml 文件的 Grid 配置示例。

CLI 选项 中显示的所有选项都可以通过 TOML 文件进行配置。此页面显示了不同 Grid 组件的配置示例。

请注意,如果某个选项被修改或添加但尚未记录,则此文档可能会过时。如果您遇到这种情况,请查看 “配置帮助” 部分,并随时向我们发送拉取请求以更新此页面。

概述

Selenium Grid 使用 TOML 格式的配置文件。配置文件由多个部分组成,每个部分都有选项及其各自的值。

有关详细的使用指南,请参阅 TOML 文档。如果出现解析错误,请使用 TOML linter 验证配置。

通用配置结构具有以下模式

[section1]
option1="value"

[section2]
option2=["value1","value2"]
option3=true

以下是一些使用 Toml 文件配置的 Grid 组件的示例,组件可以通过以下方式启动

java -jar selenium-server-<version>.jar <component> --config /path/to/file/<file-name>.toml

独立模式

一个独立的服务器,在端口 4449 上运行,新的会话请求超时时间为 500 秒。

[server]
port = 4449

[sessionqueue]
session-request-timeout = 500

特定浏览器和最大会话数限制

一个独立的服务器或节点,默认情况下仅启用 Firefox 和 Chrome。

[node]
drivers = ["chrome", "firefox"]
max-sessions = 3

配置和自定义驱动程序

具有自定义驱动程序的独立或节点服务器,允许诸如使用 Firefox Beta 或 Nightly 以及具有不同的浏览器版本之类的事情。

[node]
detect-drivers = false
[[node.driver-configuration]]
max-sessions = 100
display-name = "Firefox Nightly"
stereotype = "{\"browserName\": \"firefox\", \"browserVersion\": \"93\", \"platformName\": \"MAC\", \"moz:firefoxOptions\": {\"binary\": \"/Applications/Firefox Nightly.app/Contents/MacOS/firefox-bin\"}}"
[[node.driver-configuration]]
display-name = "Chrome Beta"
stereotype = "{\"browserName\": \"chrome\", \"browserVersion\": \"94\", \"platformName\": \"MAC\", \"goog:chromeOptions\": {\"binary\": \"/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta\"}}"
[[node.driver-configuration]]
display-name = "Chrome Dev"
stereotype = "{\"browserName\": \"chrome\", \"browserVersion\": \"95\", \"platformName\": \"MAC\", \"goog:chromeOptions\": {\"binary\": \"/Applications/Google Chrome Dev.app/Contents/MacOS/Google Chrome Dev\"}}"
webdriver-executable = '/path/to/chromedriver/95/chromedriver'

独立模式或使用 Docker 的节点

一个能够在 Docker 容器中运行每个新会话的独立或节点服务器。禁用驱动程序检测,最多允许 2 个并发会话。配置的刻板印象需要映射到 Docker 镜像,并且 Docker 守护程序需要通过 http/tcp 公开。此外,可以通过 devices 属性定义主机上可访问的哪些设备文件将在容器中可用。有关 Docker 设备映射的工作原理的更多信息,请参阅 docker 文档。

[node]
detect-drivers = false
max-sessions = 2

[docker]
configs = [
    "selenium/standalone-chrome:93.0", "{\"browserName\": \"chrome\", \"browserVersion\": \"91\"}", 
    "selenium/standalone-firefox:92.0", "{\"browserName\": \"firefox\", \"browserVersion\": \"92\"}"
]
#Optionally define all device files that should be mapped to docker containers
#devices = [
#    "/dev/kvm:/dev/kvm"
#]
url = "http://localhost:2375"
video-image = "selenium/video:latest"

将命令中继到支持 WebDriver 的服务端点

将支持 WebDriver 的外部服务连接到 Selenium Grid 非常有用。此类服务的示例可以是云提供商或 Appium 服务器。通过这种方式,Grid 可以为本地不存在的平台和版本提供更多的覆盖范围。

以下是将 Appium 服务器连接到 Grid 的示例。

[node]
detect-drivers = false

[relay]
# Default Appium/Cloud server endpoint
url = "http://localhost:4723/wd/hub"
status-endpoint = "/status"
# Optional, enforce a specific protocol version in HttpClient when communicating with the endpoint service status (e.g. HTTP/1.1, HTTP/2)
protocol-version = "HTTP/1.1"
# Stereotypes supported by the service. The initial number is "max-sessions", and will allocate 
# that many test slots to that particular configuration
configs = [
  "5", "{\"browserName\": \"chrome\", \"platformName\": \"android\", \"appium:platformVersion\": \"11\"}"
]

启用基本身份验证

可以通过使用用户名和密码配置 Router/Hub/Standalone 来保护 Grid 的基本身份验证。加载 Grid UI 或启动新会话时将需要此用户名/密码组合。

[router]
username = "admin"
password = "myStrongPassword"

这是一个 Java 示例,显示如何使用配置的用户和密码启动会话。

ClientConfig clientConfig = ClientConfig.defaultConfig()
  .baseUrl(new URL("http://localhost:4444"))
  .authenticateAs(new UsernameAndPassword("admin", "myStrongPassword"));
HttpCommandExecutor executor = new HttpCommandExecutor(clientConfig);
RemoteWebDriver driver = new RemoteWebDriver(executor, new ChromeOptions());

在其他语言中,您可以使用 URL http://admin:myStrongPassword@localhost:4444

设置自定义功能以匹配特定节点

重要提示: 自定义功能需要在所有节点的配置中进行设置。它们也需要始终包含在每个会话请求中。

[node]
detect-drivers = false

[[node.driver-configuration]]
display-name = "firefox"
stereotype = '{"browserName": "firefox", "platformName": "macOS", "browserVersion":"96", "networkname:applicationName":"node_1", "nodename:applicationName":"app_1" }'
max-sessions = 5

这是一个 Java 示例,显示如何匹配该节点

FirefoxOptions options = new FirefoxOptions();
options.setCapability("networkname:applicationName", "node_1");
options.setCapability("nodename:applicationName", "app_1");
options.setBrowserVersion("96");
options.setPlatformName("macOS");
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444"), options);
driver.get("https://seleniumcn.cn");
driver.quit();

启用节点管理的下载。

可以指示节点自动管理下载。这将导致节点将特定会话下载的所有文件保存到临时目录中,该临时目录稍后可以从节点检索。要启用此功能,请使用以下配置

[node]
enable-managed-downloads = true

有关完整示例,请参阅 CLI 部分

上次修改时间:2024 年 4 月 23 日:[grid] 更新 CLI/TOML 选项列表 (#1683) (1f27efd060f)