Selenium Grid - 多台计算机上并行运行

当你希望在多台计算机上并行运行测试?Selenium Grid可以帮你实现。
官方文档原文:

https://www.selenium.dev/documentation/grid/getting_started/

Selenium Grid允许通过将客户端发送的命令路由到远程浏览器实例,在远程机器上执行WebDriver脚本。

Grid 可以做那些事?

  • 1.提供一种在多台机器上并行运行测试的简单方法

  • 2.允许在不同的浏览器版本上进行测试

  • 3.启用跨平台测试

感兴趣吗?通过以下部分了解 Grid 是如何工作的,以及如何设置自己的 Grid 。

快速开始

1.必备的环境:

  • Java 11 或更高版本

  • Google Chrome 浏览器,或其它浏览器的安装

  • 浏览器对应驱动(如chromedriver)添加到环境变量下,如果添加--Selenium Manager true,则 Selenium Manager https://www.selenium.dev/documentation/selenium_manager/将自动配置驱动程序

2.启动命令

java -jar selenium-server-<version>.jar standalone

3.将您的WebDriver测试指向http://localhost:4444
4.通过在打开浏览器检查正在运行的测试和可用功能http://localhost:4444

Grid 角色

Grid 由六个不同的组件组成,这使您可以选择以不同的方式部署它。
根据您的需要,您可以单独启动它们中的每一个(分布式),将它们分组在Hub & Node中,或者在一台机器上将它们集中在一起(Standalone)

Standalone 模式

Standalone 将所有 Grid 组件无缝组合为一个组件.在 Standalone 模式下运行Grid,可以在单个进程中使用单个命令创建一个功能齐全的Grid。Standalone 只能在单机上运行。

Standalone 模式也是启动 Selenium 网格的最简单模式。默认情况下,服务器将侦听上的 RemoteWebDriver 请求 http://localhost:4444.
默认情况下,服务器将从系统路径检测可用的驱动程序。

java -jar selenium-server-<version>.jar standalone

在 Standalone 模式下成功启动 Grid 后,将 WebDriver 测试指向 http://localhost:4444.

Standalone 的常见场景有:

  • 使用RemoteWebDriver在本地开发或调试测试

  • 在推送代码之前运行快速测试套件

  • 在CI/CD工具中轻松设置 Grid(GitHub Actions、Jenkins等)

Hub-Node 模式

Hub-Node 模式是最常用的角色,因为它允许:

  • 将不同的机器组合在一个 Grid 中.例如,具有不同操作系统和/或浏览器版本的机器

  • 具有在不同环境中运行 WebDriver 测试的单一入口点.

  • 在不破坏网格的情况下放大或缩小容量

Hub 由以下组件组成:路由器、分发服务器、会话映射、新会话队列和事件总线
启动Hub :

java -jar selenium-server-<version>.jar hub

