什么是Selenium Grid

Selenium GridSelenium套件的一部分,它专门用于并行运行多个测试用例在不同的浏览器、操作系统和机器上。
Selenium Grid有两个版本——老版本Grid 1和新版本Grid 2。我们只对新版本做介绍,因为Selenium团队已经逐渐遗弃老版本了。
Selenium Grid 主要使用 master-slaves (or hub-nodes) 理念 --一个 master/hub 和多个 基于master/hub注册的子节点 slaves/nodes。当我们在master上基于不同的浏览器/系统运行测试用例时,master将会分发给适当的node运行。

什么时候用Selenium Grid

  • 同时在不同的浏览器、操作系统和机器上运行测试。最大程度用于兼容性测试
  • 减少运行时间

怎样启动Selenium Grid?

启动Selenium Grid的三种方式,一种直接用命令行,另一种用JSON配置文件,最后一种docker启动。

1. 命令行启动

将会使用2台机器,一台运行hub另一台运行node,为了方便描述,将运行hub的机器命名为“Machine H”(IP:192.168.1.100),运行node的机器命名为“Machine N”(IP:192.168.1.101)

Step 1
  • 配置Java环境

  • 已安装需要运行的浏览器

  • 下载浏览器driver,放到和selenium server相同的路径下(查看) ,否则在启动node时要加参数,不然启动不了浏览器(java -Dwebdriver.chrome.driver="C:\your path\chromedriver.exe" -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.1.100:5566/grid/register/,可切换浏览器)

    driver link comments
    Firefox 下载 安装的浏览器要和下载的driver版本一致
    Chrome 下载 安装的浏览器要和下载的driver版本一致
    IE 下载 安装的浏览器要和下载的driver版本一致
    Edge 下载 安装的浏览器要和下载的driver版本一致
  • 下载selenium server,将selenium-server-standalone-X.XX.jar分别放在“Machine H”和“Machine N”上(自定义路径)

Step 2
  • 在机器“Machine H”上打开命令行,到selenium server所在的路径,运行:java -jar selenium-server-standalone-3.141.59.jar -role hub -port 5566,成功启动你会看到:

    或者直接在机器“Machine H”上的浏览器(“Machine N”则需要将IP修改为“Machine H”的)打开:http://localhost:5566/grid/console ,将会看到:
  • 在机器“Machine N”上打开命令行,到selenium server所在的路径,运行:java -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.1.100:5566/grid/register/ -port 5577,成功启动你会看到:

    刷新:http://localhost:5566/grid/console ,将会看到:
Step 3

运行测试脚本,将会看到在机器“Machine N”上打开了Chrome浏览器,并运行了测试用例:

from selenium import webdriverds = {'platform': 'ANY','browserName': "chrome",'version': '','javascriptEnabled': True}
dr = webdriver.Remote('http://192.168.1.101:5577/wd/hub', desired_capabilities=ds)
dr.get("https://www.baidu.com")
print dr.name
2. Json配置文件启动
Step 1
  • 创建hub的Json配置文件( 查看)
    代码如下:

    {"port": 4444,"newSessionWaitTimeout": -1,"servlets" : [],"withoutServlets": [],"custom": {},"capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher","registry": "org.openqa.grid.internal.DefaultGridRegistry","throwOnCapabilityNotPresent": true,"cleanUpCycle": 5000,"role": "hub","debug": false,"browserTimeout": 0,"timeout": 1800
    }
    

    将上述代码保存为hub_config.json文件,放在“Machine H”上和selenium server相同的路径下。

  • 创建nodes的 Json配置文件(如果selenium版本是3.0或更高则查看,否则查看)
    代码如下:

    {"capabilities":[{"browserName": "firefox","marionette": true,"maxInstances": 5,"seleniumProtocol": "WebDriver"},{"browserName": "chrome","maxInstances": 5,"seleniumProtocol": "WebDriver"},{"browserName": "internet explorer","platform": "WINDOWS","maxInstances": 1,"seleniumProtocol": "WebDriver"},{"browserName": "safari","technologyPreview": false,"platform": "MAC","maxInstances": 1,"seleniumProtocol": "WebDriver"}],"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy","maxSession": 5,"port": -1,"register": true,"registerCycle": 5000,"hub": "http://192.168.1.100:4444","nodeStatusCheckTimeout": 5000,"nodePolling": 5000,"role": "node","unregisterIfStillDownAfter": 60000,"downPollingLimit": 2,"debug": false,"servlets" : [],"withoutServlets": [],"custom": {}
    }
    

    保存为node_config.json文件(注意将hub对应的值改为机器“Machine H”的IP),放在“Machine N”上和selenium server相同的路径下。(当多个node时需将该文件放在多个node机器上或者同一个机器上启动多个node)

