什么是Selenium Grid
Selenium Grid是Selenium套件的一部分,它专门用于并行运行多个测试用例在不同的浏览器、操作系统和机器上。
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 webdriver ds = {'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
运行之前的验证方法和脚本查看是否正确
3、docker启动
参考文章:https://www.gaoyaxuan.net/blog/409.html
还没有评论,来说两句吧...