什么,我们拼错了,是selenium? 哈哈,没有,今天要讲的就是zalenium。
主要内容
什么是zalenium
如何配置zalenium
Cucumber脚本连接zalenium
什么是zalenium?
zalenium一个灵活且可扩展的基于容器的Selenium Grid,具有视频录制、实时预览、用户认证和仪表板功能。
我们知道,通过Selenium Grid,可以提供按需获取的自动化运行环境,比如:
从Grid中选取所需的浏览器类型和版本来运行自动化脚本,验证在特定浏览器下的Web应用的功能。
或者同时操作批量的浏览器执行自动化任务,模拟真实大量浏览器访问Web应用对服务器的压力。
Selenium Grid有多种用途,但搭建稳定好用的Selenium Grid却有一定的复杂性。docker技术的引入方便了Selenium Grid的搭建。而zalenium就是一种基于 docker-selenium的解决方案,可以动态扩展基于Firefox 和 Chrome 浏览器的自动化测试脚本,让UI测试运行得更快,因为它们运行在本地网格上。另外可以从头创建的干净节点,在之上运行测试,并且在完成后被废弃。
使用zalenium的优势:
提供稳定的Selenium Grid环境
保持最新的浏览器版本
覆盖所有平台和浏览器
在自动化测试中使用zalenium的可以协助解决同学们经常遇到的多浏览器测试问题。
官方参考地址:
https://opensource.zalando.com/zalenium
如何配置zalenium
下面在Windows10上使用 Virtual Box安装Centos7最新虚拟机演示相关环境的配置。
相关软件下载地址:
· VirtualBox:
https://www.virtualbox.org
· Centos 7:
http://isoredirect.centos.org/centos/7/isos/x86_64
配置虚拟机网络
1.配置Windows网络共享
打开网络连接,启用共享网络给VirtualBox
2.配置 VirtualBox【管理】--【网络操作管理器】
打开【属性】--【手动配置网卡】
配置DHCP服务器
3.设置网卡
点击CentOS7 设置,找到网络选项,配置网卡1
虚拟机为关闭状态,配置网卡2
4.编辑CentOS7 网络配置文件
启动虚拟机CentOS 7,
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
修改字段为
BOOTPROTO=dhcpONBOOT=yes
5.重启网络
systemctl restart network
6.查看IP地址
[root@localhost ~]# ip addr1: lo: mtu65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lftforever inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: enp0s3: mtu 1500 qdisc pfifo_fast state UPgroup default qlen 1000 link/ether 08:00:27:b9:50:41 brd ff:ff:ff:ff:ff:ff inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamicenp0s3 valid_lft 86396sec preferred_lft 86396sec inet6 fe80::aa08:3920:8067:9b36/64 scope link noprefixroute valid_lft forever preferred_lft forever3: enp0s8: mtu 1500 qdisc pfifo_fast state UPgroup default qlen 1000 link/ether 08:00:27:e0:80:f4 brd ff:ff:ff:ff:ff:ff inet 192.168.56.101/24 brd 192.168.56.255 scope global noprefixroutedynamic enp0s8 valid_lft 879sec preferred_lft 879sec inet6 fe80::3008:cae4:d9b0:1bed/64 scope link noprefixroute valid_lft forever preferred_lft forever
看到ip 地址为 192.168.56.101
7.测试网络
root@localhost ~]# ping www.baidu.comPING www.baidu.com (180.101.49.12)56(84) bytes of data.64 bytes from www.baidu.com(180.101.49.12): icmp_seq=1 ttl=51 time=11.6 ms64 bytes from www.baidu.com(180.101.49.12): icmp_seq=2 ttl=51 time=12.7 ms
安装Docker
可以参考Docker官网:
https://docs.docker.com/install/linux/docker-ce/centos
zalenium需要你的docker版本大于等于1.11.1,如果不满足,请卸载旧版本docker,安装一个更新的。下面介绍简单的Docker安装步骤。
1.卸载旧版本docker
$ sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
2.安装必要工具
$ sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
3.配置docker-ce源
$ sudo yum-config-manager --add-repohttp://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
4.安装 Docker-ce
$ sudo yum install -y docker-cedocker-ce-cli containerd.io
5.启动Docker 服务
$ sudo systemctl start docker
6.验证docker是否安装成功
$ docker run hello-worldUnable to find image'hello-world:latest' locallylatest: Pulling from library/hello-world1b930d010525: Pull completeDigest:sha256:b8ba256769a0ac28dd126d584e0a2011cd2877f3f76e093a7ae560f2a5301c00Status: Downloaded newer image forhello-world:latestHello from Docker!This message shows that yourinstallation appears to be working correctly.To generate this message, Docker tookthe following steps: 1. The Docker client contacted the Dockerdaemon. 2. The Docker daemon pulled the"hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new containerfrom that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output tothe Docker client, which sent it to your terminal.To try something more ambitious, you canrun an Ubuntu container with: $docker run -it ubuntu bashShare images, automate workflows, andmore with a free Docker ID: https://hub.docker.com/For more examples and ideas, visit: https://docs.docker.com/get-started/
安装并启动Zalenium
# Pull docker-seleniumdocker pull elgalu/selenium# Pull Zaleniumdocker pull dosel/zalenium# Run it!docker run --rm -ti --name zalenium -p 4444:4444\ -v /var/run/docker.sock:/var/run/docker.sock \ -v /tmp/videos:/home/seluser/videos \ --privileged dosel/zalenium start
Cucumber脚本结合
现在zalenium设置好了,就可以用来执行自动化测试了。现在创建一个简单的自动化测试脚本并使用zalenium。
打开CukeTest (可从http://cuketest.com获得),新建项目,选择项目模板【Web】
接下来在该目录下执行“npm install”安装项目运行依赖库。
配置驱动
修改模板中support\web_driver.js文件,添加语句“usingServer('http://192.168.56.101:4444/wd/hub')”。这条语句让Selenium使用Selenium Grid中的浏览器。
const webDriver= require('selenium-webdriver');//根据您的浏览器配置创建WebDriver实例;function createDriver() { let browserConfig = process.env.BROWSER || 'chrome'; let browser = browserConfig.toLowerCase(); if (['chrome', 'firefox',].indexOf(browser) <0) browser='chrome'; //default to chrome return new webDriver.Builder() .usingServer('http://192.168.56.101:4444/wd/hub') .forBrowser(browser) .build();}exports.driver= createDriver();
因为从模板创建的脚本已包含访问www.bing.com的代码,所以我们可以直接执行测试脚本。
运行项目
因为运行是在zalenium中的浏览器中,不会在本地看到自动化的浏览器。为查看实时执行情况,可以打开 http://192.168.56.101:4444/grid/admin/live
点击【运行项目】按钮,可以远程查看执行中的浏览器窗口。