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
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.
First we need to download the necessary files to run the VM.
- Sun’s VirtualBox – download the latest and greatest
- Ubuntu Desktop – comes with Firefox pre-installed and works great for our installation
Next install VirtualBox, open the application, click “New”, and follow the directions below:
Specify a name – I went with something original.
Allocate as much memory as you can afford as it will make a big difference in the performance of the VM.
Create a new virtual hard drive.
Accept the default values.
Specify “Fixed” size as it improves performance. (That could be a myth…)
Tell it how big and click “Create”.
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.
Allocate as much processing power as you can afford as well.
Any extra juice available for video memory will also likely improve test times.
The next step is to attach the Ubuntu ISO file we downloaded so it used as the installation disk.
Click “Choose disk” and navigate to the ISO file.
Select the ISO and click “Open”.
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:
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.
HiringThing is easy to use, intuitive online recruiting software that makes it easy to post jobs online, manage applicants and hire great employees.