Selenium Grid- 让自动化分布式执行变得可能

什么是 Selenium Grid?

Selenium Grid 是 Selenium 的三大组件之一,允许用户同时在不同的机器和系统上测试不同浏览器。
也就是说 Selenium Grid 支持分布式的测试执行。它可以让你的测试用例在一个分布式的执行环境中运行。

由上图可见,测试脚本会通过 selenium hub 节点分发给不同的 node 节点,而不同的 node 节点所在的操作系统和搭载的浏览器类型可以是不同的,接下来我们看下 hub 节点和 node 节点具体分别有什么作用:

hub 节点
中心节点,或总控节点
管理各个 node 节点的注册信息和状态信息
接收并转发客户端(测试脚本)请求到合适的 node 节点
node 节点
子节点,或代理节点
负责注册配置信息到 hub 节点(平台、浏览器、浏览器版本等)
负责接收来自 hub 节点的转发的请求以执行测试脚本
也可单独作为远程节点执行测试脚本
环境准备

环境说明:
本地端系统为 win10,搭载 V68 版本的 Chrome 浏览器,IP 地址为 192.168.1.222
远程端系统为 win7,搭载 V56 版本的 Firefox 浏览器,IP 地址为 192.168.140.130
两台主机能够相互 ping 通
hub 节点和 node1 节点都是在本地端工作,node2 节点在远程端工作
环境准备步骤:
测试机器安装 JDK,配置环境变量
下载 selenium-server-standalone-x.x.x.jar
https://npm.taobao.org/mirrors/selenium
下载浏览器驱动
Chrome:https://npm.taobao.org/mirrors/chromedriver
Firefox:https://npm.taobao.org/mirrors/geckodriver/
IE:https://npm.taobao.org/mirrors/selenium 注:IE 驱动和 selenium 依赖在一起
配置运行
1、本地端启动 hub
java -jar selenium-server-standalone-3.141.59.jar -role hub -port 8888 -maxSession 10
参数解释:

Java -jar selenium-server-standalone-3.141.59.jar 运行 jar 包
-role hub 以 hub 的角色运行
-port 8888 指定 hub 运行的端口(默认为 4444)
-maxSession 10 最大的处理会话
2、查看 hub 的运行状态
浏览器中输入 http://localhost:8888/grid/console 进入到到 hub 的 console 控制台

3、启动 node1

node1 节点工作在本地端,配置的浏览器为 V68 的 Chrome

java -Dwebdriver.chrome.driver="chromedriver.exe" -jar selenium-server-standalone-3.141.59.jar -role node -hub "http://192.168.4.4:8888/grid/register" -port 5558 -browser "browserName=chrome,maxInstances=2,version=68,platform=WINDOWS"

参数解释:

-Dwebdriver.chrome.driver="chromedriver.exe" 指定 chromeDriver 驱动所在的路径(本地)
-jar selenium-server-standalone-3.141.59.jar 运行 jar 包
-role node 以 node 角色执行
-hub "http://192.168.1.222:8888/grid/register" 指定要链接的 hub 地址,这样 node 才能和 hub 通讯连接上
-port 5558 node 的执行端口
-browser "browserName=chrome,maxInstances=2,version=68,platform=WINDOWS"
browserName=chrome 运行的浏览器名称
maxInstances=2 最多支持两个浏览器实例
version=68 浏览器版本号
platform=WINDOWS 平台名称
运行之后的效果:

浏览器再次刷新访问 hub 的 console 端

4、启动远程端的 node2 节点

node2 节点工作在远程端,配置的浏览器为 V56 的 Chrome

启动配置为

java  -Dwebdriver.firefox.driver="geckodriver.exe" -jar selenium-server-standalone-3.141.59.jar -role node -hub "http://192.168.140.1:8888/grid/register" -port 5558  -browser "browserName=firefox,maxInstances=2,version=56,platform=WINDOWS"

浏览器再次刷新访问 hub 的 console 端,可以看到两个 node 节点已经成功注册到 hub 节点上

 执行脚本

// 期望能力对象
DesiredCapabilities capabilities = new DesiredCapabilities();
//配置测试的浏览器,配置Chrome则会启动本地node1节点浏览器进行测试,配置Firefox则会启动远程端的Firefox进行测试
capabilities.setBrowserName(BrowserType.CHROME);
// hub节点
String url = "http://192.168.1.222:8888/wd/hub";
//和hub建立通讯,把相应配置传给hub,hub会根据配置选择注册的node节点,打开相应的浏览器进行测试
WebDriver driver = new RemoteWebDriver(new URL(url), capabilities);
driver.get("http://www.baidu.com");
driver.findElement(By.id("kw")).sendKeys("柠檬班软件测试");
Thread.sleep(10000);
driver.quit();

通过上述配置及脚本我们能够启动本地 node1 节点和 node2 节点分别进行测试,单 node 节点只能支持一款浏览器进行测试,如何让 node 节点可以支持多款浏览器呢?

答案就是在配置 node 节点的时候传入的参数配置兼容多款浏览器即可。

node 节点配置兼容不同浏览器
node 节点可以配置单一浏览器,如 Chrome、Firefox、IE 等等,也可以配置兼容多款浏览器

这样只要符合 node 节点配置中的任何一个,hub 会将客户端的执行请求发送过来

java -Dwebdriver.ie.driver="IEDriverServer.exe" -Dwebdriver.chrome.driver="chromedriver.exe" -Dwebdriver.firefox.driver="geckodriver.exe" -jar selenium-server-standalone-3.141.59.jar -role node -hub "http://192.168.140.1:8888/grid/register" -port 5555  -browser "browserName=internet explorer,maxInstances=4,version=11,platform=WINDOWS" -browser "browserName=chrome,maxInstances=2,version=68,platform=WINDOWS" -browser "browserName=firefox,maxInstances=3,version=56,platform=WINDOWS"

现在 node1 节点和 node2 节点采用相同的启动参数配置启动

我们可以看到,grid 的 console 控制台显示两个 node 节点都可以支持多浏览器测试

node 节点启动方式二

之前启动的方式是通过命令行或者 bat 脚本启动 node 节点,官方提供了两种方式,其二就是通过 JSON 配置文件启动

java -jar selenium-server-standalone-3.141.59.jar -role node -nodeConfig node.json

node.json 配置文件如下:

{
  "capabilities":
	  [
		{
          "browserName": "firefox",
          "maxInstances": 5,
          "seleniumProtocol": "WebDriver",
		  "webdriver.firefox.driver":"firefox.exe",
          #"firefox_binary":"D:\\Program Files\\Mozilla Firefox\\firefox.exe",
          "platform":"WINDOWS"
        },
        {
          "browserName": "chrome",
          "maxInstances": 5,
          "seleniumProtocol": "WebDriver",
          "webdriver.chrome.driver":"chromedriver.exe",
          "platform":"WINDOWS"
        },
        {
          "browserName": "internet explorer",
          "maxInstances": 5,
          "seleniumProtocol": "WebDriver",
          "platform":"WINDOWS"
        }
	  ],
	  "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
	  "maxSession": 5,
	  "port": -1,
	  "register": true,
	  "registerCycle": 5000,
	  "hub": "http://192.168.1.222:8888",
	  "nodeStatusCheckTimeout": 5000,
	  "nodePolling": 5000,
	  "role": "node",
	  "unregisterIfStillDownAfter": 60000,
	  "downPollingLimit": 2,
	  "debug": false,
	  "servlets" : [],
	  "withoutServlets": [],
	  "custom": {}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值