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.
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:
- Java SE Runtime Edition (JRE), which is required by Selenium,
- 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
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
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!!
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.
- Adams, D. (2014). Behat And Selenium In Vagrant. [Blog] Programming Are Hard. Available at http://programmingarehard.com/2014/03/17/behat-and-selenium-in-vagrant.html [Accessed 26 Jun. 2015].