Rails Testing — Leveraging VirtualBox for Browser Tests (Part 2)


Let Us Proceed

In this second installment we’ll touch on the remaining tasks involved in setting up an Ubuntu VirtualBox as your dedicated browser testing platform. If you haven’t already read the first post in this series, click here to learn more.

VirtualBox Networking

After we’ve installed Ubuntu we need to configure networking with two adapters. Two? Yep, here’s why:

  • Bridged Adapter – this is the most common means of connecting your VirtualBox to the Internet. A virtual network adapter is created and bound to a physical adapter (WiFi, Ethernet, etc.) on the host machine for proxying network requests.
  • Host-Only Adapter – this is very helpful means of creating a private network between host and VM so that all requests live in a specific subnet. It makes working with Firewall issues, DNS issues, and a slew of other networking issues much easier to diagnose. If you only utilize the Bridged adapter then connections by way of host name or IP are prone to change as often as you change networks (work, home, coffee shop, etc.) – net result: pardon the pun, frustration.

First setup the bridged adapter like so:

Step 15: Bridged Adapter

Click ‘OK’ and you’re set. Shut down VirtualBox before proceeding to the next step as it is a little more complicated. VirtualBox doesn’t create the Host-Only adapter out of the box. Open up a terminal window and issue the following:

$ VBoxManage hostonlyif create Now you re-open VirtualBox, select your Ubuntu image, click Settings … Network and we’ll add our second adapter like so:
Step 16: Host-Only Adapter

The Host-Only adapter creates a network with two IP’s when the VM has been started:

  • Host (MacBook Pro):
  • VM (Ubuntu):

Okay, now we’re getting somewhere. Let’s keep going.

Ubuntu Hosts File

Ubuntu keeps the hosts file at /etc/hosts . I recommend editing the it to include the following:

# mine says "rubybook" the-name-of-your-host-machine# this will likely already be there127.0.1.1 seth-VirtualBox # if this is missing you will experience great anguish when your code works most but not all of the time127.0.0.1 localhost 

Installing RVM

Next up install the latest RVM and Ruby dependencies. Before we install RVM however we need to be sure we have the right libraries available to RVM so that Ruby will compile with the needed requirements satisfied.

These are required for Ruby to be configured properly by RVM .
$ sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion pkg-config git-core libreadline-dev mysql-client libmysql-ruby libmysqlclient-dev

Now download RVM ’s and let’s get rolling.
$ curl -L https://get.rvm.io | sudo bash -s stable

This will install the latest stable RVM . Next up install the Ruby version your application is running. We’re using version 1.9.3-p125.

$ rvm install 1.9.3-p125$ rvm use --default 1.9.3-p125$ rvm reload

This will both install Ruby and set version 1.9.3-p125 as your default.

We’re making progress now. You should be able to type:

ruby -v and see something like this: _ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]_


Side note: If you’ve wondered why we don’t use apt-get to update Ruby or RubyGems please refer to Ryan Bigg’s post.

Connect to MySQL

When your browser tests run they will require a connection to a database server. If you’re using SQL ite then no configuration is necessary since it’s all file-based. Rails and Rake both know how to initialize and connect based on convention. If you’re connecting with MySQL on the other hand you’ll have to make sure that a few things are in order. First, find the my.cnf on your host machine. I found mine at:
$ /usr/local/etc/my.cnf

Open it up and find the heading [mysqld]

event_scheduler = ON
collation_server = utf8_unicode_ci
character_set_server = utf8

bind-address =
port = 3306
socket = /tmp/mysql.sock
max_connections = 20


tag: tech
The important key to note here is bind-address . Unless bind-address is set to you won’t be able to access the server from any IP besides the one listed. While probably obvious, this is a security measure to prevent unauthorized access to your database.

From here we should be ready to test the connection between your host MySQL server and the Ubuntu client. My machine is rubybook. Insert your host instead or use the client IP like so:

$ mysql -h rubybook -u root -p

It’s definitely a good practice connect with a user besides root but for verification purposes this works well. We should be prompted for a password before seeing the familiar mysql prompt.

Step 17: MySQL prompt

Shared or Isolated Code?

There are two schools of thought when it comes to testing your Rails code itself. Let’s discuss them a bit.

  • Shared Code Base Pros : No need to checkout, pull, merge, rebase or any other form source control during your testing process. You can run rake test:browser when ever you feel your code is ready to test.
    Cons : You can’t continue to edit code while your tests are running. You also can’t change your branch to review other work.
  • Isolated Code Base Pros : If your browser tests are running in isolation you are free to work on other branches or even run other tests in parallel on your host machine.
    Cons : Changes to your code base must be committed to a branch, pushed, then pulled down to your testing directory during each iteration of your tests. This can be very time consuming unless you have a means of automation.

My preference, while subject to change, is to use a shared code base.

VirtualBox File Sharing

Setting up file sharing in VirtualBox I found to be quite easy once you learn where the shares are visible in Ubuntu. While this may not be necessary, I’d recommend shutting down our VM before making these changes. Once shut down (or suspended) click on the Ubuntu Settings … Shared Folders
Step 18: Create File Share

Click the Add Share and choose the location on your host machine you’d like to make visible to Ubuntu. I choose my “work” path and then named my share “work” accordingly. After you click “OK” and start up the VM again you might wonder where these shares are visible.
Step 19: Add New Share

I remember it took me a while to find them originally but they are mounted under /media with a prefix of sf_ . Thus, work becomes /media/sf_work .

Bundle Install/Update

Before we can run Rake or start up Rails let’s be sure Bundler is happy by executing. Open up terminal and navigate to your working Rails directory and execute:

$ bundle install
I would expect there to be a whole lot of activity the first time you run this. Granted, you could setup a mapping between your host gems and Ubuntu’s gems but that’s not worth the trouble since some gems are compiled specifically for Linux and will just give you a headache later.

Run Browser Tests!

$ rvmsudo rake test:browser You should now see Firefox plowing through your browser tests while you remain in control of your mouse and keyboard.

Step 20: We're Testing

VirtualBox has returned my computer from the icy grip of browser testing. Feel free to reach out if you have any questions or suggestions. I’m always eager to learn from others’ adventures in code.



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.