本文对Selenium Grid进行了完整的介绍,从环境准备到使用Selenium Grid进行一次完整的多节点分布式测试。
运行环境为Windows 10,Selenium版本为 3.5.0,Chrome版本为62,Firefox版本为56,IE版本为11。

1. Selenium Grid简介

Selenium Grid允许你在多台机器的多个浏览器上并行的进行测试,即分布式测试。
通常,以下两种情况会需要使用Selenium Grid:
1) 通常多个浏览器的兼容性测试,即在不同浏览器或不同操作系统的浏览器中进行测试
2) 测试用例较多时,可以通过分布式测试减少测试执行时间
 

2. Selenium Grid结构

Selenium Grid由一个中心hub及多个节点node组成(类似于其他分布式系统的matser-slave),其中hub用来管理各个node的注册和状态信息,并且接受远程客户端代码的请求调用,然后把请求再转发给node来执行。

 

3. 环境准备

由于Selenium Grid的hub及node启动时需要java环境,所以首先需要安装JDK。
 

3.1 JDK环境

2. 选择Java SE 8u151/ 8u152,点击JDK下载
3. 安装下载的JDK
4. 配置系统环境变量
 

3.2 selenium-server-standalone下载

2. 下载与本机selenium 3.5.0匹配的版本:selenium-server-standalone-3.5.0.jar
 
2. 拷贝selenium-server-standalone-3.5.0.jar至本地工作目录下,如D:\grid

3.3 webdriver下载

3.3.1 IE

2) 下载与selenium版本、系统均匹配的IEDriver版本:IEDriverServer_Win32_3.5.0.zip

3.3.2 Chrome

2) 下载与系统及浏览器版本匹配的driver版本:chromedriver_win32.zip

3.3.3 Firefox

2) 下载与系统及浏览器版本匹配的driver版本:geckodriver-v0.19.1-win64.zip
 

driver下载完成解压后,分别拷贝IEDriverServer.exe,geckodriver.exe,chromedriver.exe至Python安装路径(如C:\Python27\)下即可

上述selenium-server-standalone 及webdriver,我已上传至百度网盘,下载链接: https://pan.baidu.com/s/1i4MBpXF 密码: ygdy

4. Selenium Grid启动

4.1 启动hub

hub启动命令如下:
java -jar selenium-server-standalone-3.5..jar -role hub
其中 -role指定角色为hub,通过下图可以看到:hub已使用默认4444端口启动成功,且node可以通过http://localhost:4444/grid/register/进行注册
 

4.2 启动node

node启动命令如下:
java -jar selenium-server-standalone-3.5..jar -role node -port  -hub http://localhost:4444/grid/register
其中 -role指定角色为node, -port指定端口为 5555, -hub指定连接hub地址,通过下图可以看到node已成功连接hub

同理我们另外启动两个 node(使用端口号分别为5556/5557):

java -jar selenium-server-standalone-3.5..jar -role node -port  -hub http://localhost:4444/grid/register
java -jar selenium-server-standalone-3.5..jar -role node -port -hub http://localhost:4444/grid/register
 
此时打开页面http://localhost:4444/grid/console,可以看到我们启动的三个node:

4.3 更好的启动方法 — bat脚本

1. 新建一个文件selenium_grid.bat,写入我们刚才启动hub及node的命令:
cd /d D:\grid
start java -jar selenium-server-standalone-3.5..jar -role hub
start java -jar selenium-server-standalone-3.5..jar -role node -port -hub http://localhost:4444/grid/register
start java -jar selenium-server-standalone-3.5..jar -role node -port -hub http://localhost:4444/grid/register
start java -jar selenium-server-standalone-3.5..jar -role node -port -hub http://localhost:4444/grid/register
1) 第一行是进入并修改当前目录为存放 selenium-server-standalone-3.5.0.jar的目录:D:\grid
2) 后面三行stat java...是分别打开新的cmd窗口用以启动hub及node
 
2. 双击selenium_grid.bat即可启动hub及node
 

4.4 更多选项

关于Selenium Grid更多命令选项,可运行--help查看:
d:\grid>java -jar selenium-server-standalone-3.5..jar --help
Usage: <main class> [options]
Options:
--version, -version
Displays the version and exits.
Default: false
-browserTimeout
<Integer> in seconds : number of seconds a browser session is allowed to
hang while a WebDriver command is running (example: driver.get(url)). If the
timeout is reached while a WebDriver command is still processing, the session
will quit. Minimum value is . An unspecified, zero, or negative value means
wait indefinitely.
Default:
-debug
<Boolean> : enables LogLevel.FINE.
Default: false
-enablePassThrough
<Boolean>: Whether or not to use the experimental passthrough mode.
Defaults to true.
Default: true
-jettyThreads, -jettyMaxThreads
<Integer> : max number of threads for Jetty. An unspecified, zero, or
negative value means the Jetty default value () will be used.
-log
<String> filename : the filename to use for logging. If omitted, will log
to STDOUT
-port
<Integer> : the port number the server will use.
Default:
-role
<String> options are [hub], [node], or [standalone].
Default: standalone
-timeout, -sessionTimeout
<Integer> in seconds : Specifies the timeout before the server
automatically kills a session that hasn't had any activity in the last X seconds. The
test slot will then be released for another test to use. This is typically
used to take care of client crashes. For grid hub/node roles, cleanUpCycle
must also be set.
Default:

5. Selenium Grid 分布式测试脚本

下面,我们编写一个Selenium Grid自动化测试脚本,分别在3个node上运行Chrome,Firefox及IE浏览器,执行WEB页面自动化测试。
示例脚本如下:
# coding:utf-

