Check out my answer here:
Jari
There should be plenty of resources online to get you started. The
headless gem linked to from the SO post provides a nice Ruby wrapper,
assuming you have Xvfb installed (should be available in package
managers on all modern Linux distros) - check its README for example
usage:
https://github.com/leonid-shevtsov/headless/blob/master/README.md
If you're using Hudson for CI there's also an Xvfb/Xvnc plugin that
will start/stop the virtual display for you.
You should be able to get Xvfb + Firefox working under X11 on OS X,
but I don't know what's needed. And I doubt any speed increase will be
significant enough to be worth the trouble.