基于Selenium 分布式的自动化测试环境搭建

背景介绍

最近致力于研究Selenium自动化测试(包含RF)筹备,经过B度一番探求后,未找到契合自己需求的部署方案。
另外这是一个拓荒的过程,再没有先例可以借鉴的情况下,只能找些支离破碎的例子拼凑兮尝试矣,故本文旨在阐述该方案的可行性及探索的过程。

硬件配置及组网

  • CI 持续集成环境部署于Linux (192.168.31.72)

  • 自动化测试Server 部署于Linux (192.168.31.147

  • UI 自动化测试执行环境部署于Win10 (192.168.31.192)

  • Testbed部署于Linux(192.168.31.86)

    组网如下图
    测试环境组网

软件配置及流程

序号模块OS名称版本备注
1CI 持续集成LinuxApache Tomcat8.5.47版本无特别要求
2CI 持续集成LinuxJenkins2.190.2版本无特别要求
3Auto-Testing ServerLinuxjava1.8.0_212用于启动Hub
4Auto-Testing ServerLinuxpython3.6.9Ubuntu18.04预安装
5Auto-Testing ServerLinuxselenium3.141.0与UI Test Clilent保持一致
6Auto-Testing ServerLinuxselenium-server-standalone3.141.0与selenium保持一致
7Auto-Testing ServerLinuxrobotframework3.1.2N/A
8Auto-Testing ServerLinuxrobotframework-seleniumlibrary4.2.0N/A
9UI Test ClientWin10Internet Explore11.0.120N/A
10UI Test ClientWin10selenium-server-standalone3.141.0与selenium保持一致
11UI Test ClientWin10java1.8.0_91用于启动Node
12TestBedLinuxnexus3.19.1被测对象
13TestBedLinuxjava1.8.0_212运行nexus需要

在这里插入图片描述

Jenkins Server搭建

下载Apache Tomcat 并上传解压

下载Jenkins.war 包并上传解压

启动及配置jenkins

Auto-Testing Server 搭建

Java 安装及配置

root@ubuntu:~# tar -xzvf jdk-8u212-linux-x64.tar.gz 

vi .profile 加入环境变量

export JAVA_HOME=/root/jdk1.8.0_212/bin
export PATH=$PATH:$JAVA_HOME

环境变量生效

root@ubuntu:~# source ~/.profile

验证环境变量

root@ubuntu:~# java

Python环境调整及验证

  • 初始化的Ubuntu 创建软连接方便调用
root@ubuntu:/usr/bin# ls -trl python*
-rwxr-xr-x 1 root root    1342 May  1  2016 python3-jsonpointer
-rwxr-xr-x 1 root root    3661 Oct 28  2017 python3-jsonpatch
-rwxr-xr-x 1 root root    1018 Oct 28  2017 python3-jsondiff
-rwxr-xr-x 1 root root     398 Nov 15  2017 python3-jsonschema
lrwxrwxrwx 1 root root      17 Oct 25  2018 python3m-config -> python3.6m-config
lrwxrwxrwx 1 root root      10 Oct 25  2018 python3m -> python3.6m
lrwxrwxrwx 1 root root      16 Oct 25  2018 python3-config -> python3.6-config
lrwxrwxrwx 1 root root       9 Oct 25  2018 python3 -> python3.6
lrwxrwxrwx 1 root root      34 Nov  7 10:44 python3.6m-config -> x86_64-linux-gnu-python3.6m-config
-rwxr-xr-x 2 root root 4526456 Nov  7 10:44 python3.6m
lrwxrwxrwx 1 root root      33 Nov  7 10:44 python3.6-config -> x86_64-linux-gnu-python3.6-config
-rwxr-xr-x 2 root root 4526456 Nov  7 10:44 python3.6

创建python软连接

root@ubuntu:/usr/bin# ln -s /usr/bin/python3.6 /usr/bin/python

安装pip包管理工具

root@ubuntu:/usr/bin# apt install python3-pip

创建pip软连接

ln -s /usr/bin/pip3  /usr/bin/pip
  • pip 镜像源配置
root@ubuntu:~# mkdir ~/.pip;cd ~/.pip;vi pip.conf

粘贴如下国内镜像源至pip.conf并保存

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn

Robotframework 自动化测试依赖安装

root@ubuntu:/usr/bin#  pip install selenium
root@ubuntu:/usr/bin# pip install robotframework
root@ubuntu:/usr/bin# pip install robotframework-seleniumlibrary

查看selenium版本

root@ubuntu:/usr/bin# pip show selenium
Name: selenium
Version: 3.141.0
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
License: Apache 2.0
Location: /usr/local/lib/python3.6/dist-packages
Requires: urllib3

selenium-server-standalone下载及安装
相较于浏览器Driver 例如IEDriverServer_xxx.exe,RemoteWebDriver支持控制远程浏览器能力,通过搭配Selenium-Server-standalone的服务,将IE浏览器和测试代码分离,从而实现远程控制浏览器操作。

Testbed搭建

Java 安装及配置

test@kali:~$ tar -xzvf jdk-8u212-linux-x64.tar.gz 

vi .profile 加入环境变量

export JAVA_HOME=/home/test/jdk1.8.0_212/bin
export PATH=$PATH:$JAVA_HOME

环境变量生效

test@kali:~$ source ~/.profile

验证环境变量

test@kali:~$ java

被测对象Nexus安装

test@kali:~$ tar -xzvf nexus-3.19.1-01-unix.tar.gz 
test@kali:~$ cd nexus-3.19.1-01/bin
test@kali:~/nexus-3.19.1-01/bin$ ./nexus start

安装完打开效果如下图:
在这里插入图片描述

UI Test Client 搭建

Java 安装及配置
windows下安装jdk及配置环境变量
(此处不再赘述)

selenium-server-standalone下载及安装

Internet Explore配置

  • IE 设置勾选所有“启动保护模式”
  • IE 确保缩放比例为100%

流程串联

创建RF脚本并上传至Auto-Testing Server
在这里插入图片描述

*** Setting ***
Library    SeleniumLibrary


*** Test Cases ***
OpenUrl
    Open Browser    http://192.168.31.86:8081   Ie  None  http://192.168.31.147:4444/wd/hub
    Close Browser
~

启动Hub
在这里插入图片描述

root@ubuntu:~# java -jar selenium-server-standalone-3.141.0.jar -role hub -port 4444

10:35:52.172 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.0, revision: 2ecb7d9a
10:35:52.285 INFO [GridLauncherV3.lambda$buildLaunchers$5] - Launching Selenium Grid hub on port 4444
2020-02-02 10:35:52.681:INFO::main: Logging initialized @738ms to org.seleniumhq.jetty9.util.log.StdErrLog
10:35:52.898 INFO [Hub.start] - Selenium Grid hub is up and running
10:35:52.899 INFO [Hub.start] - Nodes should register to http://192.168.31.147:4444/grid/register/
10:35:52.900 INFO [Hub.start] - Clients should connect to http://192.168.31.147:4444/wd/hub
10:35:56.641 INFO [DefaultGridRegistry.add] - Registered a node http://192.168.31.192:30500

详细参数另起文档详述

启动Node监听
在这里插入图片描述

D:\>java -jar selenium-server-standalone-3.141.0.jar -role node -hub http://192.168.31.147:4444/grid/register

19:07:33.020 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.0, revision: 2ecb7d9a
19:07:33.099 INFO [GridLauncherV3.lambda$buildLaunchers$7] - Launching a Selenium Grid node on port 28330
2020-02-02 19:07:33.356:INFO::main: Logging initialized @502ms to org.seleniumhq.jetty9.util.log.StdErrLog
19:07:33.502 INFO [WebDriverServlet.<init>] - Initialising WebDriverServlet
19:07:33.544 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 28330
19:07:33.545 INFO [GridLauncherV3.lambda$buildLaunchers$7] - Selenium Grid node is up and ready to register to the hub
19:07:33.644 INFO [SelfRegisteringRemote$1.run] - Starting auto registration thread. Will try to register every 5000 ms.
19:07:33.780 INFO [SelfRegisteringRemote.registerToHub] - Registering the node to the hub: http://192.168.31.147:4444/grid/register
19:07:33.792 INFO [SelfRegisteringRemote.registerToHub] - The node is registered to the hub and ready to use

详细参数另起文档详述

执行RF脚本

root@ubuntu:~# robot LoginNexus.robot 
==============================================================================
LoginNexus                                                                    
==============================================================================
OpenUrl                                                               | PASS |
------------------------------------------------------------------------------
LoginNexus                                                            | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  /root/output.xml
Log:     /root/log.html
Report:  /root/report.html

结果展示

19:13:21.321 INFO [SelfRegisteringRemote$1.run] - Starting auto registration thread. Will try to register every 5000 ms.
19:13:21.692 INFO [SelfRegisteringRemote.registerToHub] - Registering the node to the hub: http://192.168.31.147:4444/grid/register
19:13:21.780 INFO [SelfRegisteringRemote.registerToHub] - The node is registered to the hub and ready to use
19:13:28.545 INFO [ActiveSessionFactory.apply] - Capabilities are: {
  "browserName": "internet explorer",
  "platform": "WINDOWS",
  "version": ""
}
19:13:28.548 INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.grid.session.remote.ServicedSession$Factory (provider: org.openqa.selenium.ie.InternetExplorerDriverService)
Started InternetExplorerDriver server (64-bit)
3.14.0.0
Listening on port 44178
Only local connections are allowed
19:13:29.922 INFO [ProtocolHandshake.createSession] - Detected dialect: W3C
19:13:29.945 INFO [RemoteSession$Factory.lambda$performHandshake$0] - Started new session 6447f3fd-14a9-4823-8b0e-5e3370bc2a80 (org.openqa.selenium.ie.InternetExplorerDriverService)
19:13:31.997 INFO [ActiveSessions$1.onStop] - Removing session 6447f3fd-14a9-4823-8b0e-5e3370bc2a80 (org.openqa.selenium.ie.InternetExplorerDriverService)

在这里插入图片描述
[1]: https://softwaretester.info/robot-framework-selenium-grid-2-0/
[2]: https://stackoverflow.com/questions/29369339/robot-framework-selenium-standalone-only-java-installed
[3]: https://stackoverflow.com/questions/48682708/selenium-standalone-server
[4]:https://www.jianshu.com/p/05784d35ee34

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值