from selenium.webdriver import Remote
import time # 定义node_hub与浏览器对应关系
nodes = {
'http://127.0.0.1:5555/wd/hub': 'chrome',
'http://127.0.0.1:5556/wd/hub': 'internet explorer',
'http://127.0.0.1:5557/wd/hub': 'firefox'
} # 通过不同的浏览器执行测试脚本
for host, browser in nodes.items():
print(host, browser)
# 调用remote方法
driver = Remote(command_executor=host,
desired_capabilities={'platform': 'ANY', 'browserName': browser, 'version': '', 'javascriptEnabled': True}) # 打开百度首页并搜索词语,最后判断搜索跳转页面标题是否含有搜索词
wd = 'lovesoo'
driver.get('https://www.baidu.com')
driver.find_element_by_id("kw").send_keys(wd)
driver.find_element_by_id("su").click()
time.sleep()
assert wd in driver.title, '{0} not in {1}'.format(wd, driver.title.encode('utf-8'))
driver.quit()
运行结果如下:
('http://127.0.0.1:5555/wd/hub', 'chrome')
('http://127.0.0.1:5557/wd/hub', 'firefox')
('http://127.0.0.1:5556/wd/hub', 'internet explorer')
 

6. 常见问题

脚本运行过程中若IE浏览器报错找不到元素,而Chrome及Firefox浏览器正常,原因是IE浏览器的保护模式没有关闭:
1) 打开IE浏览器,在浏览器的菜单栏上点击“工具”选项,然后点击“Internet选项”
2) 在“Internet选项”中,切换到“安全”选项,取消“启用保护模式”的勾选
3) 并且上面四个区域的保护模式都需要关闭

Selenium Grid分布式测试入门详解的更多相关文章

  1. Selenium Grid分布式测试环境搭建

    Selenium Grid简介 Selenium Grid实际上是基于Selenium RC的,而所谓的分布式结构就是由一个hub节点和若干个node代理节点组成.Hub用来管理各个代理节点的注册信息 ...

  2. selenium之多线程启动grid分布式测试框架封装(四)

    九.工具类,启动所有远程服务的浏览器 在utils包中创建java类:LaunchAllRemoteBrowsers package com.lingfeng.utils; import java.n ...

  3. Redis快速入门详解

    Redis入门详解 Redis简介 Redis安装 Redis配置 Redis数据类型 Redis功能 持久化 主从复制 事务支持 发布订阅 管道 虚拟内存 Redis性能 Redis部署 Redis ...

  4. SQL注入攻防入门详解

    =============安全性篇目录============== 本文转载 毕业开始从事winfrm到今年转到 web ,在码农届已经足足混了快接近3年了,但是对安全方面的知识依旧薄弱,事实上是没机 ...

  5. SQL注入攻防入门详解(2)

    SQL注入攻防入门详解 =============安全性篇目录============== 毕业开始从事winfrm到今年转到 web ,在码农届已经足足混了快接近3年了,但是对安全方面的知识依旧薄弱 ...

  6. Quartz 入门详解

    Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个, ...

  7. [转]SQL注入攻防入门详解

    原文地址:http://www.cnblogs.com/heyuquan/archive/2012/10/31/2748577.html =============安全性篇目录============ ...

  8. 转:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法、shiro认证与shiro授权

    原文地址:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法.shiro认证与shiro授权 以下是部分内容,具体见原文. shiro介绍 什么是shiro shiro是Apache ...

  9. 【转载】SQL注入攻防入门详解

    滴答…滴答…的雨,欢迎大家光临我的博客. 学习是快乐的,教育是枯燥的. 博客园  首页  博问  闪存    联系  订阅 管理 随笔-58 评论-2028 文章-5  trackbacks-0 站长 ...

随机推荐

  1. JavaWeb学习之JDBC API中常用的接口和类

    JDBC API中包含四个常用的接口和一个类分别是: 1.Connection接口 2.Statement接口 3.PreparedStatement接口 4.ResultSet接口 5.Driver ...

  2. Identifying Duplicate Indexes

    本文是在阅读<Troubleshooting SQL Server>->Chapter 5: Missing Indexes->Identifying Duplicate In ...

  3. 再探Spring IOC

    这次做了提纲 下面再来一个case study case描述: 这是工具类  //bean的配置信息略去 class MyUtil{ private static UserDao userDao; p ...

  4. hdu4578 线段树 三次方,二次方,一次方的值

    Yuanfang is puzzled with the question below: There are n integers, a 1, a 2, -, a n. The initial val ...

  5. HDU1698 线段树(区间更新区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  6. Easy sssp

    Easy sssp 时间限制: 1 Sec  内存限制: 128 MB提交: 103  解决: 20[提交][状态][讨论版] 题目描述 输入数据给出一个有N(2  < =  N  < = ...

  7. Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)ABCD

    A. Arpa and a research in Mexican wave time limit per test 1 second memory limit per test 256 megaby ...

  8. 统计学习方法——CART, Bagging, Random Forest, Boosting

    本文从统计学角度讲解了CART(Classification And Regression Tree), Bagging(bootstrap aggregation), Random Forest B ...

  9. python邮件SMTP的GUI编程

    写的是python中smtp的gui编程,用的163邮箱给qq邮箱发送邮件做测试,如果你发现你的发送失败,试着用以下方法解决: 1.网页登陆你的邮箱,设置中查看smtp是否开启,比如163邮箱的smt ...

  10. 在Kubernetes集群中使用calico做网络驱动的配置方法

    参考calico官网:http://docs.projectcalico.org/v2.0/getting-started/kubernetes/installation/hosted/kubeadm ...