Running Integration Tests in WebKit Without a Browser
By Mike Farmer
December 8, 2011
But maybe there’s a different scenario at play here. What if you don’t do your development in an environment that has a browser? At End Point, we are strong advocates of doing development on the same environment that your app is going to run on. It eliminates unexpected issues down the road. We believe in it so much, actually, that we’ve created DevCamps that allows you to setup development environments on a server.
Obviously, your selenium based tests are not going to work here without some work to get it to run headless.
The good folks at thoughtbot have come up with a great solution to this and it is called capybara-webkit. It assumes that you are using capybara for your testing framework. If you are using webrat, the transition is fairly smooth. You’ll probably only need to change a few minor details in your tests.
What capybara-webkit does for you is enable you to run your tests inside of WebKit. This will simulate an environment that will be very close to what you would see in Google Chrome or Safari as well as many mobile browsers. I’ve found that except for some edge cases, it covers Firefox and IE as well.
To install capybara-webkit you will need to install the Qt development toolkit. It’s fairly straight forward so I’ll just refer you to the github wiki page for instructions for the various platforms. In Ubuntu, I just ran the following:
sudo apt-get install libqt4-dev
If you are installing on a server environment, you’ll also need to install Xvfb. You can do that in Ubuntu with the following command:
sudo apt-get install xvfb
It’s a little outside the scope of this blog post to go into other little things you need to setup with xvfb. The important thing is that you set it up to run on display 99. Another important note, is that you don’t have to set it up to run on boot. We will be starting it up when we run our tests if it isn’t running.
The next step is to configure your cucumber tests to use the capybara-webkit driver. To do that, add
to your Gemfile in the development and test group. Then in your env.rb file for cucumber add the following lines:
In some cases, I’ve found it helpful to also specify a server port and app_host as follows:
Capybara.server_port = '8000' Capybara.app_host = 'http://localhost:8000'
Now your tests are setup to run in WebKit. The final step is running the tests. To do this, you’ll need to run them from within xvfb. You can do that with the following command:
xvfb-run bundle exec cucumber
I’ve created an alias for this and dropped it in my .bashrc file. Here’s my entry, but you can set it up anyway you’d like.
alias xcuke="xvfb-run bundle exec cucumber"
Now running tests is a simple as running xcuke from the root of my Rails application.
Thanks to the guys at thoughtbot for putting together this awesome gem.