Selenium Headless Automated Testing in Ubuntu
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.
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.