python-3.x - 在 Ubuntu 18.04.2 LTS docker 容器上使用 Python 3.6.8 的机器人框架

标签 python-3.x docker robotframework ubuntu-18.04 google-chrome-headless

我一直无法在 Ubuntu v18.x 上找到使用 Python v3.x 运行 Robot Framework 的良好公共(public)镜像。一位同事构建了一个图像,我将其用作基础,但我的机器人脚本无法连接到该站点,并且我的轮子旋转太久了。是时候向社区寻求帮助了!
我有一个简单的机器人框架脚本(见下文),它连接到一个站点(chrome headless)并验证标题。它可以在我的 Ubuntu 工作站上找到,但是当我在该工作站上运行 docker 容器时 - 它只是无法连接。最初,我认为这是一个管道问题,但事实并非如此。以下是我采取的步骤以及适用于我的工作站但不适用于容器的机器人代码:

  • 确认连接正在从容器工作:curl -o /dev/null -s -w %{http_code} https://www.google.com (这将返回 200)
  • 确保我的工作站和容器上的 python3、pip3 和 ubuntu 的版本相同
  • 确保 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
    
  • 运行后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 一起工作,但是,我能够使用 selenium 网格让它工作:
    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
    
    
    现在要弄清楚如何在容器中运行节点......

    关于python-3.x - 在 Ubuntu 18.04.2 LTS docker 容器上使用 Python 3.6.8 的机器人框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63939822/

    相关文章:

    docker - 如何运行具有持久数据的 Prestashop docker 容器?

    jira - 使全局变量可在 Robot Framework 监听器中访问

    python - 在 CSV 的空列中写不带引号?

    python - 如果元组中的 string1 或 string2

    docker - Hyperledger Fabric Chaincode 安装失败 - 没有适用于 linux/arm64/v8 的匹配 list

    redirect - 在 docker 容器之间重定向的最佳实践

    python - 如何在机器人框架内运行sikuli脚本?

    android - 机器人框架 + appium 无法在 ionic 角度应用程序中按 ID 访问元素

    python - 正则表达式匹配单词,如果可选地后跟任何单词,除非后跟某些单词

    python-3.x - 为什么用 Process 调用多处理模块可以创建相同的实例?