Docker实践--搭建分布式UI测试环境

  背景:项目需要在chrome firefox不同版本下做UI自动化测试

  现状:单台机器只能安装一个版本浏览器;多台电脑协同太麻烦;

  解决方案:通过Docker简化Selenium Grid分布式测试的部署

  1.Grid介绍

  

  

  web端的自动化测试呈现一家独大的状态,大部分都在使用selenium完成,也是各大浏览器官方支持的工具,应用非常普遍。

  以传统的方式部署分布式Selenium Grid集群需要耗费大量时间和机器成本来准备测试环境。比如为了针对不同版本的Chrome进行测试,需要将指定版本的Chrome浏览器安装到不同物理机或虚拟机上。

  

  2.Docker部署

  2.1 Docker环境搭建

  本机使用windows7 64Bit 通过DockerToolbox安装  宿主机基于linux

  安装完成初始化比较慢、需要FQ更快

  2.2  安装images 

  Hub 安装   chrome+firefox不同版本安装

  baozhida/selenium-hub  https://hub.docker.com/r/baozhida/selenium-hub/

  baozhida/selenium-node-firefox-debug https://hub.docker.com/r/baozhida/selenium-node-firefox-debug/

  baozhida/selenium-node-chrome-debug https://hub.docker.com/r/baozhida/selenium-node-chrome-debug/ 

  suyunrong/selenium-node-chrome-debug https://hub.docker.com/r/suyunrong/selenium-node-chrome-debug/tags/

  安装命令

  docker pull baozhida/selenium-hub:3.3.1

  docker pull baozhida/selenium-node-chrome-debug:48

  docker pull baozhida/selenium-node-chrome-debug:58

  docker pull baozhida/selenium-node-firefox-debug:47

  docker pull baozhida/selenium-node-firefox-debug:52

  docker pull suyunrong/selenium-node-chrome-debug:66.0.3359.170

  注1:debug版本可以通过VNC工具登录看到远程桌面

  注2:可以自由下载其它版本镜像

  查看安装成功 docker images

  REPOSITORY                                        TAG                    IMAGE ID            CREATED             SIZE

  suyunrong/selenium-node-chrome-debug  66.0.3359.170    7bccb076f06a    3 months ago 889MB
  baozhida/selenium-node-chrome-debug    58        36863bdc2bbf    15 months ago 923MB
  baozhida/selenium-node-firefox-debug   47          2792d74ddc5c    15 months ago 729MB
  baozhida/selenium-node-firefox-debug      52            6952015b6de6    15 months ago 736MB
  baozhida/selenium-hub           3.3.1       8bbca693c0c0       15 months ago 394MB

  REPOSITORY  代表镜像仓库名 

  IMAGE ID  代表镜像ID

  TAG    代表版本号 latest--最新版本 一般不标准表示最新版本

  

  3.创建运行容器

  创建selenium hub容器
  docker run -d -p 4444:4444 --name selehub baozhida/selenium-hub:3.3.1


  创建chrome node容器
  docker run -d -p 5901:5900 --name gc48 --link selehub:hub --shm-size=512m baozhida/selenium-node-chrome-debug:48
  docker run -d -p 5902:5900 --name gc58 --link selehub:hub --shm-size=512m baozhida/selenium-node-chrome-debug:58
  docker run -d -p 5903:5900 --name gc66 --link selehub:hub --shm-size=512m suyunrong/selenium-node-chrome-debug:66.0.3359.170
  docker run -d -p 5904:5900 --name gc65 --link selehub:hub --shm-size=512m suyunrong/selenium-node-chrome-debug:65.0.3325.162

  创建firefox node容器
  docker run -d -p 5911:5900 --name ff47 --link selehub:hub --shm-size=512m baozhida/selenium-node-firefox-debug:47
  docker run -d -p 5912:5900 --name ff52 --link selehub:hub --shm-size=512m baozhida/selenium-node-firefox-debug:52

  -d 代表后台模式运行

  -p 代表端口映射 5901:5900 容器的5900端口映射到docker的5901端口 访问docker 5901端口可访问容器

  --name 代表 容器名字 可自定义 不设置系统自动分配

  --shm-size参数:docker默认的共享内存/dev/shm只有64m,有时导致chrome崩溃,该参数增加共享内存大小到512m.*

  --link 绑定到某个容器

  注:端口定义chrome nod [5901---] firefox nod[5911--]

  

  查看在运行容器

  docker ps

  

  NAMES 容器运行起来后名字

  PORTS 5912:5900 容器端口5900 docker端口5912

  CONTAINER ID 容器运行之后生成唯一的id 一个容器可以运行N次生成N个ID

  在浏览器输入地址http://192.168.99.100:4444/grid/console
  查看Selenium Grid控制台,能看到刚创建的容器已经正常注册。

  

  4.VNC远程浏览器环境

  66为例

  输入192.168.99.100:5903 回车 输入密码secret 

  5.Python初始化Driver 并发执行

  chrome_driver = os.path.abspath(r"D:Python27")
  os.environ["webdriver.chrome.driver"] = chrome_driver
  chrome_capabilities = {
"browserName": "chrome",
"version": "58.0.3029.81",
"platform": "ANY",
"javascriptEnabled": True,
"webdriver.chrome.driver": chrome_driver
  }
  
  firfox_capabilities = {
"browserName": "firefox",
"version": "47.0",
"platform": "ANY",
"javascriptEnabled": True
  }
  dr = webdriver.Remote(command_executor ="http://192.168.99.100:4444/wd/hub", desired_capabilities = chrome_capabilities)


  --chrome_driver 指定驱动路径chromedriver.exe
  --firefox driver 低版本不需要驱动
  --firefox 48版本以上需要驱动
 
  firfox_driver = os.path.abspath(r"D:Python27")
  os.environ["webdriver.gecko.driver"] = firfox_driver
  firfox_capabilities = {
"browserName": "firefox",
"version": "61.0.1",
"platform": "ANY",
"javascriptEnabled": True,
"webdriver.gecko.driver": firfox_driver
  }
  使用线程实现并发同时调用 (后续可以借助单元测试框架实现报告)
  


  

 

原文地址:https://www.cnblogs.com/aeip/p/9480099.html