问题
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
回答1:
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
回答2:
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
回答3:
Please try the below. Run the keyword in Test setup
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}
回答4:
Try out these two keywords:
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()}
First one will launch a local chrome, while the second one is to launch a remote chrome. If you need to be able to handle the remote vs. local into a single keyword, you can create a wrapper around these two with a boolean argument that will determine which keyword to call.
回答5:
${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
回答6:
I created example repository and instructions how to run Chrome and Firefox native, headless or with docker and also included comparison chart for run times. You can find examples on my github page https://github.com/shnigi/Docker-Headless-Testing-Demo
The main Idea is however In both tests to use the Selenium's webdriver API to tell chromedriver and geckodriver to launch browser with some arguments. SeleniumLibrary's Open Browser keyword does not have this functionality, but Create Webdriver does the trick.
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
You will need latest browsers and drivers installed.
回答7:
You can now use aliases:
*** 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
See https://github.com/robotframework/SeleniumLibrary/pull/1011
来源:https://stackoverflow.com/questions/46812155/how-to-run-headless-remote-chrome-using-robot-framework