Read more

How to hide your selenium browser window with "headless"

Ulrich Berkmueller
July 23, 2011Software engineer

We don't use the headless gem. Tests can run with Chrome in headless mode without additional libraries.

Note: While the solution in this card should still work, we prefer another solution now: Hide your Selenium browser window with a VNC server.


Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

If you would like to hide the annoying selenium browser window that always gets the focus and prevents you from working, you can use the headless gem Show archive.org snapshot . This note provides some instructions how you can get it to work with your cucumber acceptance tests using selenium webdriver as a capybara driver.

  1. Install xvfb if it is not installed yet on your system.

    sudo apt-get install xvfb
    
  2. Add the headless gem to your Gemfile.

    group :cucumber do
      ...
      gem 'headless'
    end
    
  3. Go to your features/support/env.rb file and include the following snippet:

    require 'headless'
    headless = Headless.new
    at_exit do
      headless.destroy
    end
    
    Before("@selenium,@javascript", "~@no-headless") do
      headless.start if Capybara.current_driver == :selenium
    end
    
    After("@selenium,@javascript", "~@no-headless") do
      headless.stop if Capybara.current_driver == :selenium
    end
    

    This starts an xvfb process, registers a hook to destroy the process on exit and starts and stops the headless mode if you are running a scenario
    that is tagged with @selenium or @javascript.

    If you don't want a specific scenario to hide the selenium browser window, you can tag it with @no-headless.

Note: If you are using paralell_tests to run your tests in parallel you have to take care of several things to get selenium to work. Please have a look at the note on running your tests in parallel.

Posted by Ulrich Berkmueller to makandra dev (2011-07-23 17:57)