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

时间:2017-10-18 14:07:31

标签: robotframework selenium2library

我试图用我的机器人框架测试套件运行chrome headless。 我设法用pylen使用selenium独立完成,如下所示:

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

以下代码是我在机器人中所做的,但没有工作:

${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

7 个答案:

答案 0 :(得分:15)

在较新版本的SeleniumLibrary(3.1.0)和Selenium(3.8.0)中,您只需将浏览器设置为f.defer(1000)(1, 2);,而不是headlesschrome

还有chrome可用。

例如。

headlessfirefox

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

答案 1 :(得分:10)

要运行无头,您需要设置参数并将其转换为功能,以便在使用Remote Driver选项时可以使用它们。这适用于Open Browser以及导航到网址的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

答案 2 :(得分:2)

请尝试以下方法。在测试设置

中运行关键字
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}

答案 3 :(得分:0)

尝试以下两个关键字:

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。如果您需要能够将远程与本地处理为单个关键字,则可以使用布尔参数创建这两个关键字的包装器,该参数将确定要调用的关键字。

答案 4 :(得分:0)

${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

答案 5 :(得分:0)

我创建了示例存储库并说明了如何运行Chrome和Firefox本机,无头或使用docker,还包括运行时的比较图表。您可以在我的github页面https://github.com/shnigi/Docker-Headless-Testing-Demo

上找到示例

然而,主要的想法是在两个测试中使用Selenium的webdriver API告诉chromedriver和geckodriver使用一些参数启动浏览器。 SeleniumLibrary的Open Browser关键字没有此功能,但Create Webdriver可以解决问题。

Open Headless Chrome Browser to Page
  ${chrome_options}=    Evaluate    
  sys.modules['selenium.webdriver'].ChromeOptions()    sys
  Call Method    ${chrome_options}    add_argument    test-type
  Call Method    ${chrome_options}    add_argument    --disable-extensions
  Call Method    ${chrome_options}    add_argument    --headless
  Call Method    ${chrome_options}    add_argument    --disable-gpu
  Call Method    ${chrome_options}    add_argument    --no-sandbox
  Create Webdriver    Chrome    chrome_options=${chrome_options}
  Set Window Size    1920    1080
  Go To    ${PAGE URL}

Firefox true headless
  ${firefox options}=     Evaluate    sys.modules['selenium.webdriver'].firefox.webdriver.Options()    sys, selenium.webdriver
  Call Method    ${firefox options}   add_argument    -headless
  Create Webdriver    Firefox    firefox_options=${firefox options}
  Set Window Size    1920    1080
  Go To    ${PAGE URL}
  Page Should Be Open

您需要安装最新的浏览器和驱动程序。

答案 6 :(得分:0)

您现在可以使用别名:

*** 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