Acceptance tests with Behat and Chrome headless
— by Stefano Angaran, CTO at Caendra Inc.
Setting up your development environment to run your acceptance tests can be tricky. Don’t worry! We have gone through that for you and created this article to make your life a little bit easier. We hope you like it!
Disclaimer
The instructions provided in the article were tested on a CentOS based Linux distribution. You can use Vagrant, as we do, to build and run your development environment based on CentOS or you can use your favorite OS. This will keep your machine clean while giving you also the ability to reproduce the environment on as many workstations you want. Given that you can certainly run the instructions below on any Linux system with the proper adaptations, feel free to drop a line if you would like further insights into any command.
Gathering the tools
First of all, here’s our shopping list:
- Chromedriver
- Google Chrome (in headless mode)
- Selenium 2.33
- Behat
These are tools we need to set up a working acceptance test execution environment. Let’s get down to business.
There is no repository to install Chromedriver so we will need to download it manually.
VERSION=$(curl http://chromedriver.storage.googleapis.com/LATEST_RELEASE)
wget https://chromedriver.storage.googleapis.com/$VERSION/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/chromedriver
The above commands fetch the latest release of Chromedriver from Google servers, unzip it and move the binary inside our PATH.
The next step would be to install Chrome. Lucky for us there is an official repository available for our distribution, CentOS 7. One might be available for yours too, just take a look around.
cat << EOF > /etc/yum.repos.d/google-chrome.repo
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/\$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
EOF
The above hieroglyphs will define a new repository from which our package manager will download the Chrome binaries. The next command will install the latest available stable version.
yum install google-chrome-stable -y
As Selenium is a Java software, we need the Java Runtime in order to run it. After installing that using our package manager, we fetch the Selenium .jar archive and store it in a dedicated folder.
yum -y install java-1.7.0-openjdk-devel
mkdir /var/selenium
wget http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.1.jar -P /var/selenium
If you are using Composer to manage your PHP dependencies (and you should!), including Behat in your project will be as simple as:
composer require --dev behat/behat
We used the --dev
switch in order to avoid installing it in production since we only need Behat during development.
Configuring Behat
This is our example behat.yml
configuration file. You should put it in the root folder of your project so Behat will be able to pick it up without even specifying its path.
# behat.ymldefault:
suites:
example:
paths:
features: %paths.base%/acceptance/features/Example
contexts:
- Behat\MinkExtension\Context\MinkContext
extensions:
Behat\MinkExtension:
base_url: http://local.machine.test/
sessions:
default:
selenium2:
browser: chrome
wd_host: http://127.0.0.1:4444/wd/hub
capabilities:
chrome:
switches:
- "--headless"
- "--disable-gpu"
- "--window-size=1920,1080"
- "--no-sandbox"
The above configuration should be used mostly as a reference, and yours might be different depending on where you store your feature files or what the base URL of your application is. Make sure to adapt it before you move forward.
Running your tests
Of course, all this work would be useless without this final step! In order to run our tests, we first need to execute the Selenium daemon and let it work in the background.
java -jar /var/selenium/selenium-server-standalone-2.53.1.jar -Dwebdriver.chrome.driver=/usr/bin/chromedriver
Here you go! Now you can run Behat and wait for your acceptance tests to complete:
vendor/bin/behat
All green? We hope so!
Wrapping up
We have come a long way from the start, haven’t we? We hope that you enjoyed this article. Please feel free to leave feedback. We look forward to hearing from you.
See you next time!