selenium hub + docker 环境搭建
云服务器一台 Ubuntu,已安装 docker
镜像地址 :https://hub.docker.com/u/selenium
How to run this image
The Hub and Nodes will be created in the same network and they will recognize each other by their container name. A Docker network needs to be created as a first step.
- Create a Docker Network
docker network create grid
- Start the Hub using the created network
docker run -d -p 4442-4444:4442-4444 --net grid --name selenium-hub selenium/hub:latest
- Start the Node using the created network
docker run -d --net grid -e SE_EVENT_BUS_HOST=selenium-hub \
--shm-size="2g" \
-e SE_EVENT_BUS_PUBLISH_PORT=4442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 \
selenium/node-chrome:latest
docker run -d --net grid -e SE_EVENT_BUS_HOST=selenium-hub \
--shm-size="2g" \
-e SE_EVENT_BUS_PUBLISH_PORT=4442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 \
selenium/node-edge:latest
docker run -d --net grid -e SE_EVENT_BUS_HOST=selenium-hub \
--shm-size="2g" \
-e SE_EVENT_BUS_PUBLISH_PORT=4442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 \
selenium/node-firefox:latest
If you are using Windows Powershell, use this command:
docker run -d --net grid -e SE_EVENT_BUS_HOST=selenium-hub `
--shm-size="2g" `
-e SE_EVENT_BUS_PUBLISH_PORT=4442 `
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 `
selenium/node-chrome:latest
docker run -d --net grid -e SE_EVENT_BUS_HOST=selenium-hub `
--shm-size="2g" `
-e SE_EVENT_BUS_PUBLISH_PORT=4442 `
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 `
selenium/node-edge:latest
docker run -d --net grid -e SE_EVENT_BUS_HOST=selenium-hub `
--shm-size="2g" `
-e SE_EVENT_BUS_PUBLISH_PORT=4442 `
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 `
selenium/node-firefox:latest
-
Point your WebDriver tests to http://localhost:4444
-
That's it!
-
(Optional) To see what is happening inside the container, head to the Grid UI at http://localhost:4444/ui.
-
The example above uses
latestas a tag, but we recommend to full tag to pin a specific browser and Grid version. Please see Tagging Conventions for details. -
A more detailed explanation that shows how to run the Hub and Nodes in different configurations can be seen at the Docker-Selenium project in GitHub
-
When you are done using the Grid, and the containers have exited, the network can be removed with the following command:
docker network rm grid
How to choose the correct tag for you
The tag structure is as follows:
selenium/hub-<Major>.<Minor>.<Patch>-<YYYYMMDD>
Example of a release with Selenium Grid Server 4.9.0, released on 20230426
Selenium Server 4.9.0
Release date 20230426
e126989f151e selenium/hub 4
e126989f151e selenium/hub 4.9
e126989f151e selenium/hub 4.9.0
e126989f151e selenium/hub 4.9.0-20230426
With that, you can use any of the different tags to get the most recent release in a simplified way.
Full documentation
The Docker-Selenium project in GitHub has an extensive README that will help you find the correct way to get this images up and running for your use case.
License
The project is made possible by volunteer contributors who have put in thousands of hours of their own time, and made the source code freely available under the Apache License 2.0.
几个运行命令
docker run -d --name grid_hub -p 5442-5444:4442-4444 selenium/hub
sudo docker run -d -p 5442-5444:4442-4444 --net grid --name selenium-hub selenium/hub:latest
docker run -d --name grid_node \
-p 5902:5900 \
-e SE_EVENT_BUS_HOST=101.43.55.51 \
-e SE_NODE_MAX_SESSIONS=20 \
-e NODE_MAX_INSTANCES=20 \
-e SE_NODE_OVERRIDE_MAX_SESSIONS=true \
-e SE_EVENT_BUS_PUBLISH_PORT=5442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=5443 \
-v /dev/shm:/dev/shm selenium/node-chrome
sudo docker run -d --net grid -e SE_EVENT_BUS_HOST=selenium-hub \
--shm-size="2g" \
-e SE_EVENT_BUS_PUBLISH_PORT=4442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 \
selenium/node-chrome:latest
sudo docker run -d --net grid --name grid_node \
-p 5902:5900 \
-e SE_EVENT_BUS_HOST=selenium-hub \
-e SE_NODE_MAX_SESSIONS=20 \
-e NODE_MAX_INSTANCES=20 \
-e SE_NODE_OVERRIDE_MAX_SESSIONS=true \
-e SE_EVENT_BUS_PUBLISH_PORT=4442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 \
-v /dev/shm:/dev/shm selenium/node-chrome
- --name:给启动的容器命名一个别名。
- 5442-5443:hub 与 node 的通信服务。
- 5444:提供 hub 的 web 图形界面服务。
- 5902:提供 node 的远程访问服务,以查看浏览器执行情况(需配合 VNC Viewer 使用)
- -e:指定环境变量,用于传参。
- --link grid_hub:获取 hub 容器的网络信息。
- MAX_SESSION:限制同时最多只能开启 20 个浏览器(超过则排队)。
- NODE_MAX_INSTANCES:一般和 NODE_MAX_SESSION 设置一样,表示最多同时启动的浏览器实例数。
- /dev/shm:用于优化内存。
安装 VNC viewer
- vnc viewer 是一款优秀的远程控制工具软件。
- 官网下载地址:https://www.realvnc.com/en/connect/download/viewer/
- 安装好以后 File->New connection,在弹出的界面中输入 node 的 IP 和端口号,保存后双击启动。
- 连接时要求输入密码,默认密码是 secret 。
浙公网安备 33010602011771号