Step 2

hub机器上命令行运行:java -jar selenium-server-standalone-3.141.59.jar -role hub -hubConfig hub_config.json
node机器上命令行运行:java -jar selenium-server-standalone-3.141.59.jar -role node -nodeConfig node_config.json
运行之前的验证方法和脚本查看是否正确

(1、2)方式启动的挑战

不易启动和维护:

  • 每个node需要下载和配置依赖
  • java 进程占内存
  • 出现问题时需手动启动
  • 不易维护
  • 扩展性差
3. docker启动
docker简介
  • https://docs.docker.com/docker-hub/
  • https://yeasy.gitbooks.io/docker_practice/appendix/repo/mysql.html
  • https://docker-curriculum.com/
  • https://towardsdatascience.com/learn-enough-docker-to-be-useful-1c40ea269fa8
docker启动Selenium Grid

docker上已经有selenium官方的Selenium Grid镜像,只有你已经安装了docker,即可使用。

  • 启动hub:docker run -d -p 4444:4444 --name selenium-hub selenium/hub
  • 启动node(Chrome&&Firefox):
    • docker run -d --link selenium-hub:hub selenium/node-chrome
    • docker run -d --link selenium-hub:hub selenium/node-firefox

运行命令将会下载内置镜像文件(包括java、Chrome、Firefox、selenium-server-standalone-XXX.jar 等运行selenium所需的环境);此时你可以访问:http://localhost:4444/grid/console

如果需要多个Chrome node则继续运行这个命令:docker run -d --link selenium-hub:hub selenium/node-chrome,刷新则看到多了一个Chrome实例。
通过运行命令:docker ps,显示正在运行的容器

关闭docker-grid的命令:docker stop $(docker ps -a -q)docker rm $(docker ps -a -q)
docker已经简化了selenium Grid的搭建流程,但是还是有很多的手动工作。需要一个一个的启动/关闭hub/nodes.

docker 组件启动Selenium Grid

selenium Grid通常需要启动一个hub,多个nodes像Chrome、Firefox等。我们可以把他们定义到一个文件中叫做docker-compose.yml,通过一个命令来整体启动,docker提供了一个这样的工具 –Docker-Compose
安装docker-compose查看,一旦安装成功,则创建一个新的文件夹,创建文件 docker-compose.yml, docker-compose.yml内容:

version: "3"
services:selenium-hub:image: selenium/hubcontainer_name: selenium-hubports:- "4444:4444"chrome:image: selenium/node-chromedepends_on:- selenium-hubenvironment:- HUB_PORT_4444_TCP_ADDR=selenium-hub- HUB_PORT_4444_TCP_PORT=4444firefox:image: selenium/node-firefoxdepends_on:- selenium-hubenvironment:- HUB_PORT_4444_TCP_ADDR=selenium-hub- HUB_PORT_4444_TCP_PORT=4444
docker-compose命令:
  • 运行命令启动(到docker-compose.yml路径下):docker-compose up -d
  • 查看启动是否成功:docker-compose ps
  • 创建更多实例:docker-compose scale chrome=5
  • 关闭命令:docker-compose down


浏览器打开http://localhost:4444/grid/console将会看到:

