[Selenium Grid]:无法在远程计算机上启动机器人测试

时间:2018-02-20 08:18:47

标签: selenium selenium-webdriver robotframework selenium-grid

我在本地计算机上设置了selenium网格,并在远程计算机上设置了一个节点。

节点正在运行

java -jar selenium-server-standalone-3.8.1.jar -role node  -hub http://<hub_machine>:4444/grid/register

现在我编写了机器人测试来在远程机器上运行脚本,如下所示:

Open Browser   google.com   chrome  None  http://<hub_machine>:4444/wd/hub  desired_capabilities=browserName:chrome,platform:WINDOWS

我也尝试使用远程Webdriver,如下所示:

${executor}=    Evaluate    str('http://<hub_machine>:4444/wd/hub')
    ${desired capabilities}=    Evaluate    { "browserName": "chrome", "version": "", "platform": "VISTA", "javascriptEnabled": True}
    Create Webdriver    Remote    desired_capabilities=${desired capabilities}    command_executor=${executor} 

但代码抛出错误如下:

2018-02-20 13:33:41.655:WARN:osjs.HttpChannel:qtp662736689-13: /wd/hub/session java.io.IOException: org.openqa.grid.common.exception.GridException: Cannot extract a capabilities from the request: {"capabilities": {"alwaysMatch": {"version": "", "platform": "VISTA", "browserName": "chrome", "javascriptEnabled": true}, "firstMatch": []}, "desiredCapabilities": {"version": "", "platform": "VISTA", "browserName": "chrome", "javascriptEnabled": true}}

请建议如何解决此问题。

2 个答案:

答案 0 :(得分:2)

错误说明了一切:

/wd/hub/session java.io.IOException: org.openqa.grid.common.exception.GridException: Cannot extract a capabilities from the request: {"capabilities": {"alwaysMatch": {"version": "", "platform": "VISTA", "browserName": "chrome", "javascriptEnabled": true}, "firstMatch": []}, "desiredCapabilities": {"version": "", "platform": "VISTA", "browserName": "chrome", "javascriptEnabled": true}}

要启动 Selenium网格节点,您应该将 WebDriver 变体作为参数提供,如下所示:

  • Mozilla Firefox

    java -Dwebdriver.gecko.driver=geckodriver.exe -jar selenium-server-standalone-3.8.1.jar -role node -hub http://<hub_machine>:4444/grid/register
    
  • Chrome

    java -Dwebdriver.chrome.driver=chromedriver.exe -jar selenium-server-standalone-3.8.1.jar -role node -hub http://<hub_machine>:4444/grid/register
    
  • Internet Explorer

    java -Dwebdriver.ie.driver=IEDriverServer.exe -jar selenium-server-standalone-3.8.1.jar -role node -hub http://<hub_machine>:4444/grid/register
    

更新

您仍然需要看到相同的错误:

  • 提供包含url协议的完整https://www.google.com(而不仅仅是google.com
  • IDE
  • 清除项目
  • 使用CCleaner工具清除所有操作系统杂务。
  • 执行测试

答案 1 :(得分:0)

注意,如果您需要在许多系统中运行Robot测试,那么Grid就是这样。但如果您只需要一台远程计算机,则可以在独立模式下使用selenium服务器。

我刚刚解决了这个问题。以下是我为Grid做的事情:

启动Hub和Node Selenium服务器

注意我在python的虚拟环境中运行来控制版本。所以下面的virPath是我放置selenium服务器jar文件的bin目录的完整路径

#! /bin/bash
virPath="$(echo $PATH | cut -f 1 -d':')"

java -jar ${virPath}/selenium-server.jar \
     -role hub \
     >/tmp/selenium_server_hub.log 2>&1 &

sleep 3

java -jar ${virPath}/selenium-server.jar \
     -role node \
     -hub http://localhost:4444/grid/register \
     -maxSession 10 \
     -browser browserName=chrome,maxInstances=10 \ 
     >/tmp/selenium_server_node.log 2>&1 &

运行机器人测试

当我想使用远程服务器运行机器人测试时,我将标志传递给pybot:

--variable Use Selenium Server:True
--variable Selenium Server URL:http://localhost:4444/wd/hub

我有很多关键字可以根据我的环境打开浏览器,但最终可以归结为:

