使用
selenium/node-docker
和 selenium/hub
如何访问 http://<node-ip>:7900?autoconnect=1&resize=scale&password=secret
上显示的 novnc 实时预览
这是我的 docker-compose
version: "3"
services:
node-docker:
container_name: node-docker
image: selenium/node-docker:4.21.0-20240522
volumes:
- ./assets:/opt/selenium/assets #For video recordings
- ./config.toml:/opt/bin/config.toml #Mount configuration file
- /var/run/docker.sock:/var/run/docker.sock #Grid to run docker commands
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
- SE_VNC_VIEW_ONLY=1
- SE_VNC_NO_PASSWORD=1
selenium-hub:
image: selenium/hub:4.21.0-20240522
container_name: selenium-hub
ports:
- "4442:4442"
- "4443:4443"
- "4444:4444"
app:
container_name: app
build:
context: ./app
ports:
- "5000:5000"
这是 selenium/node-docker 用于构建节点的 config.toml
[docker]
# Configs have a mapping between the Docker image to use and the capabilities that need to be matched to
# start a container with the given image.
configs = [
"selenium/standalone-chrome:4.21.0-20240522", '{"browserName": "chrome"}'
]
host-config-keys = ["Dns", "DnsOptions", "DnsSearch", "ExtraHosts", "Binds"]
# URL for connecting to the docker daemon
# Most simple approach, leave it as http://127.0.0.1:2375, and mount /var/run/docker.sock.
# 127.0.0.1 is used because internally the container uses socat when /var/run/docker.sock is mounted
# If var/run/docker.sock is not mounted:
# Windows: make sure Docker Desktop exposes the daemon via tcp, and use http://host.docker.internal:2375.
# macOS: install socat and run the following command, socat -4 TCP-LISTEN:2375,fork UNIX-CONNECT:/var/run/docker.sock,
# then use http://host.docker.internal:2375.
# Linux: varies from machine to machine, please mount /var/run/docker.sock. If this does not work, please create an issue.
url = "http://127.0.0.1:2375"
# Docker image used for video recording
video-image = "selenium/video:ffmpeg-6.1.1-20240522"
# Uncomment the following section if you are running the node on a separate VM
# Fill out the placeholders with appropriate values
#[server]
#host = <ip-from-node-machine>
#port = <port-from-node-machine>
我想要的只是在我的应用程序中显示实时测试,但我无法让它工作,我什至尝试直接通过
ws://0.0.0.0:4444/session/<session_id>/se/vnc
中的 novnc websocket 二进制消息,但事情太复杂了。noVNC
上的selenium/node-docker
端口尚未暴露。
要使用 noVNC,必须配置
selenium/node-docker
,将容器的默认 noVNC 端口 (7900) 分配给任何本地/主机端口。
另外,对于包含浏览器的图像,请使用标志 --shm-size=2g 来使用主机的共享内存(基于上面的问题,即
selenium/standalone-chrome:4.21.0-20240522
)。
请参阅下面修改后的 docker-compose.yml:
version: "3"
services:
node-docker:
container_name: node-docker
image: selenium/node-docker:4.21.0-20240522
shm_size: 2gb
volumes:
- ./assets:/opt/selenium/assets #For video recordings
- ./config.toml:/opt/bin/config.toml #Mount configuration file
- /var/run/docker.sock:/var/run/docker.sock #Grid to run docker commands
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
- SE_VNC_VIEW_ONLY=1
- SE_VNC_NO_PASSWORD=1
ports:
- "7900:7900"
selenium-hub:
image: selenium/hub:4.21.0-20240522
container_name: selenium-hub
ports:
- "4442:4442"
- "4443:4443"
- "4444:4444"
app:
container_name: app
build:
context: ./app
ports:
- "5000:5000"
重新创建网格,然后您应该能够像之前通过
noVNC
一样访问
http://<node-ip>:7900autoconnect=1&resize=scale&password=secret
此外,还可以通过 Selenium Grid UI 访问
noVNC
预览,方法是转到 Sessions
选项卡,然后单击 video
旁边的 session id
图标。