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://selenium.dev.org.tw");
driver.quit();

啟用節點管理的下載。

可以指示節點自動管理下載。這將導致節點將特定會話下載的所有檔案保存到臨時目錄中,稍後可以從節點檢索這些檔案。要啟用此功能,請使用以下設定

[node]
enable-managed-downloads = true

有關完整的範例,請參閱 CLI 章節

上次修改時間:2024 年 4 月 23 日:[grid] 更新 CLI/TOML 選項列表 (#1683) (1f27efd060f)