Open Chrome Browser to Page
| | [Documentation]
| | ... | Opens Google Chrome to a given web page using Create Webdriver so
| | ... | arguments can be passed to webdriver in the form of capabilities.
| | ... | For more information on what capabilities that Google Chrome
| | ... | supports, see [http://chromedriver.chromium.org/capabilities] and
| | ... | [https://peter.sh/experiments/chromium-command-line-switches/]
| | ... |
| | ... | Webdriver can be run remotely using the selenium remote server.
| | ... | This can be done in 'Grid' mode with both a Hub server and
| | ... | Node servers where the tests are route to by the Hub to be run.
| | ... | See run_tests.sh for options to use this feature.
| | [Arguments] | ${url} |
| | ${ws}= | Set Variable | window-size=1080,1080 |
| | ${chrome_options}= | Evaluate | sys.modules['selenium.webdriver'].ChromeOptions() | sys |
| | Call Method | ${chrome_options} | add_argument | disable-infobars          |
| | Call Method | ${chrome_options} | add_argument | test-type                 |
| | Call Method | ${chrome_options} | add_argument | disable-extensions        |
| | Call Method | ${chrome_options} | add_argument | ignore-certificate-errors |
| | Call Method | ${chrome_options} | add_argument | allow-insecure-localhost  |
| | Call Method | ${chrome_options} | add_argument | disable-browser-side-navigation |
| | Call Method | ${chrome_options} | add_argument | ${ws} |
| | Run Keyword If | ${Use Chrome Headless} |
| | ... | Call Method | ${chrome_options} | add_argument | headless |
| | Run Keyword If | ${Use Selenium Server} |
| | ...        | Create Remote Chrome Webdriver | ${chrome_options} |
| | ... | ELSE | Create Local Chrome Webdriver  | ${chrome_options} |
| | Go To | ${url} |

Create Remote Chrome Webdriver
| | [Arguments] | ${chrome options} |
| | Comment | Convert webdriver options to capabilities for Selenium server |
| | ${options}= |  Call Method |  ${chrome_options} | to_capabilities |
| | ${status} | ${resp}= | Run Keyword And Ignore Error |
| | ... | Create Webdriver | Remote | command_executor=${Selenium Server URL} | desired_capabilities=${options}
| | Run Keyword If | '${status}'=='FAIL' | Fail | Could not connect to Selenium Server.  Is it up?\nError: ${resp} |

Create Local Chrome Webdriver
| | [Arguments] | ${chrome options} |
| | ${status} | ${resp}= | Run Keyword And Ignore Error |
| | ... | Create Webdriver | Chrome | chrome_options=${chrome_options} |
| | Run Keyword If | '${status}'=='FAIL' | Fail | Could not start browser.  Are tests being run locally?\nError: ${resp} |

工具版本

机器人env需要很多不同的包,所以我有一个bash脚本列出我使用的所有:

        Tool |      Version       | Path
-------------+--------------------+-------------------------------------
  Linux Dist | Ubuntu 16.04.4 LTS | NA
Linux Kernel | 4.4.0-116-generic  | NA
  Git commit | 9d7739b            | NA
  Virtualenv | 15.0.1             | /usr/bin/virtualenv
      Python | 2.7.12             | /home/bm/devel/work/rf/bin/python
          RF | 3.0.2              | /home/bm/devel/work/rf/bin/pybot
 RF Selenium | 3.1.0              | /home/bm/devel/work/rf/lib/python2.7/site-packages
    Selenium | 3.9.0              | /home/bm/devel/work/rf/lib/python2.7/site-packages
    Requests | 2.18.4             | /home/bm/devel/work/rf/lib/python2.7/site-packages
    Easysnmp | 0.2.5              | /home/bm/devel/work/rf/lib/python2.7/site-packages
      Poster | 0.8.1              | /home/bm/devel/work/rf/lib/python2.7/site-packages
      Chrome | 65.0.3325.181      | /usr/bin/google-chrome
chromedriver | 2.35.528139        | /home/bm/devel/work/rf/bin/chromedriver
     Firefox | 59.0.2             | /usr/bin/firefox
 Geckodriver | 0.19.1             | /home/bm/devel/work/rf/bin/geckodriver
------------------------------------------------------------------------