How To: Run Selenium Headless Firefox in Ubuntu

Selenium Headless Automated Testing in Ubuntu

selenium headless firefox

Selenium Testing

Selenium is a automated web testing framework. It lets you to automate your web application for testing purpose. But in my experience, I have used Selenium in variety of situations except for testing. I have used Selenium from automating E-Commerce check-out pages to crawling information from a page which is javascript generated or AJAX contents.

As a programmer, I do not believe in using Selenium IDE which is just record and replay macro. It is very limited in functionality and does not scale well if you want to deploy in multiple servers. Then your choice is to use Selenium WebDriver which is flexible and let you run selenium headless in servers with no display.

Why to run Headless Selenium Testing?

If you want to create some robust process automation which needs to run 24X7 and you need reliability, then your only choice is to have Selenium in a server. But in order to run, Selenium needs to launch a browser. If there are no display to the machine, the browsers are not launched. So in order to use selenium, you need to fake a display and let selenium and the browser thinks they are running in a machine with a display. This setup lets you run round the clock processes and also adds reliability and scalability.

How to run Selenium headless firefox in ubuntu?

For this tutorial, we will focus on configuring and running selenium headless in Ubuntu using Mozilla Firefox as our primary browser.

Install Firefox headless in Ubuntu

Make sure you have the latest version of Firefox in your ubuntu machine. I have faced issues dues to incompatibility of Selenium server versions and Firefox version. In case you do not have firefox or using older version of firefox, follow the steps to upgrade firefox.

  • In /etc/apt/sources.list add the following line.
  • Run the following commands to upgrade or install latest version of Firefox to work with Selenium.
	sudo apt-get update
	sudo apt-get install firefox

If the commands execute successfully you should have the latest version of firefox in your ubuntu system now.

Install Xvfb – the X Virtual FrameBuffer

This piece of software emulates the framebuffer using virtual memory which lets you run X-Server in machines with no display devices. This service is required to make browsers run normally by making them believe there is a display available. To install xvfb in ubuntu or Debian, run the following command,

sudo apt-get install xvfb

Now lets run the xvfb service in a display number which is less likely to clash even if you add a display at later stage. For this tutorial, we will assume a display, 10.

sudo Xvfb :10 -ac

The parameter -ac makes xvfb run with access control off. The server should be running now.

Start browser headlessly in Ubuntu

Before you can run a browser, you need to set the environment variable DISPLAY with the display number at which xvfb is running. Before we add the environmental variable as a persistent one, let us check whether everything works as expected.

export DISPLAY=:10

If there was no error on the terminal, then you have successfully running firefox headlessly in Ubuntu. The command should keep running until you kill the process by pressing ctrl+ C or similar. There wont be any output.

If you have reached till here successfully, the next part will go like a breeze. Now that we have a browser running headless in Ubuntu server, we can run selenium server as you will run in your local machine. In the next part of this tutorial, I am explaning how to run the selenium stand alone server and use web drivers to connect using PHP.

  • Pingback: How to setup headless firefox browser Ubuntu Server | BlogoSfera

  • Vishal Khialani

    thank you writing the article however I am having some issues

    1) Xvfb :10 -ac
    I get an error [dix] Could not init font path element /var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType, removing from list!

    I searched and there are many articles on this but no nothing really worked

    example article I read :

    2) [email protected]:~# export DISPLAY=:10 [email protected]:~# firefox Xlib: extension “RANDR” missing on display “:10″.

    and I try using firefox then I get “RANDR” missing.

    Please advice what I can do here.

    I have tried:

    1) installing wine
    2) sudo apt-get -y install x-ttcidfont-conf cabextract ttf-mscorefonts-installer
    3) sudo dpkg-reconfigure x-ttcidfont-conf

    • Abraham itule

      Am having the EXACT same problems!

      • Vishal Khialani

        I fixed it. after u get the firefox error open a new ssh screen and type firefox again. If it says display not defined then define it again ie ‘export Displa…. ‘ and then type firefox. If you don’t get an error then its working. Now you can use selenium ! if u have a problem ping me.

        • Abraham itule

          Basically i have a ubuntu headless server, and the idea is there’s a crontab task running every 1 minute, checks for new commits on Git. If there’s a new commit, it pulls them, then runs selenium tests on them using Grunt. Then i generate some info-graph with stats of what failed, who committed it and bla bla bla.

          Where am stuck is the selenium tasks doesn’t seem to see firefox, so they fail silently. And since am running them using crontab, am not sure how to “open a new ssh screen and type firefox again”.

          • Alex Bel

            instead of

            Xvfb :10 -ac

            try running like this

            xvfb-run –server-num=10 your_script_path_and_name

            10 is just any display number that you like, in my case in cron it looked like this

            xvfb-run –server-num=10 phpunit test.php >> testlog.txt

            this utility does is a wrapper for Xvfb, but it executes for one command and you dont need export either.

        • Jay

          Hi Vishal

          I am also getting the error ‘Xlib: extension “RANDR” missing on display “:99″.

          After opening a new ssh and defining export DISPLAY=:99 i am getting :GLib-CRITICAL **: g_slice_set_config: assertion ‘sys_page_size == 0′ failed

          Error: cannot open display: :99

          Please help

  • Or Cohen

    xvfb package should come with a utility called xvfb-run, which achieves this goal exactly. I’m testing from python with nose so I just execute: `xvfb-run nosetests`

  • Pingback: Giocando con Selenium | in Web we Trust

  • Prasanna

    I followed the above instructions, ,I have one doubt do i require to run a Selenium Standalone server to run the tests in Ubuntu. I am not running my tests in Grid or through RemoteWebDriver.

  • cnd

    Probably a better idea to run VNC on the server so it does have a display – you can then use it to set stuff up and change settings etc, then run your automation and ignore it after that.

  • Shankar

    How to deploy same in Debian server..?

  • Majed Aly

    Thanks for the guide, I guess you should add this for Ubuntu and the correct command to add repository is
    add-apt-repository ppa:mozillateam
    and not adding ppa:mozillateam/firefox-stable at the end of /etc/apt/sources.list

  • Laicis Plecis

    Or You could just do “X :10″
    No need for Xvfb

  • vinz95

    I advise you to use PhantomJS.
    It’s faster (~50% of other browsers), and be used with the Selenium2 WebDriver.

    1) Install phantomjs on Linux
    2) Run it with: phantomjs –webdriver=4444 (4444 is the port of selenium2 webdriver)