Last Updated: February 25, 2016
·
715
· gacha

Headless capybara with selenium

Install sudo apt-get install xvfb. Then save folowing code in headless file, make it executable and put it on $PATH, I prefere to put it in ~/bin - reload console.

#!/bin/sh
if [ $(pidof -x Xvfb| wc -w) -eq 0 ]; then
  Xvfb -ac -screen scrn 1024x768x24 :5.0 > /dev/null 2>&1 &
fi
export DISPLAY=:5.0
$@

Now just go to your project and run your test like headless rspec spec/ or headless bundle exec rake.

Idea is simple, any command prefixed with headless is executed into another (5.0) virtual window, so capybara will open a real Firefox browser, but it will not be visible in main (0.0) window.