基于Docker Selenium Grid 搭建分布式测试环境

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Docker Selenium Grid 搭建分布式测试环境相关的知识,希望对你有一定的参考价值。

参考技术A

分布式selenium grid 搭建方法:
https://testdriven.io/blog/distributed-testing-with-selenium-grid/

https://blog.csdn.net/ldq_sd/article/details/109893462?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link

https://blog.csdn.net/liwei_721/article/details/105109798

SeleniumHQ/docker-selenium github code:
https://github.com/SeleniumHQ/docker-selenium

基于Docker compose Selenium Grid 搭建分布式测试环境
https://techblog.dotdash.com/setting-up-a-selenium-grid-with-docker-containers-for-running-automation-tests-c43aceccd5d9
https://nitinbhardwaj6.medium.com/selenium-grid-with-debug-docker-containers-23eceb6ab17f

Docker Selenium Grid Setup:增加 node number and session number
https://dev.to/automationbro/docker-selenium-grid-setup-1lg4

So far we just have 1 instance of Chrome & Firefox, however, if you need to run multiple tests together, you\'ll need more instances spun up. You can do that quite easily by adding the parameters when running the docker container for Chrome and Firefox.

docker run -d --net grid -e HUB_HOST=selenium-hub -e NODE_MAX_INSTANCES=3 -e NODE_MAX_SESSION=3 -v /dev/shm:/dev/shm selenium/node-chrome-debug:3.141.59–20210422

You can pass NODE_MAX_INSTANCES and NODE_MAX_SESSION environment variables to add multiple instances of the browsers.

Once you do that, you will see something like this below :

https://testdriven.io/blog/distributed-testing-with-selenium-grid/

腾讯官网的doc:
https://cloud.tencent.com/developer/article/1634286

如何安装制定版本额docker-compose
1.查找到响应版本
https://github.com/docker/compose/releases
2.安装的cmd
https://docs.docker.com/compose/install/

清除(关闭全部容器) :docker kill (docker ps -a -q)
再次查看容器情况,运行:docker ps –a 发现整个世界都清净了。

清除(关闭全部容器) :docker kill (docker ps -a | grep selenium)
再次查看容器情况,运行:docker ps –a 发现整个世界都清净了。

Selenium Grid and Docker:
https://nazarkhimin.medium.com/selenium-grid-and-docker-25a79f0b9007

如何使用docker-compose mount:
https://code.visualstudio.com/remote/advancedcontainers/add-local-file-mount

If you\'ve already built the container and connected to it, run Remote-Containers: Rebuild Container from the Command Palette (F1) to pick up the change. Otherwise run Remote-Containers: Open Folder in Container... to connect to the container.

设置proxy in the container:
https://stackoverflow.com/questions/50149133/how-can-i-setup-a-proxy-in-a-selenium-chrome-container

如何使用K8S 搭建分布式 selenium:
https://github.com/alcounit/selenosis/issues/47
https://github.com/alcounit/selenosis#run-yout-tests

如何使用工具portainer监控host主机的container 状态:
https://levelup.gitconnected.com/portainer-the-easy-way-to-manage-docker-a982a17146c1

zalenium(基于selenium grid 的扩展)

zalenium的安装

1、前置条件

  • Docker version >= 1.11.1(可能适用于低版本,没有测试过)
  • 运行 docker info不会报错

2、下载镜像
docker pull elgalu/selenium
docker pull dosel/zalenium

3、运行命令(默认只启动一个chrome container和一个Firefox container,如需启动多个则运行(添加参数--desiredContainers 5))
docker run --rm -ti --name zalenium -p 4444:4444 ---第一个4444 端口是外部访问的端口, 第二个4444端口是容器里面的端口
-e PULL_SELENIUM_IMAGE=true
-v /var/run/docker.sock:/var/run/docker.sock
-v /tmp/videos:/home/seluser/videos
--privileged dosel/zalenium start --maxDockerSeleniumContainers 5 --timeZone "Asia/Shanghai"
4、停止命令
docker stop zalenium

5、准备测试脚本

#grid_demo.py

from selenium import webdriver
from time import sleep

driver = webdriver.Remote(
command_executor=‘http://127.0.0.1:4444/wd/hub‘,
desired_capabilities={‘browserName‘: ‘chrome‘,‘idleTimeout‘: 90,})

driver.set_window_size(1920, 1080)
driver.get(‘https://www.baidu.com‘)
driver.find_element_by_id("kw").send_keys("docker selenium")
driver.find_element_by_id("su").click()
sleep(15)
driver.quit()
现在可以运行测试了,通过 http://localhost:4444/wd/hub 主节点。

6、运行脚本
$ python3 grid_demo.py

7、仪表板
http://172.22.6.113:4444/dashboard/# 视频面板

8、显示实时预览 localhost 为服务器IP
http://172.22.6.113:4444/grid/admin/live?refresh =20 实施浏览
http://localhost:4444/grid/admin/live?refresh=20 显示实时预览20秒刷新














以上是关于基于Docker Selenium Grid 搭建分布式测试环境的主要内容,如果未能解决你的问题,请参考以下文章

Docker Selenium Grid 全量集成测试环境搭建和负载均衡

Docker Selenium Grid 全量集成测试环境搭建和负载均衡

基于Docker+Selenium Grid的测试技术应用

测试人员如何搭建Selenium-Grid2环境(参考Java)

Selenium Grid Docker-通过 Jenkins 实现自动化

Selenium grid 分布式测试搭建