1、简介
- selenium grid远程运行selenium test
- 主旨是在多个机器,上并行运行selenium
优点
- 所有测试的中心入口点
- 管理和控制浏览器运行的Nodes/环境
- 扩展
- 并行运行测试
- 跨平台的测试
- 负载平衡
组成
image.png
2、下载与使用
- 官网: https://www.selenium.dev/downloads/
- 镜像地址:http://selenium-release.storage.googleapis.com/index.html
- hub
java -jar selenium-server-standalone-3.141.59.jar -role hub
- node
java -jar selenium-server-standalone-3.141.59.jar -role node -port 5677
配置文件启动Node
java -jar selenium-server-standalone.jar -role node -nodeConfig node1Config.json
- https://www.selenium.dev/documentation/en/grid/setting up_your_own grid/
3、实战
-
下载selenium grid
image.png
image.png
image.png - 将此.jar文件配置到环境变量
- 在Python中安装Selenium库、将Chromedriver配置到环境变量
- 进入selenium的GitHub网址:https://github.com/SeleniumHQ
image.pngimage.png
image.pngimage.png - 在.jar文件的目录下创建
node.json
文件,内容为上图
{
"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://localhost:4444",
"nodeStatusCheckTimeout": 5000,
"nodePolling": 5000,
"role": "node",
"unregisterIfStillDownAfter": 60000,
"downPollingLimit": 2,
"debug": false,
"servlets" : [],
"withoutServlets": [],
"custom": {}
}
-
启动hub
image.png -
启动node
image.png - 编写代码:
- 使用selenium中的
DesiredCapabilities
类,复制其中的Chrome Json方法
}
CHROME = {
"browserName": "chrome",
"version": "",
"platform": "ANY",
}
- 实战代码:
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver import Remote
class TestGrid:
def test_grid(self):
hub_url = "http://127.0.0.1:4444/wd/hub"
capability = DesiredCapabilities.CHROME.copy()
for i in range(1, 5):
driver = Remote(command_executor=hub_url, desired_capabilities=capability)
driver.get("https://www.baidu.com")
-
查看日志,一共运行了4四次
image.png
下一节:基于Jenkins的自动化调度详解。