Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save textarcana/5855427 to your computer and use it in GitHub Desktop.
Save textarcana/5855427 to your computer and use it in GitHub Desktop.
Headless Selenium on CentOS 6.3 (Mac XWindows / X11 / Xvfb / Headless Firefox / Selenium howto)

XWindows for Headless Selenium

X Wing art by Paul Harckham

How to set up a Headless Selenium Testing environment for CentOS 6.3.

On your CentOS 6.3 host

Follow these steps to set up a CentOS 6.3 host to run headless Selenium tests with Firefox.

  1. sudo yum -y install firefox Xvfb libXfont Xorg
  2. sudo yum -y groupinstall "X Window System" "Desktop" "Fonts" "General Purpose Desktop" (installation takes a few minutes)
  3. Launch an XWindows Virtual Frame Buffer(XVFB) session on display port 99: Xvfb :99 -ac -screen 0 1280x1024x24 &
  4. Tell all XWindows applications in this terminal session to use the new Xvfb display port: export DISPLAY=:99
  5. Start the Selenium server, eg: java -jar /opt/selenium-server-standalone.jar &
  6. Now start your Selenium tests. The Selenium server should spawn a headless Firefox instance and your tests should run successfully.

On your Mac

How to install an XWindows terminal so that you can connect to your CentOS 6.3 host and run your Selenium tests in a graphical Web browser. This capability will be invaluable whenever you need to debug Selenium tests on one of your remote hosts.

Running Selenium tests in the GUI via XWindows

  1. Open XQuartz and ssh to your CentOS host using the -X option, eg: ssh -X user@example.com
  2. On the remote host, type export DISPLAY=localhost:10.0
  3. Start the Selenium server in the same terminal session, eg: java -jar /opt/selenium-server-standalone.jar &
  4. Now simply run your tests and you will see Firefox start up and run in the GUI, just like on the destkop!

Note that it's much slower to execute the tests under XWindows vs. Xvfb.

Connecting to a remote XWindows host

  1. Open XQuartz and ssh to your CentOS host using the -X option, eg: ssh -X user@example.com
  2. On the remote host, type export DISPLAY=localhost:10.0
  3. On the remote host, type firefox & and after a long pause you should see Firefox start up in a GUI window.

Installing XQuartz

XQuartz replaces the old X11 terminal that was part of Mac OS up until Mountain Lion. If you have an older version of Mac OS X then X11 should already be installed, and you can mentally replace "XQuartz" with "X11" throughout these instructions.

  1. Get XQuartz here. http://xquartz.macosforge.org/landing/ (The download will take a few minutes.)
  2. Run the installer.
  3. Log out and log back in.
  4. That's it! XQuartz should now be installed.

Further Reading

  1. Xvfb + Firefox
  2. How to connect to remote X11 hosts from a Mac
  3. Headless Cucumbers And Capybaras With Selenium And Hudson
  4. Centos: Installing graphical X Window System
  5. Problems With Bundling Xvfb Into Application on StackOverflow
  6. XQuartz Users FAQ
  7. Xvfb Wikipedia entry
  8. X-Wing art by Paul Harckham
@iammichiel
Copy link

sudo yum -y groupinstall "X Window System" "Desktop" "Fonts" "General Purpose Desktop"

How is that headless? You install the whole graphical environment!

@xiankai
Copy link

xiankai commented Oct 30, 2013

Seems to me like it's just installing the prerequisites for the virtual frame buffer (Xvfb), which itself is headless.

@manukoshe
Copy link

what's the point installing whole graphical environment and then run headless? all these packages are not needed for xvfb

@shssoichiro
Copy link

The command sudo yum -y groupinstall "X Window System" "Desktop" "Fonts" "General Purpose Desktop" doesn't appear to be necessary to get selenium-server to work. In fact, all it did was make my development VM unbootable.

Other than that, this was very helpful. Just skip step 2 when setting up the CentOS box and all is good.

@englianhu
Copy link

Tried to follow the procedures but step 2 gotta error.

$ sudo yum -y groupinstall "X Window System" "Desktop" "Fonts" "Geberal Purpose Desktop"
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.i3d.net
 * epel: mirrors.coreix.net
 * extras: mirror.i3d.net
 * updates: mirror.i3d.net
- Warning: Group basic-desktop does not have any packages to install.
- Warning: group Geberal Purpose Desktop does not exist.
- Maybe run: yum groups mark install (see man yum)
- No packages in any requested group available to install or update

@shssoichiro tried to proceed further but there is another error on accessing selenium.jar.

[root@scibrokes ~]# yum groups mark install "X Window System" "Desktop" "Fonts" "Geberal Purpose Desktop"
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
 * base: mirror.i3d.net
 * epel: mirrors.coreix.net
 * extras: mirror.i3d.net
 * updates: mirror.i3d.net
Marked install: X Window System,Desktop,Fonts,Geberal Purpose Desktop
[root@scibrokes ~]# Xvfb :99 -ac -screen 0 1280x104x24 &
[1] 4560
[root@scibrokes ~]# Initializing built-in extension Generic Event Extension
Initializing built-in extension SHAPE
Initializing built-in extension MIT-SHM
Initializing built-in extension XInputExtension
Initializing built-in extension XTEST
Initializing built-in extension BIG-REQUESTS
Initializing built-in extension SYNC
Initializing built-in extension XKEYBOARD
Initializing built-in extension XC-MISC
Initializing built-in extension XINERAMA
Initializing built-in extension XFIXES
Initializing built-in extension RENDER
Initializing built-in extension RANDR
Initializing built-in extension COMPOSITE
Initializing built-in extension DAMAGE
Initializing built-in extension MIT-SCREEN-SAVER
Initializing built-in extension DOUBLE-BUFFER
Initializing built-in extension RECORD
Initializing built-in extension DPMS
Initializing built-in extension Present
Initializing built-in extension X-Resource
Initializing built-in extension XVideo
Initializing built-in extension XVideo-MotionCompensation
Initializing built-in extension SELinux
Initializing built-in extension GLX

[root@scibrokes ~]# export DISPLAY=:99
[root@scibrokes ~]# java -jar /opt/selenium-server-standalone.jar &
[2] 4616
- [root@scibrokes ~]# Error: Unable to access jarfile /opt/selenium-server-standalone.jar

Here is another error during install seleniuim http://www.thelinuxdaily.com/2011/07/how-to-install-and-use-selenium-server-in-linux/#comment-44118

@horiab
Copy link

horiab commented Mar 7, 2016

Hi - I followed these instructions above, but ran into an issue with Firefox not working due to some library mismatches in RHEL 7.1. After trying several things to no avail, I uninstalled the default firefox package (yum delete firefox) and installed specifically the .i686 package (yum install firefox.i686). Which I know seems counterintuitive, since RHEL is running on the x86_64 architecture, but for some reason, this did it for me.
Just letting other people know this might work, since I spent a good few days dabbling "in the dark".

@mehdi-sabouri
Copy link

I skip the step 2 as @shssoichiro suggested, It works fine! Thank you, both!

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