How To Run Your Tests Headlessly with Chrome
This is a guest post from Chris Kenst.
If you want to run your tests headlessly on a Continuous Integration (CI) server you'll quickly realize that you can't with an out-of-the-box setup since there is no display output for the browser to launch in. You could use a third party library like Xvfb or PhantomJS but those can be hard to install and aren't guaranteed to be supported in the long run (like PhantomJS).
Enter Headless Chrome (link)
Starting with Chrome 59 (Chrome 60 for Windows) we can pass Chrome a few configuration options to enable headless mode.
Before we start make sure you’ve at least got Chrome 59 installed and are using the latest ChromeDriver version.
First let’s create a simple Selenium script. Like all test scripts, first we will pull in the requisite libraries:
# filename: headless_chrome.rb
Now we can create our
setup method where we will pass Chrome our headless options as command line arguments.
options = Selenium::WebDriver::Chrome::Options.new
@driver = Selenium::WebDriver.for :chrome, options: options
'-- headless' allows us to run Chrome in headless mode. The second argument is, according to Google, temporarily required to work around a few known bugs. The third argument is optional but gives us the ability to debug our application in another browser if we need to (using localhost:9222).
Now let's finish our test by creating our
expect(@driver.title).to eql 'The Internet'
Here we are loading a page, asserting on the title (to make sure we are in the right place), and taking a screenshot to make sure our headless setup is working correctly.
When we save our file and run it (e.g.
ruby headless_chrome.rb) here is what will happen:
- A headless chrome browser instance will open
- Test runs and captures a screenshot
- Browser closes
Hopefully this tip has helped you get your tests running smoothly locally (or on your CI Server). This is the best option since it's easy to setup, officially supported by the browser vendor, and has a consistent rendering experience to what an end-user would experience in a full instance of Chrome.
About The Author
Chris Kenst is a Software Quality advocate, Test Engineer, occasional blogger, and maintainer of an open source list of software testing conferences (link). You can also find him online most places as @ckenst.