selenium + Selenoid 实现前端展示执行的ui过程 还有个Zalenium
Selenoid
可以在机器上跑自动化然后在web端上看到效果
https://www.jianshu.com/p/29c7240e9f48 (这篇比较详细)
https://blog.csdn.net/weixin_44149181/article/details/120214359
https://blog.csdn.net/weixin_41821317/article/details/115431336
一、下载镜像启动容器 (除了浏览器镜像和视频录制外 其他可以不下,启动容器会自动下)
//实现浏览器的启动和运行
docker pull aerokube/selenoid:latest-release
//浏览器界面
docker pull aerokube/selenoid-ui
//视频录制
docker pull selenoid/video-recorder:latest-release
//拉取一个浏览器镜像
docker pull selenoid/vnc:chrome_64.0
各种浏览器版本见maven仓库
chrome: https://hub.docker.com/r/selenoid/vnc_chrome/tags
firefox: https://hub.docker.com/r/selenoid/vnc_firefox/tags
二、编写browsers.json 文件:
自己放个目录创建这个browsers.json 文件 谢谢!
{ "chrome": { "default": "64.0", "versions": { "64.0": { "image": "selenoid/vnc:chrome_64.0", "port": "4444", "path": "/",
"env": ["LANG=ru_RU.UTF-8","LANGUAGE=ru:en","LC_ALL=ru_RU.UTF-8"]
} } }, "firefox": { "default": "59.0", "versions": { "59.0": { "image": "selenoid/vnc:firefox_59.0", "port": "4444", "path": "/wd/hub" } } }, "phantomjs":{ "default": "2.1.1", "versions": { "2.1.1": { "image": "selenoid/phantomjs:2.1.1", "port": "4444", "path": "/" } } } }
如果是Firefox浏览器,"path": "/"
需要更改为"path": "/wd/hub"
三、启动容器
启动selenoid
docker run -it --name selenoid \
-p 4444:4444 \
-v /Users/lucax/Desktop/test_script/selenoid/config:/etc/selenoid/config/ \
-v /Users/lucax/Desktop/test_script/selenoid/video:/opt/selenoid/video/ \录制的目录关联本地目录
-v /var/run/docker.sock:/var/run/docker.sock \ 注意这个,我踩了坑! 这个是本地docker 的docker.sock 用来通信的 ,我mac docker客户端的就是这个路径
-e OVERRIDE_VIDEO_OUTPUT_DIR=/Users/lucax/Desktop/test_script/selenoid/video/ \ 这个是宿主机环境变量
--user root aerokube/selenoid:latest-release \ 这个是用root的身份执行 也可以用 --privileged 替代
-conf /etc/selenoid/config/browsers.json -limit 10 -video-output-dir /opt/selenoid/video/ 这个是selenoid自己的命令容器的 ,这里指定了 录的视频存放的地方
自己成功的
docker run -d --name selenoid \
-p 4444:4444 \
-v /Users/lucax/Desktop/test_script/selenoid/config:/etc/selenoid/config/ \
-v /Users/lucax/Desktop/test_script/selenoid/video:/opt/selenoid/video/ \
-v /var/run/docker.sock:/var/run/docker.sock \
-e OVERRIDE_VIDEO_OUTPUT_DIR=/Users/lucax/Desktop/test_script/selenoid/video/ \
--user root aerokube/selenoid:latest-release \
-conf /etc/selenoid/config/browsers.json -limit 10 -video-output-dir /opt/selenoid/video
官网的是这样的 https://aerokube.com/selenoid/latest/#_video_recording_enablevideo_videoname_videoscreensize_videoframerate_videocodec (有空看下 怎么玩)
$ docker run -d \ --name selenoid \ -p 4444:4444 \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /your/directory/config/:/etc/selenoid/:ro \ -v /your/directory/video/:/opt/selenoid/video/ \ -e OVERRIDE_VIDEO_OUTPUT_DIR=/your/directory/video/ \ aerokube/selenoid:latest-release
selenoid命令支持以下配置:
-capture-driver-logs 是否向Selenoid输出添加驱动程序日志 -conf String 浏览器配置文件(默认为“config / browsers.json”) -container -network String 用于容器的网络(默认为“default”) -cpu value 容器cpu限制为float,例如0.2或1.0 -disable-docker 禁用docker 支持 -disable-privileged 是否禁用特权容器模式 -disable-queue 禁用等待队列 -enable-file-upload 文件上传支持 -limit int 同时容器运行(默认5) -listen string 网络地址接受连接(默认为“4444”) -log-conf string 字符串容器日志记录配置文件(默认为“config / container-logs.json”)- -mem value 容器内存限制,例如128m或1g -retry-count int 新会话尝试重试计数(默认值为1) -service-startup-timeout duration 服务启动超时时间。持续格式(默认为30秒) -session-attempt-timeout持续时间 新会话尝试超时time.Duration格式(默认30秒) -session-delete-timeout duration 会话删除超时时间。持续格式(默认30秒) -timeout duration 持续时间会话空闲超时时间。持续格式(默认1m0s) -version 显示版本并退出 -video-output-dir String 将录制视频保存到(默认“video”)的目录 -video-recorder-image string 用作录像机的images(默认为“selenoid / video-recorder”)
启动:selenoid-ui:
这个可能只能用windows
docker run -d --name selenoid-ui -p 8080:8080 aerokube/selenoid-ui --selenoid-uri http://${SELENOID_HOST}:4444
mac 的要直接用宿主机的ip
获取本机ip
ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1
执行启动容器
docker run -d --name selenoid-ui -p 8080:8080 aerokube/selenoid-ui --selenoid-uri http://192.168.1.102:4444 (这个用你自己的本机ip替换 即上面的)
四、查看页面
打开selenoid-ui 界面:http://localhost:8080
五、脚本编写
python
from selenium import webdriver from time import sleep capabilities = { "browserName": "chrome", "browserVersion": "64.0", "selenoid:options": { "enableVNC": True, //是否实时查看 "enableVideo": True, //是否需要录制 "videoName":'xxx31232', "name":'123445123',
"screenResolution": "1620x1080x24"
} } driver = webdriver.Remote( command_executor="http://192.168.108.235:4444/wd/hub", desired_capabilities=capabilities) sleep(3) driver.get("https://www.jianshu.com/p/29c7240e9f48") sleep(3) driver.get("https://www.baidu.com") driver.quit()
查看录制的视频: http://localhost:4444/video/
dockercommap(有空研究下)
version: '3'
services:
python:
restart: always
build:
context: .
dockerfile: ./Dockerfile
image: prod_erybj_service
container_name: prod_erybjp_service
network_mode: bridge
volumes:
- /home/yuanbaojun/app/erybjp/data/picture/:/usr/src/app/erybjp/uiplatform/utils/data/picture/
- /home/yuanbaojun/app/erybjp/data/logs/:/usr/src/app/erybjp/logs/
ports:
- "6851:6851"
command: uwsgi --ini uwsgi.ini
selenoid:
restart: always
network_mode: bridge
image: aerokube/selenoid:latest
container_name: prod_erybjp_selenoid
ports:
- "4444:4444"
depends_on:
- python
shm_size: 2g
environment:
- TZ=Asia/Shanghai
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /home/yuanbaojun/app/erybjp/erybjp-master:/etc/selenoid/
- /home/yuanbaojun/app/erybjp/data/selenoid-logs/:/opt/selenoid/logs/
- /etc/localtime:/etc/localtime
command: -limit 10 -service-startup-timeout 1m -enable-file-upload -log-output-dir /opt/selenoid/logs
selenoid-ui:
image: "aerokube/selenoid-ui"
network_mode: bridge
restart: always
container_name: prod_erybjp_selenoid-ui
environment:
- TZ=Asia/Shanghai
links:
- selenoid
ports:
- "5901:8080"
command: ["--selenoid-uri", "http://selenoid:4444"]
networks:
service:
version: '3'
services:
python:
restart: always
build:
context: .
dockerfile: ./Dockerfile
image: prod_erybj_service
container_name: prod_erybjp_service
ports:
- "6851:6851"
command: python manage.py runserver -h 0.0.0.0 -p 6851
redis:
image: redis:latest
container_name: prod_erybjp_redis
ports:
- "6379:6379"
networks:
service:
aliases:
- redis
selenium-hub:
restart: always
image: selenium/hub:latest
container_name: selenium-hub
ports:
- "4444:4444"
depends_on:
- python
volumes:
- /opt/conf/:/mnt
- /etc/localtime:/etc/localtime
environment:
- TZ=Asia/Shanghai
- GRID_MAX_SESSION=10
chrome:
restart: always
image: selenium/node-chrome-debug:latest
container_name: prod_erybjp_chrome
ports:
- "5901:5555"
volumes:
- /dev/shm:/dev/shm
- /etc/localtime:/etc/localtime
depends_on:
- selenium-hub
shm_size: 2g
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
- NODE_MAX_INSTANCES=5
- NODE_MAX_SESSION=5
- START_XVFB=false
- TZ=Asia/Shanghai
firefox:
image: selenium/node-firefox-debug:latest
container_name: prod_erybjp_firefox
volumes:
- /dev/shm:/dev/shm
- /etc/localtime:/etc/localtime
ports:
- "5902:5555"
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
- NODE_MAX_INSTANCES=5
- NODE_MAX_SESSION=5
- START_XVFB=false
- TZ=Asia/Shanghai
networks:
service:
参考下这个: https://blog.csdn.net/qq_41522024/article/details/126322550
zalenium : https://www.cnblogs.com/fnng/p/8439359.html
zalenium问题解决 https://www.jianshu.com/p/86de1cfe44fe