运行脚本的话直接运行就好(IP:http://localhost:4444/wd/hub) ,和上边两种的方法不太一样;不会有浏览器打开(容器内部运行),但是已经运行成功:

import unittest
from selenium import webdriverclass MyTestCase(unittest.TestCase):def setUp(self):ds = {'platform': 'ANY','browserName': "chrome",'version': '','javascriptEnabled': True}self.dr = webdriver.Remote('http://localhost:4444/wd/hub', desired_capabilities=ds)def test_something(self):self.dr.get("https://www.baidu.com")self.assertEqual(self.dr.name, "chrome")def test_search_button(self):self.dr.get("https://www.baidu.com")self.assertTrue(self.dr.find_element_by_id("su").is_displayed())def tearDown(self):self.dr.quit()if __name__ == '__main__':unittest.main()

总结

主要对启动Selenium Grid的三种方式做了整理,方便使用者选出最佳的方式。

参考:

  • https://github.com/SeleniumHQ/selenium/wiki/Grid2
  • https://www.guru99.com/introduction-to-selenium-grid.html
  • http://www.testautomationguru.com/selenium-grid-setup-using-docker/

Selenium Grid使用相关推荐

  1. Selenium Grid的使用(分布式测试)

    1.Selenium Grid简介 Selenium Grid组件专门用于远程分布式测试或并发测试.使用此组件可以在一台计算机上给多台计算机(不同操作系统和不同版本浏览器环境)分发多个测试用例从而并发 ...

  2. Robot Framework + Selenium2Library环境下,结合Selenium Grid实施分布式自动化测试

    最近一段时间,公司在推行自动化测试流程,本人有幸参与了自定义通用控件的关键字封装和脚本辅助编写.数据驱动管理.测试用例执行管理等一系列工具软件的研发工作,积累了一些经验,在此与大家做一下分享,也算是做 ...

  3. Selenium Grid的搭建方法

    一.什么是Selenium Grid Selenium Grid 是一种可以并发执行 GUI 测试用例的测试执行机的集群环境,分为 HUB 和 Node 模式.Selenium Hub 用来管理各个 ...

  4. 03-19 分布式测试-Selenium Grid

    简介 Selenium Grid 远程运行 Selenium test,主旨是在多个机器上并行运行 selenium 优点 它是所有测试的中心入口点 可以管理和控制浏览器运行的 Nodes/环境 扩展 ...

  5. Selenium Grid跨浏览器-兼容性测试

    Selenium Grid跨浏览器-兼容性测试 这里有两台机子,打算这样演示: 一台机子启动一个作为主点节的hub 和 一个作为次节点的hub(系统windows 浏览器为ie) ip为:192.16 ...

  6. Selenium大家族介绍(selenium RC,selenium IDE, selenium Grid, selenium Webdriver)

    Selenium selenium 是当下非常流行的自动化测试框架,selenium是开源项目. selenium是一个大家族,这个家族中的主要成员有如下所示: selenium RC: seleni ...

  7. 【Selenium Grid 分布式测试】Selenium Grid-简介

    前言 一直想学习自动化测试,但是都没行动,业余时间学习零零碎碎并记录20210423. 9.Selenium Grid 分布式测试 Selenium Grid 分布式测试-Selenium Grid简 ...

  8. Selenium Grid 分享

    Selenium 家族简单介绍: Selenium 1.0 Selenium 1.0 包括了 Selenium IDE, Selenium Grid和Selenium RC(Selenium Remo ...

  9. 【转】Selenium Grid 个人理解

    估计现在用selenium 的人也越来越多了.selenium rc用起来了之后.肯定会有一些想法. 第一,怎么能同时跑多个测试,就是所谓的并发.否则来100个case,一个一个的跑,几个小时才能跑完 ...

  10. selenium grid环境配置

    走了好多弯路,记录一下配置经验.如果有描述不恰当之处,欢迎指出. 首先保证电脑安装了java并且配置好了环境变量:我使用的编译器是pycharm,已经提前下载了selenium 下载selenium ...

最新文章

  1. win7+php5.3.10下安装memcache (转)
  2. linux的 0号进程(idle进程) 和 1 号进程(init进程)
  3. 计算机转集成光学,最新集成光学考试总结.docx
  4. LVS之NAT模型配置实验
  5. 【渝粤题库】国家开放大学2021春1026西方经济学(本)题目
  6. 阿里P8架构师谈:架构设计经验汇总
  7. win10无线投屏_Win10电脑屏幕分割成四分屏投屏测试
  8. 如何在windows2008/2012上安装启明星系统。
  9. 光立方体c语言程序,444光立方程序怎么写 光立方原理图、源代码及制作教程
  10. DreamweaverCS6手把手教你安装并破解
  11. matlab除法使用broadcast机制要小心
  12. 改之理java文件_apk改之理反编译错误,来大神
  13. python操作excel(二):自动填充
  14. 教你一个快速掌握知识的学习方法
  15. 6G八大关键技术(国泰君安团队)
  16. 期货反跟单-千万不要盲目开始反跟单交易
  17. YOLOv7来临:论文解读附代码解析
  18. trim函数去掉字符串首尾空格
  19. 浏览器插件实现GitHub代码翻译原型演示
  20. Ubuntu 4.10 (Warty Warthog,长疣的疣猪)

热门文章

  1. sql-update 用法
  2. 软件测试面试,面试官最后问:你有什么要问我的吗?应该如何回答
  3. Java实现港(澳)台大陆身份证校验(亲测有效)
  4. 用数字万用表测量电阻-2/4/6线制测量
  5. 计算机CPU高温,电脑cpu温度高怎么办?别怕,三招搞定
  6. 台式计算机连接投影仪无信号,投影仪连接电脑没反应 电脑连接投影仪无信号解决办法...
  7. 迁移学习Transfer Learning
  8. vss服务器状态失败_VSS错误自动修复
  9. Flutter 如何实现禁止手机横屏的功能
  10. 使用PS制作背景透明的png图片