selenium Grid全集
1.selenium node启动报错信息如下:
1 2 | 22 : 26 : 07.086 INFO [RequestHandler.process] - Error forwarding the new session cannot find : Capabilities {browserName: chrome, platform: MAC, version: 10 } org.openqa.grid.common.exception.CapabilityNotPresentOnTheGridException: cannot find : Capabilities {browserName: chrome, platform: MAC, version: 10 } |
原因:代码里的capability和node.json里的capability不一致导致,改成一样的就行
2.运行报错:
1 2 | 10:43:53.451 INFO [RequestHandler.process] - Error forwarding the new session Empty pool of VM for setup Capabilities {browserName: chrome, version: } org.openqa.grid.common.exception.GridException: Empty pool of VM for setup Capabilities {browserName: chrome, version: } |
原因:只启动了hub节点,没有启动node节点:
3.运行报错:
1 | selenium.common.exceptions.WebDriverException: Message: Error forwarding the new session cannot find : Capabilities {browserName: chrome, platform: MAC, seleniumProtocol: WebDriver, version: 10} |
原因:
1)没有对应的node.json文件
2)node启动时没有挂对应的node.json文件
selenium的node.json文件格式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | { "capabilities" : [ { "browserName" : "chrome" , "maxInstances" : 5, "seleniumProtocol" : "WebDriver" , "platform" : "MAC" , "version" : "10" } ], "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" : {} } |
node节点启动挂对应的node.json文件命令:
1 | $ java -jar selenium-server-standalone-141.59.jar -role node -port 4445 -nodeConfig node.json |
4.selenium Grid举例:
步骤1:启动hub节点:
1 | $ java -jar selenium-server-standalone-3.141.59.jar -role hub -port 4444 |
步骤2:启动node节点:
1 | $ java -jar selenium-server-standalone-141.59.jar -role node -port 4445 -nodeConfig node.json |
步骤3:编辑代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import time import pytest from selenium import webdriver class TestGrid: def test_hub(self): capabilities={ "browserName" : "chrome" , "seleniumProtocol" : "WebDriver" , "platform" : "MAC" , "version" : "10" } driver = webdriver.Remote( 'http://192.168.56.1:4444/wd/hub' ,capabilities) driver.get( 'https://wwww.baidu.com' ) time . sleep (5) driver.quit() |
运行完成后:
hub打印的日志:
1 2 | 10:55:50.342 INFO [RequestHandler.process] - Got a request to create a new session: Capabilities {browserName: chrome, platform: MAC, seleniumProtocol: WebDriver, version: 10} 10:55:50.343 INFO [TestSlot.getNewSession] - Trying to create a new session on test slot {server:CONFIG_UUID=d4379450-1f66-40da-a32e-c88d017ea32d, seleniumProtocol=WebDriver, browserName=chrome, maxInstances=5, platformName=MAC, version=10, platform=MAC} |
node打印的日志
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 10:55:50.355 INFO [ActiveSessionFactory.apply] - Capabilities are: { "browserName" : "chrome" , "goog:chromeOptions" : { }, "platform" : "MAC" , "seleniumProtocol" : "WebDriver" , "version" : "10" } 10:55:50.355 INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.grid.session.remote.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService) Starting ChromeDriver 86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs /branch-heads/4240 @{ #378}) on port 9813 Only local connections are allowed. Please see https: //chromedriver .chromium.org /security-considerations for suggestions on keeping ChromeDriver safe. [1607223350.408][WARNING]: FromSockAddr failed on netmask ChromeDriver was started successfully. 10:55:52.126 INFO [ProtocolHandshake.createSession] - Detected dialect: W3C 10:55:52.128 INFO [RemoteSession$Factory.lambda$performHandshake$0] - Started new session 6f9a43d0829ccb56e37540afca296450 (org.openqa.selenium.chrome.ChromeDriverService) 10:55:58.383 INFO [ActiveSessions$1.onStop] - Removing session 6f9a43d0829ccb56e37540afca296450 (org.openqa.selenium.chrome.ChromeDriverService) |
5.docker+selenium Grid
第一步:启动selenium hub容器:
1 | $ docker run --name=hub -p 5555:4444 -e GRID_TIMEOUT=30000 -e GRID_THROW_ON_CAPABILITY_NOT_PRESENT= true -e GRID_NEW_SESSION_WAIT_TIMEOUT=5000 -e GRID_BROWSER_TIMEOUT=15000 -e GRID_CLEAN_UP_CYCLE=30000 -d selenium /hub |
第二步:启动node容器:
ps:这里启动【node-chrome-debug】镜像是为了更好的调试,这里可以下载VNC View来看运行结果
--link选项是用来和hub容器进行关联,但是这个选项只能是hub和node部署于同一台机器才行
1 | $ docker run --name=chrome -p 5900:5900 -e NODE_MAX_INSTANCES=5 -e NODE_MAX_SESSION=5 -e NODE_REGISTER_CYCLE=5000 --link hub -d selenium /node-chrome-debug |
第三步:执行代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import os import time import pytest from selenium import webdriver class TestHubDocker: def test_hub( self ): capabilities = { "browserName" : "chrome" , # 这里注意,不要写下面这几个配置,不然就会报错 # "seleniumProtocol": "WebDriver", # "platform": "MAC", # "version": "10" } driver = webdriver.Remote( 'http://127.0.0.1:5555/wd/hub' ,capabilities) driver.get( 'https://wwww.baidu.com' ) time.sleep( 5 ) driver.quit() |
6.如果是要把Appium服务注册到selenium Grid的话,方法和node.json和selenium的都不一样
官方参考连接为:https://appium.io/docs/cn/advanced-concepts/grid/
复制官方的注意,它在【"hubHost":】这一行最后少了一个逗号
appium的node.json格式如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | { "capabilities" : [ { "browserName" : "ANDROID" , "version" : "6.0" , "maxInstances" : 1 , "platform" : "ANDROID" } ], "configuration" : { "cleanUpCycle" : 2000 , "timeout" : 30000 , "proxy" : "org.openqa.grid.selenium.proxy.DefaultRemoteProxy" , "url" : "http://localhost:4723/wd/hub" , "host" : "localhost" , "port" : 4723 , "maxSession" : 1 , "register" : true, "registerCycle" : 5000 , "hubPort" : 4444 , "hubHost" : "192.168.91.78" , "hubProtocol" : "http" } } |
启动节点命令:
1 | $ appium - - nodeconfig node_app1.json |
7.第二个appium node启动报错,报错如下:
1 2 3 4 5 6 | Could not start REST http interface listener. The requested port may already be in use. Please make sure there is no other instance of this server running already. Fatal Error: listen EADDRINUSE: address already in use 0.0 . 0.0 : 4723 at Server.setupListenHandle [as _listen2] (net.js: 1301 : 14 ) at listenInCluster (net.js: 1349 : 12 ) at doListen (net.js: 1488 : 7 ) at processTicksAndRejections (internal / process / task_queues.js: 81 : 21 ) |
原因有2,需要具体排查:
1)这个node要启动的appium的端口和已启动的端口重复了,要把node.json文件中【configuration】的【url、port】这俩参数里涉及到端口的改成和已经启动的node节点的配置不一样的,比如上面第2点中是我第一个启动的node节点,那我第二个要启动的node节点的node.json的配置就改成下面这样的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | { "capabilities" : [ { "browserName" : "ANDROID" , "version" : "6.0" , "maxInstances" : 1 , "platform" : "ANDROID" } ], "configuration" : { "cleanUpCycle" : 2000 , "timeout" : 30000 , "proxy" : "org.openqa.grid.selenium.proxy.DefaultRemoteProxy" , "url" : "http://localhost:4724/wd/hub" , "host" : "localhost" , "port" : 4724 , "maxSession" : 1 , "register" : true, "registerCycle" : 5000 , "hubPort" : 4444 , "hubHost" : "192.168.91.78" , "hubProtocol" : "http" } } |
启动节点命令:
1 | $ appium - - nodeconfig node_app2.json |
补充知识:
Grid的节点连接情况可以从【hub的服务器地址/grid】这个连接中,点击【console】跳转的页面查看,grid这个页面点击【wiki】可以跳转到selenium grid的github wiki文档地址【https://github.com/SeleniumHQ/selenium/wiki/Grid2】,这里主要是说明selenium node连接hub的,appium的不在这里,appium的是在【https://appium.io/docs/cn/advanced-concepts/grid/】
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