demo 地址:https://github.com/xglh/selenoid_demo
selenoid 官方地址:https://aerokube.com/selenoid/latest/
selenoid 是一款开源的 web 自动化远程执行器,与 seleium grid 功能类似,具有 seleium grid 不具备的实时画面和录屏的优点,具体对比如下
对比项 | seleium grid | selenoid |
---|---|---|
实时预览 | N | Y |
录屏 | N | Y |
多版本浏览器 | Y | Y |
资源回收 | N | Y |
支持分布式 | Y | N |
除了不支持分布式,其他项都优于 seleium grid
依赖模块:docker、docker-compose
{
"chrome": {
"default": "85.0",
"versions": {
"85.0": {
"image": "selenoid/vnc:chrome_85.0",
"port": "4444",
"path": "/"
}
}
},
"firefox": {
"default": "79.0",
"versions": {
"79.0": {
"image": "selenoid/vnc:firefox_79.0",
"port": "4444",
"path": "/wd/hub"
}
}
},
"opera": {
"default": "70.0",
"versions": {
"70.0": {
"image": "selenoid/vnc:opera_70.0",
"port": "4444",
"path": "/"
}
}
}
}
保存为 docker-compose.yml,与 browsers.json 放在同一目录下
version: '3'
services:
selenoid:
image: "aerokube/selenoid"
network_mode: bridge
restart: always
ports:
- "4444:4444"
volumes:
- "$PWD:/etc/selenoid/" # assumed current dir contains browsers.json
- "$PWD/video/:/opt/selenoid/video/" #
- "/var/run/docker.sock:/var/run/docker.sock"
- "$PWD/logs/:/opt/selenoid/logs/"
# 录屏保存地址
environment:
- OVERRIDE_VIDEO_OUTPUT_DIR=$PWD/video/
# limit为允许开启实例最大值
command: -limit 10 -service-startup-timeout 1m -retry-count 3 -enable-file-upload -log-output-dir /opt/selenoid/logs
selenoid-ui:
image: "aerokube/selenoid-ui"
network_mode: bridge
restart: always
links:
- selenoid
ports:
- "8080:8080"
command: ["--selenoid-uri", "http://selenoid:4444"]
docker pull selenoid/vnc:chrome_85.0
docker pull selenoid/vnc:firefox_79.0
docker pull selenoid/vnc:opera_70.0
docker pull selenoid/video-recorder:latest-release
docker-compose -f docker-compose.yml up -d
import time
from selenium import webdriver
class TestClass:
def test_baidu_chrome(self):
capabilities = {
# 测试用例名称
"name": "test_baidu_chrome",
# 浏览器类型
"browserName": "chrome",
# 版本号
"version": "85.0",
# 开启远程VNC实时画面
"enableVNC": True,
# 开启视频录制
"enableVideo": True,
# 保存视频名称
"videoName": "test_baidu_chrome.mp4"
}
driver = webdriver.Remote(
command_executor="http://10.118.80.65:4444/wd/hub",
desired_capabilities=capabilities)
driver.get('https://www.baidu.com')
driver.find_element_by_id('kw').send_keys('selenoid')
time.sleep(2)
def test_baidu_firefox(self):
capabilities = {
# 测试用例名称
"name": "test_baidu_firefox",
# 浏览器类型
"browserName": "firefox",
# 版本号
"version": "79.0",
# 开启远程VNC实时画面
"enableVNC": True,
# 开启视频录制
"enableVideo": True,
# 保存视频名称
"videoName": "test_baidu_firefox.mp4"
}
driver = webdriver.Remote(
command_executor="http://10.118.80.65:4444/wd/hub",
desired_capabilities=capabilities)
driver.get('https://www.baidu.com')
driver.find_element_by_id('kw').send_keys('selenoid')
time.sleep(2)
def test_baidu_opera(self):
capabilities = {
# 测试用例名称
"name": "test_baidu_opera",
# 浏览器类型
"browserName": "opera",
# 版本号
"version": "79.0",
# 开启远程VNC实时画面
"enableVNC": True,
# 开启视频录制
"enableVideo": True,
# 保存视频名称
"videoName": "test_baidu_opera.mp4",
# opera浏览器需要额外参数
"operaOptions": {"binary": "/usr/bin/opera"}
}
driver = webdriver.Remote(
command_executor="http://10.118.80.65:4444/wd/hub",
desired_capabilities=capabilities)
driver.get('https://www.baidu.com')
driver.find_element_by_id('kw').send_keys('selenoid')
time.sleep(2)
chrome 支持 48.0~78.0版本
firefox 支持 3.6~83.0版本
opera 支持 33.0~72.0版本
找到对应版本号,pull 镜像,并将版本号加入到 browsers.json 中
以 IE 为例
{
"internet explorer": {
"default": "11",
"versions": {
"11": {
"image": ["C:\\IEDriverServer.exe", "--log-level=DEBUG"]
}
}
}
}
./selenoid_win_amd64.exe -conf ./browsers.json -disable-docker
http://localhost:4444/wd/hub
备注:其他类型浏览器找到对应驱动即可,目前已接入 ie,qq,360 浏览器