Skip to main content

Selenium 网格(实验)

介绍

🌐 Introduction

Playwright 可以连接到运行 Selenium 4 的 Selenium Grid Hub 来启动 Google ChromeMicrosoft Edge 浏览器,而不是在本地机器上运行浏览器。请注意,此功能是实验性的,因此其优先级也相应调整。

🌐 Playwright can connect to Selenium Grid Hub that runs Selenium 4 to launch Google Chrome or Microsoft Edge browser, instead of running browser on the local machine. Note this feature is experimental and is prioritized accordingly.

warning

Playwright 与 Selenium Grid Hub 的集成在未来可能存在中断的风险。在使用之前,请确保权衡风险和收益。

🌐 There is a risk of Playwright integration with Selenium Grid Hub breaking in the future. Make sure you weight risks against benefits before using it.

更多详情

在内部,Playwright 使用 Chrome DevTools 协议 websocket 连接到浏览器。Selenium 4 当前提供了此功能。然而,未来可能不会如此。如果 Selenium 放弃此功能,Playwright 将无法继续与其配合使用。

🌐 Internally, Playwright connects to the browser using Chrome DevTools Protocol websocket. Selenium 4 currently exposes this capability. However, this might not be the case in the future. If Selenium drops this capability, Playwright will stop working with it.

在将 Playwright 连接到你的 Selenium Grid 之前,请确保该网格可以与 Selenium WebDriver 一起使用。例如,运行 其中一个示例 并传递 SELENIUM_REMOTE_URL 环境变量。如果 webdriver 示例无法正常工作,请查看你的 Selenium hub/node/standalone 输出中的任何错误,并在 Selenium 问题 中搜索可能的解决方案。

🌐 Before connecting Playwright to your Selenium Grid, make sure that grid works with Selenium WebDriver. For example, run one of the examples and pass SELENIUM_REMOTE_URL environment variable. If webdriver example does not work, look for any errors at your Selenium hub/node/standalone output and search Selenium issues for a possible solution.

启动 Selenium 网格

🌐 Starting Selenium Grid

如果你运行分布式 Selenium Grid,Playwright 需要 Selenium 节点注册一个可访问的地址,以便它可以连接到浏览器。为了确保它按预期工作,在运行 Selenium 节点时设置指向 hub 的 SE_NODE_GRID_URL 环境变量。

🌐 If you run distributed Selenium Grid, Playwright needs selenium nodes to be registered with an accessible address, so that it could connect to the browsers. To make sure it works as expected, set SE_NODE_GRID_URL environment variable pointing to the hub when running selenium nodes.

# Start selenium node
SE_NODE_GRID_URL="http://<selenium-hub-ip>:4444" java -jar selenium-server-<version>.jar node

将 Playwright 连接到 Selenium Grid

🌐 Connecting Playwright to Selenium Grid

要将 Playwright 连接到 Selenium Grid 4,请设置指向你的 Selenium Grid Hub 的 SELENIUM_REMOTE_URL 环境变量。请注意,这仅适用于 Google Chrome 和 Microsoft Edge。

🌐 To connect Playwright to Selenium Grid 4, set SELENIUM_REMOTE_URL environment variable pointing to your Selenium Grid Hub. Note that this only works for Google Chrome and Microsoft Edge.

SELENIUM_REMOTE_URL=http://<selenium-hub-ip>:4444 npx playwright test

你不需要更改你的代码,只需像平常一样使用你的测试框架或 browserType.launch()

🌐 You don't have to change your code, just use your testing harness or browserType.launch() as usual.

传递额外的能力

🌐 Passing additional capabilities

如果你的网格需要设置额外的功能(例如,你使用外部服务),可以设置 SELENIUM_REMOTE_CAPABILITIES 环境变量来提供 JSON 序列化的功能。

🌐 If your grid requires additional capabilities to be set (for example, you use an external service), you can set SELENIUM_REMOTE_CAPABILITIES environment variable to provide JSON-serialized capabilities.

SELENIUM_REMOTE_URL=http://<selenium-hub-ip>:4444 SELENIUM_REMOTE_CAPABILITIES="{'mygrid:options':{os:'windows',username:'John',password:'secure'}}" npx playwright test

传递附加标头

