> ## Content Index
> Fetch the complete content index at: https://www.kenst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How To Run Your Selenium Tests with Headless Chrome
- URL: https://www.kenst.com/how-to-run-your-tests-with-headless-chrome-ruby-version/
- Published: 2018-10-11T20:53:00.000Z
- Updated: 2022-06-09T18:29:28.000Z
- Author: Chris Kenst
- Tags: Ruby, Selenium, Test Design, Test Techniques, Test Automation, Tutorials, Testing

## The Problem

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 (tip 38)](http://elementalselenium.com/tips/38-headless?ref=kenst.com) or [PhantomJS (tip 46)](http://elementalselenium.com/tips/46-headless-ghostdriver?ref=kenst.com) but those can be hard to install and aren’t guaranteed to be supported in the long run (like PhantomJS).

## A Solution

Enter [Headless Chrome](https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md?ref=kenst.com). Headless is simply a mode you can put Chrome into that allows it to run without displaying on screen but still gets you the same great results. This is a better option than using Chrome in a Headless manner such as in a docker container where the the container actually uses Xvfb.

Starting with Chrome 59 (Chrome 60 for Windows) we can simply pass Chrome a few configuration options to enable headless mode.

## An Example in Ruby

Before we start make sure you’ve at least got the latest version of Chrome installed along with the latest version of ChromeDriver.

Let’s create a simple Selenium script (the example is posted below).

1. We will pull in the requisite libraries and then create our `setup` method where we will pass Chrome our headless option as a command line argument. The first add\_argument of ‘– headless’ allows us to run Chrome in headless mode. The second argument is, [according to Google](https://developers.google.com/web/updates/2017/04/headless-chrome?ref=kenst.com), 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).
2. Now let’s finish our test by creating our `teardown` and `run` methods:

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. Here’s what our screenshot looks like:

![](https://storage.ghost.io/c/64/2d/642d9fac-06bc-48ce-a22c-14219bd777e6/content/images/wordpress/2018/10/headless.png)

## Expected Behavior

When we save our file and run it (e.g. `ruby headless_chrome.rb`) here is what will happen:

1. An empty chrome browser instance will open
2. Test runs and captures a screenshot
3. Browser closes

## Outro

Hopefully this tip has helped you get your tests running smoothly locally or on your CI Server. Happy Testing!

### Additional References

- [Use a Docker Container to run Chrome Headlessly](https://www.kenst.com/how-to-run-your-selenium-tests-headlessly-in-docker/)

> This was originally written for and posted on the [Elemental Selenium](http://elementalselenium.com/tips/72-headless-chrome?ref=kenst.com) newsletter. I liked it so much I decided to cross post it with some updates.

Oh and if this article worked for you please consider sharing it or [buying me coffee!](https://ko-fi.com/ckenst?ref=kenst.com)