Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)

简介:Selenium分布式运行:SeleniumGrid

环境配置:

jenkins:win10,ip:100.100.100.101

selenium-hub:win10,ip:100.100.100.101

selenium-node1:执行机1:win10,ip:111.111.111.111

selenium-node2:执行机2:centos,ip:122.122.122.123

前置条件:

1、hub和node节点电脑互相能ping通

2、安装配置selenium-grid,操作参考简介链接

3、Jenkins安装可参考centos7:docker快速安装jenkins

全部步骤:

一、Jenkins配置selenium插件

二、对应执行机安装配置driver驱动

三、编写Python执行源码

四、Jenkins配置item

五、启动运行

一、Jenkins配置selenium插件:

1、Jenkins需要安装selenium两个插件。

2、配置hub端口4488。

3、开启服务SeleniumGrid服务。

Jenkins需要安装selenium两个插件:进入设置 - 插件管理

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第1张图片

选择可选插件搜索selenium勾选Selenium Capability Axis版本 和 Selenium Plugin:注意首次安装选择的是可选插件,安装完成后重新才是已安装。

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第2张图片

插件安装完成后,主界面菜单就会多出Selenium Grid:

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第3张图片

配置4488端口:界面 - 设置 -系统设置 - selenium - 4488 - 先应用后保存

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第4张图片

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第5张图片

开启服务SeleniumGrid服务:注意端口号改成4488

java -jar selenium-server-standalone-3.141.59.jar -role hub -port 4488

二、对应执行机安装配置driver驱动

1、win10安装配置浏览器驱动driver

2、centos安装配置chrome + chromedriver

win10安装配置浏览器驱动driver:

百度搜索chrome并下载安装。

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第6张图片

查看chrome版本,下载对应chromedriver版本。

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第7张图片

如 97.0.4692.99(正式版本)则下载

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第8张图片

chrome版本和chromedriver版本映射

https://npm.taobao.org/mirrors/chromedriver/

将chromedriver放在Python配置环境变量路径

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第9张图片

开启服务:

java -jar /opt/selenium-server-standalone-3.141.59.jar -role node -port 5555 -hub http://100.100.100.101:4488/grid/register

centos安装配置chrome + chromedriver:
安装浏览器:

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

查看浏览器版本信息:

/usr/bin/google-chrome -version

下载对应Linux版本:如Google Chrome 96.0.4664.45

https://npm.taobao.org/mirrors/chromedriver/

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第10张图片

新建目录并解压下载好的chromedriver_linux64.zip,设置权限,软连接。

mkdir -p /root/webDriver
cd /root/webDriver
wget http://chromedriver.storage.googleapis.com/96.0.4664.45/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
chmod 755 chromedriver
ln -s /root/webDriver/chromedriver /usr/bin/chromedriver

开启服务:

java -jar /opt/selenium-server-standalone-3.141.59.jar -role node -port 6666 -hub http://100.100.100.101:4488/grid/register

打开http://127.0.0.1:4488/grid/console会有相应2个node-grid

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第11张图片

三、编写Python执行源码

# coding=utf-8
from selenium import webdriver
import os
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--headless')
chrome_driver = os.path.abspath("/usr/bin/chromedriver")
os.environ["webdriver.chrome.driver"] = chrome_driver

chrome_capabilities = {
    "browserName": "chrome",  # 浏览器名称
    "version": "",  # 操作系统版本
    "platform": "ANY",  # 平台,这里可以是windows、linux、andriod等等
    "javascriptEnabled": False,  # 是否启用js
    "webdriver.chrome.driver": chrome_driver
}
driver = webdriver.Remote("http://111.111.111.111:5555/wd/hub", desired_capabilities=chrome_capabilities,
                          options=chrome_options)

driver.get("https://www.baidu.com")
print(driver.title)
driver.quit()


driver = webdriver.Remote("http://122.122.122.123:6666/wd/hub", desired_capabilities=chrome_capabilities)
driver.get("https://www.baidu.com")
print("driver.title2:", driver.title)
driver.quit()

注意:selenium.webdriver.remote.webdriver.WebDriver这个类其实是所有其他Webdriver的父类,所以在Linux中因为需要定制化设置chromedriver的options,即:因为Linux服务可能不支持可视化,所以需要增加参数。

其他参数配置参考:

options.add_argument('--disable-infobars')  # 禁止策略化
options.add_argument('--no-sandbox')  # 解决DevToolsActivePort文件不存在的报错
options.add_argument('window-size=1920x3000')  # 指定浏览器分辨率
options.add_argument('--disable-gpu')  # 谷歌文档提到需要加上这个属性来规避bug
options.add_argument('--incognito')  # 隐身模式(无痕模式)
options.add_argument('--disable-javascript')  # 禁用javascript
options.add_argument('--start-maximized')  # 最大化运行(全屏窗口),不设置,取元素会报错
options.add_argument('--disable-infobars')  # 禁用浏览器正在被自动化程序控制的提示
options.add_argument('--hide-scrollbars')  # 隐藏滚动条, 应对一些特殊页面
options.add_argument('blink-settings=imagesEnabled=false')  # 不加载图片, 提升速度
options.add_argument('--headless')  # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败

四、Jenkins配置item

新建一个自由风格item,构建步骤Linux选择Execute shell,win选择ExecuteWindows batch command

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第12张图片

附,如:Windows batch command,激活对应Python虚拟环境,Python运行test2.py, Linux雷同。

cd C:\venv\Scripts
activate.bat  && python C:\test2.py

五、调试运行

点击立即构建

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第13张图片

执行结果:

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第14张图片

以上,通过Jenkins,我们实现了分别在不同操作系统执行web-ui自动化的脚本。

微信公众号:玩转测试开发
欢迎关注,共同进步,谢谢!

Python + Jenkins + Selenium-Grid实现分布式web-ui自动化测试(centos+win10为例)_第15张图片

你可能感兴趣的:(心得体会,Python学习,jenkins,python,centos)