🌐 Passing additional headers

如果你的网格需要设置额外的请求头(例如,在云中使用浏览器时需要提供授权令牌),你可以设置 SELENIUM_REMOTE_HEADERS 环境变量以提供 JSON 序列化的请求头。

🌐 If your grid requires additional headers to be set (for example, you should provide authorization token to use browsers in your cloud), you can set SELENIUM_REMOTE_HEADERS environment variable to provide JSON-serialized headers.

SELENIUM_REMOTE_URL=http://<selenium-hub-ip>:4444 SELENIUM_REMOTE_HEADERS="{'Authorization':'Basic b64enc'}" npx playwright test

详细日志

🌐 Detailed logs

使用 DEBUG=pw:browser* 环境变量运行,以查看 Playwright 如何连接到 Selenium Grid。

🌐 Run with DEBUG=pw:browser* environment variable to see how Playwright is connecting to Selenium Grid.

DEBUG=pw:browser* SELENIUM_REMOTE_URL=http://internal.grid:4444 npx playwright test

如果你提出问题,请包含此日志。

🌐 If you file an issue, please include this log.

使用 Selenium Docker

🌐 Using Selenium Docker

使用 Selenium Grid 的一种简单方法是运行官方 Docker 容器。更多信息请参阅 selenium docker images 文档。有关镜像标签规范,请点击这里了解更多

🌐 One easy way to use Selenium Grid is to run official docker containers. Read more in selenium docker images documentation. For image tagging convention, read more.

独立模式

🌐 Standalone mode

这里是一个运行 Selenium 独立模式并连接 Playwright 的示例。请注意,hub 和 node 位于同一个 localhost 上,我们传递了指向它的 SE_NODE_GRID_URL 环境变量。

🌐 Here is an example of running selenium standalone and connecting Playwright to it. Note that hub and node are on the same localhost, and we pass SE_NODE_GRID_URL environment variable pointing to it.

首先启动 Selenium。

🌐 First start Selenium.

docker run -d -p 4444:4444 --shm-size="2g" -e SE_NODE_GRID_URL="http://localhost:4444" selenium/standalone-chromium:latest

然后运行 Playwright。

🌐 Then run Playwright.

SELENIUM_REMOTE_URL=http://localhost:4444 npx playwright test

集群和节点模式

🌐 Hub and nodes mode

这里是运行 Selenium Hub 和单个 Selenium 节点,并将 Playwright 连接到 Hub 的示例。请注意,Hub 和节点的 IP 不同,并且在启动节点容器时,我们会传递指向 Hub 的 SE_NODE_GRID_URL 环境变量。

🌐 Here is an example of running selenium hub and a single selenium node, and connecting Playwright to the hub. Note that hub and node have different IPs, and we pass SE_NODE_GRID_URL environment variable pointing to the hub when starting node containers.

首先启动 hub 容器和一个或多个节点容器。

🌐 First start the hub container and one or more node containers.

docker run -d -p 4442-4444:4442-4444 --name selenium-hub selenium/hub:4.25.0
docker run -d -p 5555:5555 \
--shm-size="2g" \
-e SE_EVENT_BUS_HOST=<selenium-hub-ip> \
-e SE_EVENT_BUS_PUBLISH_PORT=4442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 \
-e SE_NODE_GRID_URL="http://<selenium-hub-ip>:4444"
selenium/node-chromium:4.25.0

然后运行 Playwright。

🌐 Then run Playwright.

SELENIUM_REMOTE_URL=http://<selenium-hub-ip>:4444 npx playwright test

Selenium 3

在内部,Playwright 使用 Chrome DevTools Protocol websocket 连接浏览器。Selenium 4 提供了这一功能,而 Selenium 3 不支持。

🌐 Internally, Playwright connects to the browser using Chrome DevTools Protocol websocket. Selenium 4 exposes this capability, while Selenium 3 does not.

这意味着 Selenium 3 以尽力而为的方式受到支持,Playwright 会尝试直接连接到网格节点。网格节点必须可以从运行 Playwright 的机器直接访问。

🌐 This means that Selenium 3 is supported in a best-effort manner, where Playwright tries to connect to the grid node directly. Grid nodes must be directly accessible from the machine that runs Playwright.