Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run headless REMOTE chrome using robot framework

I'm trying to run chrome headless with my robot framework tests suites. I managed to do it independtly with python using selenium as follows:

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

The following code is what I did in robot but didn't work:

${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
like image 917
Dan W Avatar asked Oct 18 '17 14:10

Dan W


People also ask

How do I run Chromedriver in headless mode?

Post version 59, Chrome supports headless execution. ChromeOptions class is utilized to modify the default characteristics of the browser. The addArguments method of the ChromeOptions class is used for headless execution and headless is passed as a parameter to that method.

Does robot class work in headless mode?

In normal browser execution , it is working well but in case of headless execution it is not working. The Robot class can only interact with a visible window opened on desktop.


2 Answers

In newer versions of SeleniumLibrary (3.1.0) and Selenium (3.8.0) you can simply set the browser to headlesschrome instead of chrome.

There's also headlessfirefox available.

Ex.

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

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

like image 91
Joao Coelho Avatar answered Oct 17 '22 09:10

Joao Coelho


To run headless you need to set the arguments and convert them to capabilities so that they can be used when using the Remote Driver option. This works for both the Open Browser as well as the Create Webdriver way of navigating to a URL.

*** 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
like image 44
A. Kootstra Avatar answered Oct 17 '22 09:10

A. Kootstra