默认情况下,服务器将侦听RemoteWebDriver请求 `http://localhost:4444

Node 节点在启动期间,节点将从系统Path路径检测可用的驱动程序。
下面的命令假定Node节点在运行Hub的同一台计算机上运行。

java -jar selenium-server-<version>.jar node

同一台机器上可以有多个节点
Node 1

java -jar selenium-server-<version>.jar node --port 5555

Node 2

java -jar selenium-server-<version>.jar node --port 6666

Node 和 Hub 在不同机器上

Hub 和Node 节点通过HTTP和事件总线(事件总线位于Hub内)相互通信。Node节点通过事件总线向Hub发送消息以启动注册过程。当Hub接收到消息时,会通过HTTP联系节点以确认其存在。
要将Node 节点成功注册到Hub,重要的是在Hub计算机上开放事件总线端口(默认情况下为4442和4443)。这也适用于节点端口。有了它,Hub和 Node节点都可以通信。
(简单来说就是Node 和 Hub 不同机器能互相ping 通)

如果Hub使用默认端口,那么--Hub 可用于注册Node节点

java -jar selenium-server-<version>.jar node --hub http://<hub-ip>:4444

当hub不使用默认端口时,需要—publish事件和—subscribe事件标志。
例如,如果hub使用端口8886、8887和8888

java -jar selenium-server-<version>.jar hub --publish-events tcp://<hub-ip>:8886 --subscribe-events tcp://<hub-ip>:8887 --port 8888

Node节点需要使用这些端口才能成功注册

java -jar selenium-server-<version>.jar node --publish-events tcp://<hub-ip>:8886 --subscribe-events tcp://<hub-ip>:8887

分布式

当使用分布式 Grid 时,每个组件都是单独启动的,最好是在不同的机器上启动。
正确地暴露所有端口以允许所有组件之间的流畅通信是很重要的。

事件总线:启用不同网格组件之间的内部通信。
默认端口为:4442、4443和5557。

java -jar selenium-server-<version>.jar event-bus --publish-events tcp://<event-bus-ip>:4442 --subscribe-events tcp://<event-bus-ip>:4443 --port 5557

新建会话队列:将新的会话请求添加到队列中,分发服务器将查询该队列。默认端口为5559。

java -jar selenium-server-<version>.jar sessionqueue --port 5559

会话映射:将会话ID映射到会话正在运行的节点。默认会话映射端口为5556。会话映射与事件总线交互。

java -jar selenium-server-<version>.jar sessions --publish-events tcp://<event-bus-ip>:4442 --subscribe-events tcp://<event-bus-ip>:4443 --port 5556

分发服务器:查询新会话队列中的新会话请求,并在功能匹配时将其分配给节点。节点注册到分发服务器的方式与它们在集线器/节点网格中注册到集线器的方式相同。
默认分发服务器端口为5553。分发服务器与新会话队列、会话映射、事件总线和节点进行交互。

java -jar selenium-server-<version>.jar distributor --publish-events tcp://<event-bus-ip>:4442 --subscribe-events tcp://<event-bus-ip>:4443 --sessions http://<sessions-ip>:5556 --sessionqueue http://<new-session-queue-ip>:5559 --port 5553 --bind-bus false

路由器:将新的会话请求重定向到队列,并将正在运行的会话请求重新定向到正在运行该会话的节点。
默认路由器端口为4444。路由器与新会话队列、会话映射和分发服务器进行交互。

java -jar selenium-server-<version>.jar router --sessions http://<sessions-ip>:5556 --distributor http://<distributor-ip>:5553 --sessionqueue http://<new-session-queue-ip>:5559 --port 4444

节点默认节点端口为5555。

java -jar selenium-server-<version>.jar node --publish-events tcp://<event-bus-ip>:4442 --subscribe-events tcp://<event-bus-ip>:4443

官方文档原文:https://www.selenium.dev/documentation/grid/getting_started/

行动吧,在路上总比一直观望的要好,未来的你肯定会感 谢现在拼搏的自己!如果想学习提升找不到资料,没人答疑解惑时,请及时加入扣群: 320231853,里面有各种软件测试+开发资料和技术可以一起交流学习哦。

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

  • 10
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要编写测试用例并将它们分发到Selenium Grid上的多台计算机运行,可以按照以下步骤操作: 1. 安装pytest和Selenium Grid客户端库: ``` pip install pytest pip install selenium pip install selenium-grid-utilities ``` 2. 编写pytest测试用例,例如: ```python from selenium import webdriver import pytest @pytest.fixture(scope="session") def browser(request): driver = webdriver.Remote( command_executor='http://localhost:4444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME ) yield driver driver.quit() def test_google(browser): browser.get("https://www.google.com") assert browser.title == "Google" ``` 3. 在pytest.ini文件中配置测试执行过程中使用的pytest-selenium插件和Selenium Grid客户端库: ``` [pytest] selenium_grid_url = http://localhost:4444/wd/hub selenium_capture_debug = true selenium_log_path = /path/to/log/file.log selenium_driver_log_path = /path/to/driver/log/file.log selenium_timeout_multiplier = 3 selenium_default_capabilities = {"browserName": "chrome"} selenium_grid_utility_hosts = localhost ``` 4. 启动Selenium Grid hub和至少一个Selenium Grid node: ``` java -jar selenium-server-standalone.jar -role hub java -jar selenium-server-standalone.jar -role node -hub http://localhost:4444/grid/register ``` 5. 运行pytest测试用例,并使用`--workers`选项指定要使用的工作进程数,例如: ``` pytest --workers 2 ``` 这将分发测试用例到Selenium Grid上的两个节点,并行运行它们。在测试执行期间,pytest-selenium插件将记录测试的运行情况和结果,并在测试运行完成后生成测试报告。 注意:在使用Selenium Grid进行分布式测试时,需要确保测试用例中使用的所有Web驱动程序都与Selenium Grid节点的操作系统和浏览器版本兼容。此外,还需要在Selenium Grid节点上配置适当的防火墙规则,以便允许外部机器连接到Selenium Grid节点。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值