web自动化selenium grid分布式运行
一、docker-compose
1、安装docker-compose
使用 curl 命令下载 Docker Compose 的安装包
sudo curl -L "https://get.daocloud.io/docker/compose/releases/download/v2.6.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
修改 Docker Compose 执行权限
sudo chmod +x /usr/local/bin/docker-compose
添加软链接
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
检查 Docker Compose 是否安装成功
docker-compose --version
2、测试工具安装
docker pull busybox
docker pull selenium/node-chrome
docker pull selenium/node-firefox
docker pull selenium/node-edge
docker pull selenium/event-bus
docker pull selenium/event-queue
docker pull selenium/event-distributor
docker pull selenium/event-router
二、selenium grid
1、docker run方式启动节点
docker network create grid
docker run -d -p 4442-4444:4442-4444 --net grid --name selenium-hub selenium/hub
docker run -d --name chrome-node1 --net grid -e SE_EVENT_BUS_HOST=selenium-hub \
--shm-size="2g" \
-e SE_NODE_MAX_SESSIONS=5 \
-e SE_NODE_OVERRIDE_MAX_SESSIONS=true \
-e SE_NODE_SESSION_TIMEOUT=350 \
-e SE_EVENT_BUS_PUBLISH_PORT=4442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 \
-e SE_VNC_NO_PASSWORD=1 \
selenium/node-chrome
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
2、编写docker-compose-v3-full-grid.yml文件
- 新建yml文件
touch docker-compose-grid.yml
- 编写容器编排yml
# To execute this docker-compose yml file use `docker-compose -f docker-compose-v3-full-grid.yml up`
# Add the `-d` flag at the end for detached execution
# To stop the execution, hit Ctrl+C, and then `docker-compose -f docker-compose-v3-full-grid.yml down`
version: "3"
services:
selenium-event-bus: # 这个不要改
image: selenium/event-bus
container_name: selenium-event-bus
ports:
- "4442:4442"
- "4443:4443"
- "5557:5557"
selenium-sessions: # 这个不要改
image: selenium/sessions
container_name: selenium-sessions
ports:
- "5556:5556"
depends_on:
- selenium-event-bus
environment:
- SE_EVENT_BUS_HOST=selenium-event-bus
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
selenium-session-queue: # 这个不要改
image: selenium/session-queue
container_name: selenium-session-queue
ports:
- "5559:5559"
depends_on:
- selenium-event-bus
environment:
- SE_EVENT_BUS_HOST=selenium-event-bus
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
selenium-distributor: # 这个不要改
image: selenium/distributor
container_name: selenium-distributor
ports:
- "5553:5553"
depends_on:
- selenium-event-bus
- selenium-sessions
- selenium-session-queue
environment:
- SE_EVENT_BUS_HOST=selenium-event-bus
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
- SE_SESSIONS_MAP_HOST=selenium-sessions
- SE_SESSIONS_MAP_PORT=5556
- SE_SESSION_QUEUE_HOST=selenium-session-queue
- SE_SESSION_QUEUE_PORT=5559
selenium-router:
image: selenium/router
container_name: selenium-router
ports:
# 这个可以修改左边的4444,右边的4444不能改。左边的是4444宿主机Grid访问接口!
# 使用 ip:4444可以访问router网页,查看当前连接的node和session
# selenium自动化远程执行就是连接router,左边的端口就是入口,映射到右边的容器内端口
- "4444:4444"
depends_on:
- selenium-distributor
- selenium-sessions
- selenium-session-queue
environment:
- SE_DISTRIBUTOR_HOST=selenium-distributor
- SE_DISTRIBUTOR_PORT=5553
- SE_SESSIONS_MAP_HOST=selenium-sessions
- SE_SESSIONS_MAP_PORT=5556
- SE_SESSION_QUEUE_HOST=selenium-session-queue
- SE_SESSION_QUEUE_PORT=5559
chrome: # 这是 Node
image: selenium/node-chrome
volumes:
- /dev/shm:/dev/shm
depends_on:
- selenium-event-bus
environment:
- SE_EVENT_BUS_HOST=selenium-event-bus #不能改
- SE_EVENT_BUS_PUBLISH_PORT=4442 #不能改
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443 #不能改
- SE_NODE_MAX_SESSIONS=5 # 添加这个参数能让一个容器运行多个浏览器窗口(跑多个job)
- SE_NODE_OVERRIDE_MAX_SESSIONS=true # 如果添加了SE_NODE_MAX_SESSIONS,则这个参数必须添加
- SCREEN_HEIGHT=1080
- SCREEN_WIDTH=1920 # 容器浏览器分辨率
- SE_VNC_NO_PASSWORD=1 #VNC不用输入密码即可访问
ports:
- "5901:5900" # 下载VNC Viewer软件,新建会话 ip:5901 ,能看到容器中运行的浏览器窗口,密码secret
- "7901:7900" # 浏览器中直接访问 ip:7901 , 能看到容器中运行的浏览器窗口,密码secret。如果noVNC连不上说明网络有防火墙。
firefox:
image: selenium/node-firefox
volumes:
- /dev/shm:/dev/shm
depends_on:
- selenium-event-bus
environment:
- SE_EVENT_BUS_HOST=selenium-event-bus
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
- SE_NODE_MAX_SESSIONS=5
- SE_NODE_OVERRIDE_MAX_SESSIONS=true
- SCREEN_HEIGHT=1080
- SCREEN_WIDTH=1920
- SE_VNC_NO_PASSWORD=1
ports:
- "5902:5900"
- "7902:7900"
edge:
image: selenium/node-edge
volumes:
- /dev/shm:/dev/shm
depends_on:
- selenium-event-bus
environment:
- SE_EVENT_BUS_HOST=selenium-event-bus
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
- SE_VNC_NO_PASSWORD=1
ports:
- "5903:5900"
- "7903:7900"
- docker-compose启动容器
docker-compose -f docker-compose-grid.yml up -d
- docker-compose停止容器
docker-compose -f docker-compose-grid.yml down
- 批量删除容器与镜像
docker rm -f $(docker ps -qa)
docker ps -a | awk '{print $1}' | xargs docker rm
- 批量启动容器
docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)
docker start $(docker ps -aq)
- 关闭所有的容器
docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)
docker stop $(docker ps -aq)
- 批量重启容器
docker restart $(docker ps | grep test | awk '{ print $1}')
docker restart $(docker ps -aq)
3、自动化测试脚本
def get_options(browser_name="Chrome"):
"""浏览器添加配置选项"""
if browser_name == "Firefox":
options = webdriver.FirefoxOptions()
elif browser_name == "Chrome":
options = webdriver.ChromeOptions()
else:
options = webdriver.ChromeOptions()
if platform.system() != "Windows":
options.add_argument('--headless')
options.add_argument('--window-size=1920,1080')
else:
options.add_argument('--start-maximized')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--disable-gpu')
return options
class BrowserDriver:
def __init__(self):
self.browser = ParseConfigFile(get_path.of_config_file())("BROWSER", 'browser') # 选择默认执行的浏览器
self.ip = ParseConfigFile(get_path.of_config_file())("GRID_HUB", 'ip')
self.port = ParseConfigFile(get_path.of_config_file())("GRID_HUB", 'port')
self.browser_name = ParseConfigFile(get_path.of_config_file())("GRID_HUB", 'browser_name')
def get_driver(self):
if self.browser == 'Firefox':
options = get_options(self.browser)
self.driver = webdriver.Firefox(options=options)
elif self.browser == 'Chrome':
options = get_options(self.browser)
self.driver = webdriver.Chrome(options=options)
elif self.browser == 'Docker':
caps = {
'platform': 'ANY',
'browserName': self.browser_name,
'javascriptEnabled': True,
'acceptInsecureCerts': True
}
options = get_options(caps["browserName"].capitalize())
self.driver = webdriver.Remote(
command_executor=f'http://{self.ip}:{self.port}/wd/hub',
desired_capabilities=caps,
options=options
)
else:
raise NameError(f"Not found {self.browser} browser,You can enter 'Chrome', 'Firefox','Docker'")
self.driver.implicitly_wait(10)
return self.driver
4、Capabilities说明
- 节点配置
容器节点需要通过事件总线注册容器节点本身,所以需要告诉容器节点事件总线是谁
#事件总线名称或者IP
-e SE_EVENT_BUS_HOST=<event_bus_ip|event_bus_name>
#事件总线端口
-e SE_EVENT_BUS_PUBLISH_PORT=4442
#事件总线注册端口
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443
- 设置屏幕分辨率
#屏幕宽度
-e SE_SCREEN_WIDTH=1366
#屏幕高度
-e SE_SCREEN_HEIGHT=768
- 设置VNC密码
如果不想输入密码,那么可以在环境变量中加入此参数
-e SE_VNC_NO_PASSWORD=1
- 设置并发数
#屏幕宽度
-e SE_NODE_MAX_SESSIONS=5
#屏幕高度
-e SE_NODE_OVERRIDE_MAX_SESSIONS=true
三、VNC查看结果
打开浏览器输入:http://192.168.28.241:4444/ui
在新版本的Selenium Grid中,查看浏览器结果,不再单独安装VNC软件,直接在浏览器中即可查看执行结果。如图所示,可以看出Capabilities中,默认开启vnc功能:
查看grid 节点中的执行结果
输入密码(默认密码即为:secret)
可以看到,浏览器已经打开目标网址
四、参考
1、docker-selenium官网:https://github.com/SeleniumHQ/docker-selenium
2、帮助文档官网:https://www.selenium.dev/support/
3、博客:https://www.cnblogs.com/tracydzf/p/15641118.html
4、docker-compose-yaml:https://github.com/SeleniumHQ/docker-selenium/blob/trunk/docker-compose-v3-full-grid.yml
5、docker-compose下载命令:https://get.daocloud.io/#install-compose