Python 3.x 在Ubuntu 18.04.2 LTS docker容器上使用Python 3.6.8的Robot框架

Python 3.x 在Ubuntu 18.04.2 LTS docker容器上使用Python 3.6.8的Robot框架,python-3.x,docker,robotframework,ubuntu-18.04,google-chrome-headless,Python 3.x,Docker,Robotframework,Ubuntu 18.04,Google Chrome Headless,我一直找不到一个在Ubuntu v18.x上使用PythonV3.x运行Robot框架的良好公共映像。一位同事创建了一个图像,我正在使用它作为基础,但我的机器人脚本无法连接到该站点,我已经旋转了太长时间。是时候向社区寻求帮助了 我有一个简单的机器人框架脚本(见下文),它连接到一个站点(chrome headless)并验证标题。它在我的Ubuntu工作站上运行,但当我在工作站上运行docker容器时,它就是无法连接。起初,我认为这是管道问题,但事实并非如此。以下是我所采取的步骤,以及在我的工作站

我一直找不到一个在Ubuntu v18.x上使用PythonV3.x运行Robot框架的良好公共映像。一位同事创建了一个图像,我正在使用它作为基础,但我的机器人脚本无法连接到该站点,我已经旋转了太长时间。是时候向社区寻求帮助了

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

  • 确认连接正在从容器中工作:
    curl-o/dev/null-s-w%{http_-code}https://www.google.com
    (返回一个200)

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

    *** 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
    
  • 确保pip3库是相同的(除了一些我知道我不需要的库,是的,我知道我安装了比我需要的多的库)-下面是一个
    pip3 freeze
    命令的列表:

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

  • 运行
    robot-b debug.log testgoogle.robot
    后,调试日志显示以下有趣的内容:
  • 对故障排除步骤有何想法或建议


    多谢

    我仍然无法使用ChromeDriver direct实现此功能,但是,我能够使用selenium网格实现此功能:

    我启动了一个集线器(在一个容器中),然后直接在运行测试的容器上启动了一个节点,它成功了。我仍在构建运行节点的容器(虽然它不是一个单独的容器,但我必须添加一些配置)。以下是使用远程chrome驱动程序的robot framework测试套件:

    *** 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
    
    
    ***设置***
    文档
    ...    这是一个简单的机器人测试,用于打开浏览器并检查给定网站的标题。
    图书馆藏书
    图书馆日期时间
    图书馆对话
    图书馆操作系统
    硒库
    库字符串
    图书馆申请图书馆
    套件安装环境并打开浏览器
    套件拆卸关闭所有浏览器
    ***变数***
    ${BROWSER}HeadlessChrome
    ${SITE\u URL}https://www.google.com
    ***测试用例***
    标题测试
    标题应该是谷歌
    ***关键词***
    打开Chrome浏览器至URL
    [文档]打开Chrome浏览器并导航到设置了浏览器选项的URL
    [标签]打开浏览器
    ${chromeOptions}评估sys.modules['selenium.webdriver'].chromeOptions()sys,selenium.webdriver
    调用方法${chromeOptions}add_参数--headless
    ${ws}=Set变量窗口大小=19201080
    调用方法${chromeOptions}添加参数${ws}
    调用方法${chromeOptions}add_参数--禁用扩展
    调用方法${chromeOptions}add_参数--禁用gpu
    调用方法${chromeOptions}add_参数--禁用dev shm用法
    调用方法${chromeOptions}add_参数--忽略证书错误
    ${remoteOptions}=调用方法${chromeOptions}以实现_功能
    创建Webdriver远程命令\u executor=http://:4444/wd/hub所需的\u功能=${remoteOptions}
    转到${SITE\u URL}
    最大化浏览器窗口
    浏览器超时和速度
    [文件]
    ...    设置浏览器超时和速度
    设置Selenium超时30秒
    将Selenium速度设置为0秒
    设置环境并打开浏览器
    [文件]
    ...    此关键字将建立环境变量并打开浏览器
    [Tags]简单测试
    打开Chrome浏览器至URL
    浏览器超时和速度
    

    现在,为了弄清楚如何在容器中运行节点…

    我仍然无法使用ChromeDriver direct实现这一点,但是,我能够使用selenium网格实现这一点:

    我启动了一个集线器(在一个容器中),然后直接在运行测试的容器上启动了一个节点,它成功了。我仍在构建运行节点的容器(虽然它不是一个单独的容器,但我必须添加一些配置)。以下是使用远程chrome驱动程序的robot framework测试套件:

    *** 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
    
    
    ***设置***
    文档
    ...    这是一个简单的机器人测试,用于打开浏览器并检查给定网站的标题。
    图书馆藏书
    图书馆日期时间
    图书馆对话
    图书馆操作系统
    硒库
    库字符串
    图书馆申请图书馆
    套件安装环境并打开浏览器
    套件拆卸关闭所有浏览器
    ***变数***
    ${BROWSER}HeadlessChrome
    ${SITE\u URL}https://www.google.com
    ***测试用例***
    标题测试
    标题应该是谷歌
    ***关键词***
    打开Chrome浏览器至URL
    [文档]打开Chrome浏览器并导航到设置了浏览器选项的URL
    [标签]打开浏览器
    ${chromeOptions}评估sys.modules['selenium.webdriver'].chromeOptions()sys,selenium.webdriver
    调用方法${chromeOptions}add_参数--headless
    ${ws}=Set变量窗口大小=19201080
    调用方法${chromeOptions}添加参数${ws}
    调用方法${chromeOptions}add_参数--禁用扩展
    调用方法${chromeOptions}add_参数--禁用gpu
    调用方法${chromeOptions}add_参数--禁用dev shm用法
    调用方法${chromeOptions}add_参数--忽略证书错误
    ${remoteOptions}=调用方法${chromeOptions}以实现_功能
    创建Webdriver远程命令\u executo
    
    *** 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