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

问题描述

我一直找不到在Ubuntu v18.x上使用Python v3.x运行Robot Framework的良好公共映像。一位同事制作了一个图像,但我正在将其用作基础,但是我的机器人脚本无法连接到该站点,并且我旋转轮子的时间太长了。是时候向社区寻求帮助了!

我有一个简单的机器人框架脚本(请参见下文),该脚本连接到站点(无头chrome)并验证标题。它可以在我的Ubuntu工作站上正常工作,但是当我在该工作站上运行docker容器时-它只是无法连接。最初,我认为这是一个棘手的问题,但事实并非如此。以下是我已执行的步骤,以及可在我的工作站而非容器上运行的机器人代码

  1. 确认容器中的连接正在运行:
    curl -o /dev/null -s -w %{http_code} https://www.google.com(返回200)

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

  3. 确保pip3库是相同的(除了一些我不需要的库,是的,我知道我安装了比我需要的库更多的库)-以下是{{ 1}}命令:

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
  1. 运行*** 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 后,调试日志将显示以下有趣的位:
robot -b debug.log testgoogle.robot

对故障排除步骤有任何想法或建议吗?

非常感谢!

解决方法

我仍然无法通过ChromeDriver直接使用它,但是,我可以使用硒网格来使它使用:

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

我启动了一个集线器(在容器中),然后在容器上直接运行了一个节点,该测试成功了。我仍在构建运行节点的容器(尽管它不能作为单独的容器工作……我必须添加一些配置)。以下是使用远程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

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

,

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

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