selenium源码通读·9 |webdriver/common/desired_capabilities.py-DesiredCapabilities类分析
源码 分析 selenium py Common Capabilities WebDriver 通读
2023-06-13 09:16:57 时间
1 源码目录
selenium/webdriver/common/desired_capabilities.py
2 功能描述
- 根据测试代码中配置的DesiredCapabilities参数,来决定将测试代码分发到哪台node上进行测试;
- 补充知识:我们需要了解下
selenium grid
;
3 Selenium Grid介绍
3.1 是什么?
- Selenium套件的一部分,它专门用于并行运行多个测试用例在不同的浏览器、操作系统和机器上;
- Selenium Grid 主要使用
master-slaves (or hub-nodes)
理念 ,是一个master/hub
和多个基于master/hub
注册的子节点slaves/nodes
; - 在master上基于不同的浏览器/系统运行测试用例时,master将会分发给适当的node运行;
在这里插入图片描述
3.2 什么时候用?
- 同时在不同的浏览器、操作系统和机器上运行测试;
- 用于兼容性测试;
- 减少运行时间。
3.3 怎么用?
- 启动Selenium Grid的三种方式,一种直接用命令行,另一种用JSON配置文件,最后一种docker启动。
3.3.1 命令行启动
简单说下步骤,详细的请查阅其他资料,运行hub机器为A,运行node机器为B。
- 配置Java环境;
- 浏览器;
- 浏览器对应的driver;
- 下载
selenium server
,将selenium-server-standalone-3.141.59.jar
,机器A和机器B上; - 机器A上打开命令行,进入
selenium server
目录下,运行:
ava -jar selenium-server-standalone-3.141.59.jar -role hub -port 5566
- 浏览器输入
http://localhost:5566/grid/console
; - 机器B上打开命令行,进入
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
; - 运行测试脚本,将会看到在机器B上打开了Chrome浏览器,并会运行测试用例。
3.3.2 Json配置文件启动
- 创建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
文件,放在 机器A上和selenium server
相同的路径下; - 创建nodes的 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://这里是机器A的ip:4444",
"nodeStatusCheckTimeout": 5000,
"nodePolling": 5000,
"role": "node",
"unregisterIfStillDownAfter": 60000,
"downPollingLimit": 2,
"debug": false,
"servlets" : [],
"withoutServlets": [],
"custom": {}
}
- 保存为
node_config.json
文件,放在机器B上和selenium server相同的路径下; - 机器A,运行:
java -jar selenium-server-standalone-3.141.59.jar -role hub -hubConfig hub_config.json
- 机器B,运行:
java -jar selenium-server-standalone-3.141.59.jar -role node -nodeConfig node_config.json
3.3.3 docker启动
- 安装docker;
- 启动hub:
docker run -d -p 4444:4444 --name selenium-hub selenium/hub
- 启动node,比如chrome浏览器;
docker run -d --link selenium-hub:hub selenium/node-chrome
- 访问:
http://localhost:4444/grid/console
; - 运行多个node:
docker run -d --link selenium-hub:hub selenium/node-chrome
- 关闭docker-grid的命令:
docker stop $(docker ps -a -q), docker rm $(docker ps -a -q)
- 注意,特别说明:关于Selenium Grid的内容参考与https://blog.csdn.net/lb245557472/article/details/91966770
4 部分源码说明
class DesiredCapabilities(object):
"""
Set of default supported desired capabilities.
Use this as a starting point for creating a desired capabilities object for
requesting remote webdrivers for connecting to selenium server or selenium grid.
Usage Example::
from selenium import webdriver
selenium_grid_url = "http://198.0.0.1:4444/wd/hub"
# Create a desired capabilities object as a starting point.
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities['platform'] = "WINDOWS"
capabilities['version'] = "10"
# Instantiate an instance of Remote WebDriver with the desired capabilities.
driver = webdriver.Remote(desired_capabilities=capabilities,
command_executor=selenium_grid_url)
Note: Always use '.copy()' on the DesiredCapabilities object to avoid the side
effects of altering the Global class instance.
"""
FIREFOX = {
"browserName": "firefox",
"marionette": True,
"acceptInsecureCerts": True,
}
INTERNETEXPLORER = {
"browserName": "internet explorer",
"version": "",
"platform": "WINDOWS",
}
EDGE = {
"browserName": "MicrosoftEdge",
"version": "",
"platform": "WINDOWS"
}
# 剩下的源码省略
- 从源码看,下边均对浏览器系统等的说明:
browserName:浏览器
version:操作系统版本
platform:操作系统
相关文章
- babel源码详解-v1.7.8
- java集合源码分析(四):LinkedList「建议收藏」
- OpenCV、EmguCV和OpenCvSharp指针访问图像像素值耗时测评(附源码)
- mysql-5.7.38启动流程源码解读
- Python/GUI/tkinter/学生信息管理系统源码
- C语言实现List实现(附完整源码)[通俗易懂]
- 三网码支付系统源码三网免挂有PC软件有云端源码+搭建教程
- mybatis3源码解析--日志详解
- Golang netpoll源码分析
- 全能API接口PHP源码V1.4.2版本
- React源码分析3-render阶段(穿插scheduler和reconciler)
- 数据可视化:基于 Echarts + SpringBoot 的动态实时大屏银行监管系统【源码】
- selenium源码通读·4 |webdriver/common分析
- selenium源码通读·8 |webdriver/common/keys.py-Keys类分析
- selenium源码通读·10 |webdriver/common/proxy.py-Proxy类分析
- selenium源码通读·11 |webdriver/common/touch_actions.py-TouchActions类分析
- sqlmap 源码分析(二)初始化
- 5. 「snabbdom@3.5.1 源码分析」Thunks 函数
- 【Linux 内核】实时调度类 ⑥ ( 实时调度类核心函数源码分析 | 插入进程到执行队列 | 从执行队列中选择优先级最高的进程 )
- 【Linux 内核 内存管理】物理内存组织结构 ④ ( 内存区域 zone 简介 | zone 结构体源码分析 | zone 结构体源码 )
- Hadoop2源码分析-RPC探索实战详解大数据
- Java项目资源下载地址(包括源码和解析)
- Linux 内核源码分析:探索世界的奥秘(linux内核源码分析)
- 深度解析Linux Echo源码剖析:精彩深入分析(linuxecho源码)
- OpenStack Create Snapshot源码流程分析,理解创建快照的本质
- Redis 源码下载:一站式服务(redis 源码下载)
- C语言实现与Oracle数据库连接的研究(c 连接oracle源码)
- 深入探索Redis集群的源码分析(redis集群分析源码)