在Ubuntu 18.04.2 LTS docker容器上使用Python 3.6.8的Robot Framework



我一直找不到一个在Ubuntu v18.x上使用Python v3.x运行Robot Framework的好的公共映像。一位同事创建了一个映像,我将其作为基础,但我的机器人脚本无法连接到网站,我已经旋转了太久。是时候向社区寻求帮助了!

我有一个简单的机器人框架脚本(见下文(,它连接到一个网站(chrome-headless(并验证标题。它在我的Ubuntu工作站上可以找到,但当我在工作站上运行docker容器时,它就是无法连接。起初,我以为这是管道问题,但事实并非如此。以下是我所采取的步骤,以及在我的工作站上工作但不在容器上工作的机器人代码:

  1. 从容器确认连接正在工作:
    curl -o /dev/null -s -w %{http_code} https://www.google.com(这将返回一个200(

  2. 确保我的工作站和容器上的python3、pip3和ubuntu版本相同

  3. 确保pip3库是相同的(除了一些我知道我不需要的,是的,我知道我安装的比我需要的多了几个(-以下是来自pip3 freeze命令的列表:

asn1crypto==0.24.0
certifi==2020.4.5.1
chardet==3.0.4
coverage==5.2
cryptography==2.1.4
decorator==4.4.2
idna==2.6
importlib-metadata==1.7.0
jsonpath-rw==1.4.0
jsonpath-rw-ext==1.2.2
keyring==10.6.0
keyrings.alt==3.0
pbr==5.4.5
pluggy==0.13.1
ply==3.11
py==1.9.0
pycrypto==2.6.1
pygobject==3.26.1
pyxdg==0.25
requests==2.23.0
robotframework==3.1.1
robotframework-jsonlibrary==0.3.1
robotframework-requests==0.7.0
robotframework-seleniumlibrary==4.1.0
SecretStorage==2.3.1
selenium==3.141.0
six==1.11.0
tox==3.0.0
urllib3==1.22
virtualenv==16.7.10
zipp==3.1.0

现在机器人脚本(同样,在我的工作站上工作,只是不在容器上(

*** Settings ***
Documentation
...    Basic Test for Google.com in headless mode
Library    Collections
Library    DateTime
Library    Dialogs
Library    OperatingSystem
Library    SeleniumLibrary
Library    String
Library    RequestsLibrary
Library    JSONLibrary
Suite Setup    Setup Environment and Open Browser
Suite Teardown    Close All Browsers
*** Variables ***
${BROWSER}      HeadlessChrome
${SITE_URL}     https://www.google.com
*** Test Cases ***
Title Test
Title Should Be    Google
*** Keywords ***
Open Chrome Browser To URL
[Documentation]    Open Chrome browser and navigate to URL with browser options set
[Tags]  open_chrome_browser
${browserOptions}    Run Keyword If    'Headless' in '${BROWSER}'    Set Headless Chrome Options
Create Webdriver    Chrome    chrome_options=${browserOptions}
Go To    ${SITE_URL}
Maximize Browser Window
Browser timeout and speed
[Documentation]
...    Set browser timeout and speed
Set Selenium Timeout   30s
Set Selenium Speed   0s
Set Headless Chrome Options
[Documentation]
...     Set the Chrome options for running in headless mode.  Restrictions do not apply to headless mode.
[Tags]   headless_chrome_options
${chromeOptions}    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys
Call Method    ${chromeOptions}    add_argument    test-type
Call Method    ${chromeOptions}    add_argument    --headless
Call Method    ${chromeOptions}    add_argument    --disable-extensions
Call Method    ${chromeOptions}    add_argument    --disable-gpu
Call Method    ${chromeOptions}    add_argument    --disable-dev-shm-usage
Call Method    ${chromeOptions}    add_argument    --no-sandbox
[Return]  ${chromeOptions}
Setup Environment and Open Browser
[Documentation]
...    This keyword will establish the environment variables and open a browser
[Tags]    simple_test
Open Chrome Browser To URL
Browser timeout and speed
  1. 运行robot -b debug.log testgoogle.robot后,调试日志显示以下有趣的部分:
20200820 11:40:53.410 - DEBUG - Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/SeleniumLibrary/__init__.py", line 467, in run_keyword
return DynamicCore.run_keyword(self, name, args, kwargs)
File "/usr/local/lib/python3.6/dist-packages/SeleniumLibrary/base/robotlibcore.py", line 102, in run_keyword
return self.keywords[name](*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/SeleniumLibrary/keywords/element.py", line 569, in click_link
self.find_element(locator, tag='link').click()
File "/usr/local/lib/python3.6/dist-packages/SeleniumLibrary/base/context.py", line 74, in find_element
return self.element_finder.find(locator, tag, True, required, parent)
File "/usr/local/lib/python3.6/dist-packages/SeleniumLibrary/locators/elementfinder.py", line 76, in find
% (element_type, locator))
20200820 11:40:53.410 - INFO - +--- END KW: SeleniumLibrary.Click Link (74)
20200820 11:40:53.410 - INFO - +-- END KW: LoginHandler.User Logout (75)

对故障排除步骤有什么想法或建议吗?

非常感谢!

我仍然无法将其直接用于ChromeDriver,但是,我可以使用硒网格:

https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar

我启动了一个Hub(在一个容器中(,然后直接在运行测试的容器上启动一个节点,它就工作了。我仍在构建运行节点的容器(但它不是一个单独的容器……我必须添加一些配置(。下面是使用远程chrome驱动程序的机器人框架测试套件:

*** Settings ***
Documentation
...    This is a simple robot test to open a browser and check the title of a given website.
Library    Collections
Library    DateTime
Library    Dialogs
Library    OperatingSystem
Library    SeleniumLibrary
Library    String
Library    RequestsLibrary
Suite Setup    Setup Environment and Open Browser
Suite Teardown    Close All Browsers
*** Variables ***
${BROWSER}      HeadlessChrome
${SITE_URL}     https://www.google.com
*** Test Cases ***
Title Test
Title Should Be    Google
*** Keywords ***
Open Chrome Browser To URL
[Documentation]    Open Chrome browser and navigate to URL with browser options set
[Tags]  open_chrome_browser
${chromeOptions}    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
Call Method    ${chromeOptions}    add_argument    --headless
${ws}=    Set Variable    window-size=1920, 1080
Call Method    ${chromeOptions}    add_argument    ${ws}
Call Method    ${chromeOptions}    add_argument    --disable-extensions
Call Method    ${chromeOptions}    add_argument    --disable-gpu
Call Method    ${chromeOptions}    add_argument    --disable-dev-shm-usage
Call Method    ${chromeOptions}    add_argument    --ignore-certificate-errors
${remoteOptions}=    Call Method    ${chromeOptions}    to_capabilities
Create Webdriver    Remote   command_executor=http://<IP ADDR OF HUB>:4444/wd/hub    desired_capabilities=${remoteOptions}
Go To    ${SITE_URL}
Maximize Browser Window
Browser timeout and speed
[Documentation]
...    Set browser timeout and speed
Set Selenium Timeout   30s
Set Selenium Speed   0s
Setup Environment and Open Browser
[Documentation]
...    This keyword will establish the environment variables and open a browser
[Tags]    simple_test
Open Chrome Browser To URL
Browser timeout and speed

现在要弄清楚如何在容器中运行节点。。。

经过更多的研究,我只是使用公共SeleniumGrid3集线器和节点来解决我的问题。

https://github.com/SeleniumHQ/docker-selenium/tree/selenium-3/Hub

最新更新