说明
- 主要记录centos7+docker+zalenium+python分布式执行用例
- 它使用docker-selenium在本地运行基于Firefox和Chrome的测试,同样带有视频录制,实时预览,基本认证和仪表盘等功能
安装docker
- 输入下面命令安装
sudo yum install epel-release
sudo yum install docker
- 查看是否安装成功
[root@racknerd-4dbd89 ~]# docker --version
Docker version 1.13.1, build 0be3e21/1.13.1
[root@racknerd-4dbd89 ~]#
- 设置为开启启动
service docker start
镜像设置
- 下载镜像
docker pull elgalu/selenium
docker pull dosel/zalenium
- 下载成功后运行:
docker run --rm -ti --name zalenium -p 4444:4444 -v /var/run/docker.sock:/var/run/docker.sock -v /temp/videos:/home/seluser/videos --privileged dosel/zalenium start
- 默认只启动一个chrome container和一个Firefox container,如需启动多个则运行(添加参数--desiredContainers 4):
docker run --rm -ti --name zalenium -p 4444:4444 -v /var/run/docker.sock:/var/run/docker.sock -v /temp/videos:/home/seluser/videos --privileged dosel/zalenium start --desiredContainers 4
- 等待zalenium准备,调用:http://localhost:4444/wd/hub/status 你将会看到类似如下返回值:
{
"status": 0,
"value": {
"ready": true,
"message": "Hub has capacity",
"build": {
"revision": "unknown",
"time": "unknown",
"version": "3.141.59"
},
"os": {
"arch": "amd64",
"name": "Linux",
"version": "3.10.0-1160.21.1.el7.x86_64"
},
"java": {
"version": "1.8.0_242"
}
}
}
可以增加?refresh=numberOfSeconds`,自动刷新页面例如:http://localhost:4444/grid/admin/live?refresh=20
查看单个构建,例如:http://localhost:4444/grid/admin/live?build=myTestBuild或者可以用Docker Compose启动zalenium,docker-compose.yml样例:
# Usage:
# docker-compose up --force-recreate
version: '2.1'
services:
#--------------#
zalenium:
image: "dosel/zalenium"
container_name: zalenium
hostname: zalenium
tty: true
volumes:
- /tmp/videos:/home/seluser/videos
- /var/run/docker.sock:/var/run/docker.sock
# - /usr/bin/docker:/usr/bin/docker
ports:
- 4444:4444
command: >
start --desiredContainers 2
--maxDockerSeleniumContainers 8
--screenWidth 800 --screenHeight 600
--timeZone "Europe/Berlin"
--videoRecordingEnabled true
--sauceLabsEnabled false
--browserStackEnabled false
--testingBotEnabled false
--cbtEnabled false
--startTunnel false
environment:
- HOST_UID
- HOST_GID
- 创建
docker-compose.yml
文件后,该文件目录下运行:docker-compose up
即可看到上文描述的结果。
python环境配置
- 测试配置选项
caps = {'platform': 'ANY',
'browserName': "chrome",
'version': '',
# 是否录制视频
'recordVideo': True,
# build 名字
'build': "my_build_name123",
# 测试名称
'name': 'my_test_name123',
# 超时时间
'idleTimeout': 90,
# 分辨率
'screenResolution': '1280x720',
# 时区
# 'tz': 'Asia/Shanghai',
# 视频文件名称
'testFileNameTemplate': '{platform}_{browser}_{testStatus}_{timestamp}',
}
# 标记测试成功、失败
driver.add_cookie({"name": "zaleniumTestPassed", "value": "true"})
# 每一步在视频中添加注释
driver.add_cookie({"name": "zaleniumMessage", "value": "go to baidu page"})
本地运行环境
- python3
pip insatll -U selenium
- 本地代码
# -*- coding: utf-8 -*-
import unittest
from selenium import webdriver
class MyTestCase(unittest.TestCase):
def setUp(self):
host = "xxxxxx.201"
ds = {'platform': 'ANY',
'browserName': "chrome",
'version': '',
'recordVideo': True,
'build': "my_build_name123",
'name': 'my_test_name123',
'idleTimeout': 90,
'screenResolution': '1280x720',
# 'tz': 'Asia/Shanghai',
'testFileNameTemplate': '{platform}_{browser}_{testStatus}_{timestamp}',
}
self.dr = webdriver.Remote('http://%s:4444/wd/hub' % host, desired_capabilities=ds)
def test_something(self):
self.dr.get("https://www.baidu.com")
self.dr.add_cookie({"name": "zaleniumMessage", "value": "go to baidu page12"})
self.assertEqual(self.dr.name, "chrome")
def test_search_button(self):
self.dr.get("https://www.baidu.com")
self.dr.add_cookie({"name": "zaleniumMessage", "value": "go to baidu page"})
self.dr.add_cookie({"name": "zaleniumMessage", "value": "input keyword"})
self.dr.find_element_by_id("kw").send_keys("zalenium")
self.dr.add_cookie({"name": "zaleniumMessage", "value": "click search button"})
self.dr.find_element_by_id("su").click()
self.assertTrue(self.dr.find_element_by_id("su").is_displayed())
def tearDown(self):
print(self.dr.get_cookies())
self.dr.add_cookie({"name": "zaleniumTestPassed", "value": "true"})
self.dr.quit()
if __name__ == '__main__':
unittest.main()
- 可以直接调用远程服务器上的浏览器,执行用例,然后,http://host:4444/dashboard/#,就能看到执行结果,并且有视频录制
说明
- 官方声明此项目已经停止更新,但是并不影响使用