Behavior-Driven Development on Laravel Homestead with Behat and Selenium

Oh, my!… I do love the PHP Laravel Framework very, very much!

And one of the things I love most about it is Laravel Homestead, an official, pre-packaged Vagrant “box” that provides a powerful and flexible PHP development environment and bundles a very useful set of software.

However, you can’t please everybody, and in order to do Behavior-Driven Development (BDD) on applications that make use of JavaScript, like I extensively do, you’ll need to do some adjustments to your Homestead installation.

In this article I’ll show you how to make Homestead work smoothly with:

  • Behat, a BDD framework for PHP
  • Selenium WebDriver

Step 1: Enable X11 Forwarding over SSH

The first thing we’re going to do is to enable X11 forwarding over SSH, so we can share the windowing system between the virtual machine and the host.

The only way I know how to do this is by modifying Homestead’s Vagrantfile. Please, feel free to leave a comment if you know a better solution.


config.ssh.forward_x11 = true

Step 2: Install Java, Firefox and xvfb

Next, we’re going to add some necessary software to our virtual machine. For this, Homestead provides a provisioning script, ~/.homestead/after.sh, that we’re going to edit:

#!/bin/sh

# Update app-get
apt-get update

# Install Java, Firefox, and Xvfb
apt-get install -y openjdk-7-jre firefox xvfb

Above we are installing:

  1. Java SE Runtime Edition (JRE), which is required by Selenium,
  2. Firefox
  3. xvfb, to run applications that need a display inside our virtual machine

Step 3: Install Selenium

Then, add these two lines to the script after.sh in order to install Selenium

npm install -g selenium-standalone
selenium-standalone install

We’re using Vincent Voyer’s selenium-standalone NPM package in order to install Selenium WebDriver. As you can see in the package documentation page, we can run selenium headlessly with xvfb with the following command:

xvfb-run --server-args="-screen 0, 1366x768x24" selenium-standalone start

For easier initialization, you can add something like this to your ~/.homestead/aliases file:

alias selenium-start="xvfb-run --server-args=\"-screen 0, 1366x768x24\" selenium-standalone start"

Step 4: Add Behat, Mink and the Selenium2Driver

Finally, execute the following command from the root of your project in order to add Behat, Mink and the Selenium2Driver to your composer.json file and install the necessary packages.

composer require behat/behat behat/mink behat/mink-extension behat/mink-selenium2-driver --dev

Step 5: Run your machine and go grab a tea!

Now you can run your virtual machine with

vagrant up

Be ready to wait for a few minutes so, don’t waste your time and go grab a cup of coffee / tea…

When you come back, it will be ready for you to login to your virtual machine and start Selenium


vagrant ssh
selenium-start

Then open a new tab on your terminal and login again to your VM. Navigate to your project and run your tests!!

Final notes

In this tutorial, I’ve shown you how to customize your Laravel Homestead machine in order to test applications with Behat and Selenium. However, I haven’t included instructions on how to configure Behat for a particular project or framework because Homestead allows you to organize all your PHP projects on the same VM, and I wanted to keep this article somewhat general.

You can get my complete after.sh provisioning script on this Gist. At this moment, I’m working on two things to improve it:

  • Install Composer packages globally instead of locally, as I use this programs in almost all of my projects.
  • Remove output from provisioning script and add some progress messages
  • Test other web browsers (Chrome, Chromium, PhantomJS)

Please, if you encounter any problem following this tutorial, feel free to add a comment below and I’ll do my best to help you solve it. Also, if you have any suggestions on how to improve this setup, feel free to leave a comment here or on the Gist page.

References

One thought on “Behavior-Driven Development on Laravel Homestead with Behat and Selenium

Leave a Reply

Your email address will not be published. Required fields are marked *