selenium Grid全集

1.selenium node启动报错信息如下:

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.运行报错:

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.运行报错:

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文件格式:

{
  "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文件命令:

$ java -jar selenium-server-standalone-141.59.jar -role node -port 4445 -nodeConfig node.json

  

4.selenium Grid举例:

步骤1:启动hub节点:

$ java -jar selenium-server-standalone-3.141.59.jar -role hub -port 4444

  

步骤2:启动node节点:

$ java -jar selenium-server-standalone-141.59.jar -role node -port 4445 -nodeConfig node.json

  

步骤3:编辑代码:

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打印的日志:

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打印的日志

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容器:

$ 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部署于同一台机器才行

$ 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

  

第三步:执行代码:

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格式如下:

{
  "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"
  }
}

启动节点命令:

$ appium --nodeconfig node_app1.json

7.第二个appium node启动报错,报错如下:

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的配置就改成下面这样的

{
  "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"
  }
}

启动节点命令:

$ 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/】

原文地址:https://www.cnblogs.com/lybolg/p/14013371.html