I have recently setup a headless continuous integration test server that runs in a headless environment. It runs both my rspec tests and all of my capybara request tests. If I receive a failure in a request test, it will store a screenshot and the HTML code of the page.
There is a few key components to getting this setup correctly, I thought I would share insight on what I needed to do in order to get it working.
My setup consists of:
- A rails app with capybara and rspec tests.
- A headless server which has Xvfb, CruiseControl.rb and Firefox 3.6.x installed.
First off, you will need the following entries in your Gemfile:
1 2 3 4
Now, you will need to configure your
At the top of the file add:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
example_filename method is used to create a custom filename which
we use to store screenshots and html dumps from failed request tests.
save_screenshot method handles saving the screenshots and html
The next block allows us to run our tests in headless mode. Remember, you will need to have Xvfb installed to run the tests in headless mode.
The last line allows you to specify a custom firefox binary to use.
Because we have found latest versions of firefox do not play nice with
selenium (which capybara uses), we install firefox 3.6 into
~/firefox-3.6 and use
Now, inside of your
RSpec.configure do |config| block, put the
1 2 3 4 5 6 7 8 9 10
The first block sets the selenium driver for request tests. The second
block resets the driver back to a non-selenium driver and also calls the
save_screenshot method that we defined earlier.
Now, we will need a custom script that runs our tests in CruiseControl.rb
1 2 3 4 5 6 7 8 9 10
We use rvm, so the 3rd line handles loading the correct rvm environment. Then we reset the DB and start our test run.
We also ensure that the test script exits with the right exit code.
Now, we setup out
cruise_config.rb file as well.
1 2 3 4 5 6 7 8 9 10 11
This is a pretty standard cruise_config.rb file only extra thing is that we run the custom bash script we created.
Now cruise control should be able to run your tests, and if any request tests fail, you will get a screenshot and a html dump added to your build artifacts in CC.rb
- Ensure Xvfb is installed before running with HEADLESS=true
- Use an older version of firefox, it works best with selenium (use FIREFOX_BIN=path/to/bin)
- You need the fix_exit_code branch of headless, so ensure you have specified that in your Gemfile
So when you combine all of that together, you should be able to get a similar setup. If you have any questions please ask them in the comments!