Docker CI: Grid Server 安装与配置

一、概述

基于 Docker 集成 CI 环境。涉及技术:Linux(Ubuntu 14.04), Docker, Jenkins, Git/Gitlab, Web/Httpbin, Python/Pytest, UI/Selenium, Robotframework, Grid Server, Appium 等。

架构图如下:

在这里插入图片描述

二、Docker 平台(Ubuntu 14.04):安装 Grid Server

SSH 登陆 Docker 服务器,pull selenium/hub 和 selenium/node-chrome 等镜像(其他浏览器类似)

# docker pull selenium/hub
# docker pull selenium/node-chrome
# docker images
REPOSITORY                       TAG                 IMAGE ID            CREATED             SIZE
docker.io/selenium/node-chrome   latest              729577b4908d        3 days ago          917 MB
docker.io/selenium/hub           latest              4493038e8e4f        3 days ago          310 MB

三、Docker 平台(Ubuntu 14.04):配置 Grid Server

  1. SSH 登陆 Docker 服务器,运行 selenium/hub 容器:
    --name: 容器名
    --restart : 自动启动
    -d: daemon 守护进程
    -p: publlish 端口,5555 是 selenium/hub 网页端口
# docker run --name hub --restart always -p 5555:4444 -d selenium/hub
# docker logs hub
2018-11-06 03:52:19,956 WARN Included extra file "/etc/supervisor/conf.d/selenium-hub.conf" during parsing
2018-11-06 03:52:19,957 INFO supervisord started with pid 7
2018-11-06 03:52:20,959 INFO spawned: 'selenium-hub' with pid 10
Starting Selenium Hub with configuration:
2018-11-06 03:52:20,969 INFO success: selenium-hub entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
{
  "host": "0.0.0.0",
  "port": 4444,
  "role": "hub",
  "maxSession": 5,
  "newSessionWaitTimeout": -1,
  "capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
  "throwOnCapabilityNotPresent": true,
  "jettyMaxThreads": -1,
  "cleanUpCycle": 5000,
  "browserTimeout": 0,
  "timeout": 1800,
  "debug": false
}
03:52:21.479 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.0, revision: 2ecb7d9a
03:52:21.568 INFO [GridLauncherV3.lambda$buildLaunchers$5] - Launching Selenium Grid hub on port 4444
2018-11-06 03:52:21.928:INFO::main: Logging initialized @804ms to org.seleniumhq.jetty9.util.log.StdErrLog
03:52:22.077 INFO [Hub.start] - Selenium Grid hub is up and running
03:52:22.078 INFO [Hub.start] - Nodes should register to http://172.17.0.16:4444/grid/register/
03:52:22.078 INFO [Hub.start] - Clients should connect to http://172.17.0.16:4444/wd/hub
  1. 运行 selenium/node-chrome 容器:
# docker run -d --name gc-node --link hub:hub selenium/node-chrome
# docker logs gc-node
2018-11-06 03:53:11,686 WARN Included extra file "/etc/supervisor/conf.d/selenium.conf" during parsing
2018-11-06 03:53:11,687 INFO supervisord started with pid 7
2018-11-06 03:53:12,690 INFO spawned: 'xvfb' with pid 10
2018-11-06 03:53:12,691 INFO spawned: 'selenium-node' with pid 11
03:53:13.475 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.0, revision: 2ecb7d9a
2018-11-06 03:53:13,476 INFO success: xvfb entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
2018-11-06 03:53:13,476 INFO success: selenium-node entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
03:53:13.578 INFO [GridLauncherV3.lambda$buildLaunchers$7] - Launching a Selenium Grid node on port 5555
2018-11-06 03:53:13.668:INFO::main: Logging initialized @421ms to org.seleniumhq.jetty9.util.log.StdErrLog
03:53:13.883 INFO [WebDriverServlet.<init>] - Initialising WebDriverServlet
03:53:13.955 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 5555
03:53:13.956 INFO [GridLauncherV3.lambda$buildLaunchers$7] - Selenium Grid node is up and ready to register to the hub
03:53:14.020 INFO [SelfRegisteringRemote$1.run] - Starting auto registration thread. Will try to register every 5000 ms.
03:53:14.423 INFO [SelfRegisteringRemote.registerToHub] - Registering the node to the hub: http://172.17.0.16:4444/grid/register
03:53:14.479 INFO [SelfRegisteringRemote.registerToHub] - The node is registered to the hub and ready to use
# docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                                           NAMES
14879364323f        selenium/node-chrome   "/opt/bin/entry_po..."   3 minutes ago       Up 3 minutes                                                        gc-node
e2f758abaae1        selenium/hub           "/opt/bin/entry_po..."   3 minutes ago       Up 3 minutes        0.0.0.0:5555->4444/tcp                          hub

四、Docker 平台(Ubuntu 14.04):Robotframework 框架下,配置 Grid Server

  1. 查看 Grid Server 状态:http://ip:5555/grid/console
    在这里插入图片描述
  2. 配置 Robotframework:
# Setup Grid Server
Set Environment Variable	U_CUSTOME_BROSWER_TYPE	chrome  # browser type
Set Environment Variable	U_GRID_USER_URL	http://ip:5555/wd/hub  # remote_url
Set Environment Variable	U_WEB_DEBUG	    FALSE  # non-debug: headless

# Keyword: GUI_Open_Browser
#Author: Allan								
log	${broswer}							
Close All Browsers								
# 打开首页						
${web_domain}	Set Variable If	'%{U_WEB_TESTING_TPYE}'=='offline'	%{G_PROD_DOMAIN_OFFLINE}	%{G_PROD_DOMAIN_ONLINE}				
${chrome_options}	Evaluate	sys.modules['selenium.webdriver'].ChromeOptions()	sys, selenium.webdriver					
Call Method	${chrome_options}	add_argument	headless					
Call Method	${chrome_options}	add_argument	disable-gpu					
${options}	Run Keyword If	'%{U_WEB_DEBUG}'=='FALSE'	Call Method	${chrome_options}	to_capabilities	ELSE	Set Variable	None
Open Browser	${web_domain}	browser=${broswer}	remote_url=%{U_GRID_USER_URL}	desired_capabilities=${options}				
Run Keyword If	'%{U_WEB_DEBUG}'=='TRUE'	Maximize Browser Window	ELSE	Set Window Size	1680	1050		
Capture Page Screenshot

参考:https://blog.csdn.net/allan_shore_ma/article/details/64549802