如何 运行 headless REMOTE chrome 使用机器人框架

How to run headless REMOTE chrome using robot framework

我正在尝试 运行 chrome 无头地使用我的机器人框架测试套件。 我设法通过 python 使用 selenium 独立完成,如下所示:

options = webdriver.ChromeOptions()
options.add_argument('--headless')
my_driver = webdriver.Remote(command_executer=my_remote_address, desired_capabilities=options.to_capabilities)

下面的代码是我在robot中做的但是没用:

${options}=  Evaluate  sys.modules['selenium.webdriver'].ChromeOptions()  sys, selenium.webdriver
${options.add_argument}=  Set Variable  add_argument=--headless
Create WebDriver  Chrome  chrome_options=${options}

Open Browser   http://www.google.com   chrome

请尝试以下操作。 运行 测试设置中的关键字

Running the tests in chrome headless
    ${chrome options} =     Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
    Call Method    ${chrome options}   add_argument    headless
    Call Method    ${chrome options}   add_argument    disable-gpu
    Create Webdriver    Chrome    chrome_options=${chrome options}

要运行 headless,您需要设置参数并将它们转换为功能,以便在使用Remote Driver 选项时可以使用它们。这适用于 Open Browser 以及导航到 URL.

Create Webdriver 方式。
*** Settings ***
Library    Selenium2Library

Suite Teardown    Close All Browsers

*** Test Cases ***
Headless Chrome - Create Webdriver
    ${chrome_options} =     Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
    Call Method    ${chrome_options}   add_argument    headless
    Call Method    ${chrome_options}   add_argument    disable-gpu
    ${options}=     Call Method     ${chrome_options}    to_capabilities      

    Create Webdriver    Remote   command_executor=http://localhost:4444/wd/hub    desired_capabilities=${options}

    Go to     http://cnn.com

    Maximize Browser Window
    Capture Page Screenshot

Headless Chrome - Open Browser
    ${chrome_options} =     Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
    Call Method    ${chrome_options}   add_argument    headless
    Call Method    ${chrome_options}   add_argument    disable-gpu
    ${options}=     Call Method     ${chrome_options}    to_capabilities     

    Open Browser    http://cnn.com    browser=chrome    remote_url=http://localhost:4444/wd/hub     desired_capabilities=${options}

    Maximize Browser Window
    Capture Page Screenshot

试试这两个关键字:

Open Chrome
    [Arguments]    ${url}    ${lang}
    ${chrome_options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
    Call Method    ${chrome_options}    add_argument    headless
    Call Method    ${chrome_options}    add_argument    disable-gpu
    Create Webdriver    Chrome    chrome_options=${chrome_options}
    Go To    ${url}

Open Remote Chrome
    [Arguments]    ${url}    ${remote_url}    ${lang}
    ${chrome_options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
    Call Method    ${chrome_options}    add_argument    headless
    Call Method    ${chrome_options}    add_argument    disable-gpu
    Open Browser    ${url}    Chrome    remote_url=${remote_url}    desired_capabilities=${chrome_options.to_capabilities()}

第一个将启动本地 chrome,第二个将启动远程 chrome。如果您需要能够将远程与本地处理成一个关键字,您可以使用布尔参数围绕这两个创建一个包装器,该参数将确定调用哪个关键字。

${options}=  Evaluate  sys.modules['selenium.webdriver'].ChromeOptions()  sys, selenium.webdriver
Call Method    ${options}    add_argument    headless
Call Method    ${options}    add_argument    disable-gpu
Create WebDriver  Chrome  chrome_options=${options}
Goto  https://www.cnn.com/
Wait Until Page Contains    Politics today
Close Browser

在较新版本的 SeleniumLibrary (3.1.0) 和 Selenium (3.8.0) 中,您只需将浏览器设置为 headlesschrome 而不是 chrome

还有 headlessfirefox 可用。

例如

Open Browser      http://www.yoursite.com    headlesschrome

http://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Open%20Browser

您现在可以使用别名:

*** Settings ***
Library    SeleniumLibrary

*** Test Cases ***
Firefox
    Open Browser    http://robotframework.org/    headlessfirefox
    Capture Page Screenshot
    [Teardown]    Close All Browsers

Chrome
    Open Browser    http://robotframework.org/     headlesschrome
    Capture Page Screenshot
    [Teardown]    Close All Browsers

https://github.com/robotframework/SeleniumLibrary/pull/1011