Using Headless Chrome with Selenium

While working on the second edition of my flask book, I was reviewing my Selenium tests, which allow me to automate a web browser and do end-to-end testing. In the current version of the book I recommend running these tests against Firefox. I thought this was a great opportunity to see how Headless Chrome works, as that eliminates the annoying browser window that pops out every time you run the tests.

The results are encouraging. This super short article describes what you need to do to set up Selenium to use the Headless Chrome browser.

Unit Test Structure

In case you haven't seen my book, the way I configure the tests that use Selenium within Python's unittest framework is shown below:

from selenium import webdriver

class SeleniumTestCase(unittest.TestCase):
    client = None

    def setUpClass(cls):
        # start Firefox
            cls.client = webdriver.Firefox()

        # skip these tests if the browser could not be started
        if cls.client:
            # create the application
            cls.app = create_app('testing')
            cls.app_context = cls.app.app_context()

            # create the database and populate with some fake data

            # start the Flask server in a thread

            # give the server a second to ensure it is up

    def tearDownClass(cls):
        if cls.client:
            # stop the flask server and the browser

            # destroy database

            # remove application context

    def setUp(self):
        if not self.client:
            self.skipTest('Web browser not available')

    def tearDown(self):

    # tests go here!

The setUpClass() method creates a Selenium client, which is stored in the client class variable, and also creates an application context and a database. Then a real Flask server is started in a background thread. You can't use the Flask test client for this type of test because the browser controlled by Selenium needs a real server it can connect to. The tearDownClass() just destroys all the resources created in setUpClass(). The setUp() method checks that a client instance exists, and if it doesn't, it tells the unit testing framework that the test needs to be skipped. This can happen if, for example, you did not have Firefox installed.

Replacing Firefox with Chrome Headless

The first thing you need to do to switch to Chrome is to install Chrome if you don't have it yet (obviously!), and then you need to install ChromeDriver, which is the little bit of glue that allows Selenium to send commands to Chrome and automate it. If you are on a Mac, then brew install chromedriver is all you need to do. On other platforms, download an installer from the ChromeDriver site: https://sites.google.com/a/chromium.org/chromedriver/downloads.

To switch to Chrome, you just need to change the initialization of the client class variable:

        # start Chrome
            cls.client = webdriver.Chrome()

But this will still use a regular Chrome window. If you want to use the headless option, you have to add options:

        # start Chrome
        options = webdriver.ChromeOptions()
            cls.client = webdriver.Chrome(chrome_options=options)

And that's it! Now you get your tests running in the same way as before, but on an invisible Chrome window that is not going to disrupt your other windows. If you want to switch back to the regular mode, just comment out the options.add_argument('headless') line and you'll get a visible window that you can watch while the tests run.

So far my experience with Headless Chrome is positive. Have you tried it? Let me know how it works for you.


  • #1 Mark ten Brinke said 2017-08-13T10:41:54Z

    Hi Miguel, Great work on your Flask Mega-Tutorial and the second edition of your Flask book. Is there a release date set for the second edition of the book? Mark.

  • #2 Miguel Grinberg said 2017-08-13T18:28:39Z

    @Mark: I don't have much control on the release date of the book because of all the stuff that needs to happen after I deliver my manuscript, but I can tell you that O'Reilly estimates it will be out January 25th 2018, as shown in the pre-order Amazon page here: https://www.amazon.com/Flask-Web-Development-Developing-Applications/dp/1491991739

Leave a Comment