Rails Testing — Leveraging VirtualBox for Browser Tests


A Rails app in its’ infancy can execute an entire test suite in a few seconds. There isn’t a lot of overhead involved because the complexity of interrelated models, controllers, and views is minimal. Tests, when run as a package ( rake test:functionals , etc. ) or with tools like Guard or Watchr, provide regular feedback that code is written correctly. New features are being developed, tests move from red to green, and everybody is happy eating the meat and potatoes of test-driven-development.

That is until… browser tests

Browser Tests – A Different Beast Entirely

What makes executing a browser test so different is that, unlike the other forms of testing, a browser test is executed from the perspective of an actual user clicking through your app. Capybara (by way of the Selenium 2.0 Webdriver) actually spins up Firefox to control it remotely.

When the Webdriver is controlling the browser there can be no interaction with the computer that could steal focus or interrupt an expected behavior.

Or to put it another way, browser tests can render your machine out of service until they complete. An accidental interruption can disrupt a test and cause it to fail where it may have otherwise succeeded.

The Solution: Virtualization


VirtualBox is virtualization tool managed by Oracle that enables you to run the operating system of your choice (with a few exceptions) as a windowed application.

For our purposes that means we can run an instance of our Rails app in enough isolation to allow browser tests while using the same code and database instance. It’s preferred to have the test environment match production as closely as possible so we’ll setup an Ubuntu 12.04 virtual machine (VM) and connect it to the host MySQL server running on my Mac. We’ll also create private host-only network to ensure that our file shares are always available regardless of what network we’re joined to.

In this blog post we’ll cover the lengthy steps of installing VirtualBox and configuring an Ubuntu Desktop installation. The remaining steps we’ll cover in part two of our series.

Necessary Downloads

First we need to download the necessary files to run the VM.

Configuring VirtualBox

Next install VirtualBox, open the application, click “New”, and follow the directions below:

Specify a name – I went with something original.

Step 1:

Allocate as much memory as you can afford as it will make a big difference in the performance of the VM.

Step 2:

Create a new virtual hard drive.

Step 3:

Accept the default values.

Step 4:

Specify “Fixed” size as it improves performance. (That could be a myth…)

Step 5:

Tell it how big and click “Create”.

Step 6:

Next up, we need to choose some settings to get the VM to install correctly. If at any time you see a red icon that says “Non-optimal settings detected” you should pay attention. Hover over the red icon for more directions if that comes up.

Step 7:

Allocate as much processing power as you can afford as well.

Step 8:

Any extra juice available for video memory will also likely improve test times.

Step 8.1: Yes, I forgot one

The next step is to attach the Ubuntu ISO file we downloaded so it used as the installation disk.

Step 9:

Click “Choose disk” and navigate to the ISO file.

Step 10:

Select the ISO and click “Open”.

Step 11:

You are now ready to start the installation process.

Installing Ubuntu is incredibly easy – maybe even easier than Windows®. Click “Start” and the Ubuntu installation will begin as nothing is yet installed on your virtual disk and VirtualBox knows it.

Once it completes should see a screen like so:

Step 12:

Ubuntu Install Complete: Stay Tuned for Part 2

This concludes the first part of our series in Leveraging VirtualBox for Browser Tests . We’ll conclude the series by addressing database configuration quirks, how to get file shares properly setup, and work around some of the more annoying issues you may run into.


Author: HiringThing

HiringThing is easy to use, intuitive online recruiting software that makes it easy to post jobs online, manage applicants and hire great employees.