Installing Selenium on a Ubuntu VM with Vagrant
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
- Download Virtual Box here
- Run the installer with all default settings
- Download Vagrant here
- Run the installer
- Restart your machine
- We need an SSH client, Windows doesn’t come with one 1. Download CygWin
- Run the installer
- When it asks you to Select Packages; search for OpenSSH
- Click on the “Name” column next to OpenSSH to clarify you want to download it
- Continue with the installation
Setting up your first Box
- Open the Cygwin Terminal
- CD to a directory you want to use for vagrant (eg CD C:/vagrant-default/)
- Run:
vagrant init precise32 http://files.vagrantup.com/precise32.box
- Run:
vagrant up
This will download and set up a Ubuntu Precise32 VM running on Virtual Box
Accessing the Box
- Open Cygwin Terminal
- CD to your vagrant directory (eg CD C:/vagrant-default/)
- 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:
- Run:
cd /vagrant
- Run :
ls
Notice how the files listed are are the same files in the vagrant directory of your system?
- Now, in Windows, go to the directory and create a new folder
- Then on the VM: 1. Run:
cd /vagrant
- 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:
- Open Cygwin Terminal
- CD to your vagrant directory (eg CD C:/vagrant-default/)
- 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:
- 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
- In the file add 1 line of text:
#!/usr/bin/env bash
- Open the Vagrantfile in a text editor
- Find the “config.vm.box” property
- 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:
Any Selenium tests now pointed to the standalone server here will be run on the VM.