Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save UKNC/f51c8fdf3232e4aba39ce87b59cc9b7b to your computer and use it in GitHub Desktop.
Save UKNC/f51c8fdf3232e4aba39ce87b59cc9b7b to your computer and use it in GitHub Desktop.
Install headless Firefox on CentOS 6 for Selenium automation

Abstract

In the manual the steps required to install headless Firefox on CentOS 6 are described. Headless Firefox may be needed to perform GUI automated testing.

All steps of this manual have been validated on CentOS 6.8. It should not be a problem to adapt the solution to other distros.

Steps

Upgrade yum

yum upgrade

Install firefox from the official distro

Note: it may not be the latest version.

yum install -y firefox 

Install flash plugin

Your Firefox installation probably needs Adobe Flash plugin to render flash objects.

Install Adobe repo

yum -y install http://linuxdownload.adobe.com/linux/x86_64/adobe-release-x86_64-1.0-1.noarch.rpm

The flash plugin itself

yum install flash-plugin

Install X server and Co.

yum groupinstall "X Window System" "Desktop" "Fonts"

Install the required extensions on Firefox

Assuming you're working from Linux, use your terminal to get into your remote host with X tunneling enabled issuing

ssh -X root@host

Being inside the remote host, issue

firefox

to run host's Firefox on your desktop.

You probably need to wait several seconds (or minutes, depending on your connection speed) to get host's Firefox to be running on your machine.

Install all needed extensions, e.g. Adblock Plus.

Install ImageMagick

You have only two ways to get the feedback from your headless Firefox: (1) take a screenshot from the virtual display it runs on or (2) get Firefox's console logs to some server you arranged for that. To take screenshot you need ImageMagick utility.

yum install -y ImageMagick

Install Xvfb

This is virtual display driver that allows Firefox to send its GUI output to virtual display.

yum install -y Xvfb

Run Xvfb to create virtual display on which headless Firefox will run

Xvfb :19 -screen 0 1024x768x16 &

Run Firefox on the virtual display

DISPLAY=:19 firefox "http://www.yahoo.com" 

This will force Firefox to run on virtual display 19 created by Xvfb.

Take screenshot of the virtual display on which Firefox runs

DISPLAY=:19 import -window root screenshot.png

Install and configire consoleexport

You may absolutely want to install consoleexport extension to be able to send Firefox's console logs to some remote server. There is no other convineint way to get console logs from headless Firefox.

consoleexport is firebug's friend therefore you install first firebug and then consoleexport:

  1. Install firebug: https://addons.mozilla.org/en-US/firefox/addon/firebug/
  2. Install consoleexport: https://addons.mozilla.org/en-US/firefox/addon/consoleexport/?src=userprofile

To make consoleexport to send logs automatically to remote server you need to set three things in Firefox's about:config:

  1. extensions.firebug.consoleexport.active = true to activate atomatic sending of logs to the remote server
  2. extensions.firebug.consoleexport.serverURL = ip:port to set remote server to which the logs are sent
  3. extensions.firebug.allPagesActivation = on to enable firebug on every page; only in that case cosoleexport will send automatically console logs to remote server

Note: DNS names are not resolved on the serverURL field, so you need to put IP there.

The above settings will cause headless Firefox to send console logs to the supplied remote server in XML format. Here is the code of the server written in NodeJS that prints sent log messages:

/* File: server.js
   Run with: node server.js
*/
const express     = require('express');
const XmlStream   = require('xml-stream');
const bodyParser  = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
 
app.post('/', (req,res)=>{
  req.setEncoding('utf8');
  var xml = new XmlStream(req);
  xml.on('endElement: msg', data=>{
    console.log(data);
  });
  xml.on('end', ()=>{
    res.end();
  });
});
app.listen(8100);

If that server runs on 34.2.3.5:8080 then set extensions.firebug.consoleexport.serverURL=34.2.3.5:8080.

Install lastest Oracle Java

Get Java from https://www.java.com/en/download/manual.jsp#lin and install it. For example, JDK8u112 RPM is installed as

rpm -ivh jdk-8u112-linux-x64.rpm 

Verify Java version you're running after installation:

[root@NQPC003 ~]# java -version
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)
[root@NQPC003 ~]# 

Get and run Selenium standalone driver

Get Selenium from http://www.seleniumhq.org/download/. Running Selenium 3.0.1:

java -jar selenium-server-standalone-3.0.1.jar &

Make sure Selenium listens to connections:

[root@NQPC003 ~]# netstat -tlpn | grep 4444                                                                                 
tcp        0      0 0.0.0.0:4444                0.0.0.0:*                   LISTEN      29040/java                           
[root@NQPC003 ~]#    

Automate headless Firefox

@moose-byte
Copy link

I am installing firefox slightly differently. I'm manually putting it in /usr/local/ because there is an existing firefox installation that is being used by other automation tests. Did you have any issues with GTK3 not being available?

XPCOMGlueLoad error for file /usr/local/firefox60/libmozgtk.so:
libgtk-3.so.0: cannot open shared object file: No such file or directory
Couldn't load XPCOM.

@richardmat
Copy link

Running OEL 6.10
firefox-52.8.0-1.0.1.el6_9.x86_64
xorg-x11-server-Xvfb-1.17.4-17.0.1.el6.x86_64

As soon as firefox is executed, xvfb aborts with a segmentation fault -
(EE) Backtrace:
(EE) 0: Xvfb (xorg_backtrace+0x51) [0x578381]
(EE) 1: Xvfb (0x400000+0x17c9f9) [0x57c9f9]
(EE) 2: /lib64/libpthread.so.0 (0x7f16007cb000+0xf7e0) [0x7f16007da7e0]
(EE)
(EE) Segmentation fault at address 0x0
(EE)
Fatal server error:
(EE) Caught signal 11 (Segmentation fault). Server aborting

Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment