Installing Selenium on a Ubuntu VM with Vagrant

Reading time ~3 minutes

What is Vagrant?

Vagrant allows you to set up a VM containing your entire development environment in a similar fashion to VMWare. It uses VirtualBox to run the virtual machine then allows configuration of these machines with scripts.

It stores your environments in “Box” files which are the base images and allows you to script the installation of software making it easy to rebuild an environment from scratch.

The main advantage of Vagrant is how quickly you can script an environment whilst providing the ability to consistently recreate it from a small footprint.

The Exercise

Below I’m going to setup a Ubuntu Precise32 VM then install Selenium with the Chrome Driver. This is based on Chris Le’s article which can be found here; with the core difference being installation on a Windows environment.

Installation

  1. Download Virtual Box here
  2. Run the installer with all default settings
  3. Download Vagrant here
  4. Run the installer
  5. Restart your machine
  6. We need an SSH client, Windows doesn’t come with one 1. Download CygWin
  7. Run the installer
  8. When it asks you to Select Packages; search for OpenSSH
  9. Click on the “Name” column next to OpenSSH to clarify you want to download it
  10. Continue with the installation

Setting up your first Box

  1. Open the Cygwin Terminal
  2. CD to a directory you want to use for vagrant (eg CD C:/vagrant-default/)
  3. Run: vagrant init precise32 http://files.vagrantup.com/precise32.box
  4. Run: vagrant up

This will download and set up a Ubuntu Precise32 VM running on Virtual Box

Accessing the Box

  1. Open Cygwin Terminal
  2. CD to your vagrant directory (eg CD C:/vagrant-default/)
  3. Run: vagrant ssh

This will SSH into the Linux VM allowing you to access it like any normal Linux server on the command line.

Synchronized Folders

Here’s where it gets interesting; we have a new VM that we can SSH into, however no-one really likes using command line to develop software. Vagrant gets around this by automatically setting up synchronised folders between the VM and your parent system.

Once SSH’ed into the VM:

  1. Run: cd /vagrant
  2. Run : ls

Notice how the files listed are are the same files in the vagrant directory of your system?

  1. Now, in Windows, go to the directory and create a new folder
  2. Then on the VM: 1. Run: cd /vagrant
  3. Run: ls

Notice how that folder now exists on the VM. This means you can write all the code / scripts you need in whichever environment you prefer, then transfer them to a standardized development environment!

Provisioning Software

Now that’s set up; let’s firstly destroy the VM by:

  1. Open Cygwin Terminal
  2. CD to your vagrant directory (eg CD C:/vagrant-default/)
  3. Run: vagrant destroy

This removes the VM and all traces from your system. Now we need to tell Vagrant what we want to setup when it’s finished creating the VMV. To do this:

  1. Create a new file in the same directory as the Vagrantfile called “bootstrap.sh” 1. This is the script that will be run once vagrant has setup the VM
  2. In the file add 1 line of text: #!/usr/bin/env bash
  3. Open the Vagrantfile in a text editor
  4. Find the “config.vm.box” property
  5. Below it add the line: config.vm.provision :shell, :path => “bootstrap.sh”

Now the default Vagrant VM is setup to install Ubuntu then run bootstrap.sh as a Bash script. Whatever we script in this file will now be run by Vagrant.

I’m installing headless Selenium with Chrome using the script from this article; a vagrant box that can run Selenium tests on our behalf is a useful thing.

Chris’ article shows how to get Selenium running on a server so you can remotely run scripts from you local machine using the VM. He sets up Selenium running on port 4444 and exposes the port so we can access it via local host. This means the VM becomes a standalone Selenium server.

I had to make some changes to the script, namely:

  • Changed back to a 32 bit VM (Precise32 instead of Precise64)
  • Updated the ChromeDriver to 2.8 so it supports the latest version of Chrome retrieved by apt-get
  • Timed the bootstrap.sh file so we know how long it takes to run

Now; back on Windows if we navigate to the below URL we will see the Selenium sessions page:

http://localhost:4444/wd/hub

Any Selenium tests now pointed to the standalone server here will be run on the VM.

Behaviour Driven Unit Tests

> "If we change the implementation details, none of our tests should break" - Ian CooperThis blog comes following a work chat around one ...… Continue reading

Using Pomodoro Day To Day

Published on January 20, 2017

Removing Mistakes With Git Rebase

Published on January 16, 2017