---
title: "Running Behat Tests in a Vagrant Box"
url: "/articles/running-behat-tests-in-a-vagrant-box"
type: article
date: 2017-05-31
updated: 2017-05-30
---

# Running Behat Tests in a Vagrant Box

#  Running Behat Tests in a Vagrant Box 

 Using Vagrant for local development is great but has its challenges. One such challenge is running tests using a native browser. This article looks at one way to get around that.

 By

 [  Matt Oliveira   ](/about/matt-oliveira) 

 May 31, 2017

  

For a while now, [Vagrant](https://www.vagrantup.com/) has been my local development environment of choice. Itâ&#128;&#153;s great for a lot of reasons but sometimes working in what is essentially a remote environment, has its challenges. One such challenge is running [Behat](http://behat.org/en/latest/) Tests in a native browser like Chrome with [Selenium](https://seleniumhq.org/).

[Behat](http://behat.org/en/latest/) tests are the bee's knees. They are especially so when you can see your tests executed automatically in your desktop browser for easy debugging:

However, if you use [Vagrant](https://www.vagrantup.com/) for local development you often lose the benefit of visualizing your test run because many off-the-shelf boxes you find on the internet are configured to run [Behat](http://behat.org/en/latest/) tests using headless browsers like [PhantomJS](https://phantomjs.org/) or [headless Firefox with xvfb](https://medium.com/@griggheo/running-selenium-webdriver-tests-using-firefox-headless-mode-on-ubuntu-d32500bb6af2). It's certainly easier to get set up running your tests using [PhantomJS](https://phantomjs.org/), but it doesnâ&#128;&#153;t have to be this way:

You can configure your [Behat](http://behat.org/en/latest/) tests to run in your desktop browser so you can see everything. You can even get fancy and set breakpoints in your tests using an IDE like [PHPStorm](https://www.jetbrains.com/phpstorm/), halting the browser execution and giving you the opportunity to pop open Chrome dev tools or the like to really understand whatâ&#128;&#153;s going on during your test.

This article assumes a few things:

- Local development environment using [Vagrant](https://www.vagrantup.com/) for your Drupal site
- Familiarity with [Behat](http://behat.org/en/latest/) and [Selenium](https://seleniumhq.org/)
- [Chrome](https://sites.google.com/a/chromium.org/chromedriver/) for running the tests

If youâ&#128;&#153;re unfamiliar with [Behat](http://behat.org/en/latest/), [Selenium](https://seleniumhq.org/) and/or [Vagrant](https://www.vagrantup.com/), they are awesome, and you should check them out. Also, while weâ&#128;&#153;re going to use Chrome here, you could use any browser supported by Selenium and your host operating system. Onwards.

## Setup

There are a few things to get set up. Following is a diagram to help you understand the interaction between all the pieces as a way to visualize what you need:

   

   

 ![Behat test flow](/sites/default/files/styles/wide_xs/public/assets/2017-05/behat_teset_flow_1.png.webp?itok=TOFm0PH7 "behat_teset_flow_1.png")

The above diagram shows the test suite run by Behat (1) on the Vagrant box. Behat talks to the Selenium hub (2) server running on the Vagrant box, which talks to the Selenium node (3) server on the host machine. Finally, the Selenium node server is what drives Chrome (4) to execute the test suite. Not to worry if you donâ&#128;&#153;t understand what these things do, weâ&#128;&#153;ll get into that below.

To get started with the setup, first ensure that you have the Java Runtime Environment (JRE) installed on your host machine and in the Vagrant box, which is required to run Selenium. You may already have it installed, but can check with:

```bash
$ java -version
```

Iâ&#128;&#153;m running JRE 1.8. If you need to install or update, Google is your friend.

Next, youâ&#128;&#153;ll need Selenium. Itâ&#128;&#153;s handy to download it into a shared folder between the host machine and the Vagrant box. The important thing is that itâ&#128;&#153;s available on both the host machine and Vagrant box.

```bash
$ wget http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.1.jar
```

Finally, youâ&#128;&#153;ll need Chrome driver on the host machine. For convenience move it somewhere on your PATH:

```bash
$ wget https://chromedriver.storage.googleapis.com/2.29/chromedriver_mac64.zip
$ unzip chromedriver_mac64.zip
$ mv chromedriver /usr/local/bin
```

## Run tests

Now that you have all the software you need, itâ&#128;&#153;s time to run the tests. First thing youâ&#128;&#153;ll want to do is start Selenium server in hub mode in the Vagrant box:

```bash
$ java -jar /path/to/selenium/selenium-server-standalone-2.53.1.jar -role hub
```

Notice the `-role hub` option. This tells Selenium to run in hub mode. Running Selenium in hub mode allows Selenium to receive tests to be executed and forward them to any registered Selenium servers running in node mode. The Selenium server running in node mode is what controls the browser, driving the browser through the tests. In this way you can bridge the gap between the Vagrant box and the host machine, so that you can see the tests running in a browser on the host machine. Youâ&#128;&#153;ve already got the Selenium server in hub mode running on the Vagrant box. Now lets start a Selenium server in node mode on the host machine:

```bash
$ java -jar /path/to/selenium/selenium-server-standalone-2.53.1.jar -role node -hub http://my-vbox.local:4444/grid/register -Wdriver.chrome.driver=/path/to/chromedriver
```

Here, youâ&#128;&#153;re telling Selenium to run in node mode, using the `-role node` option. Youâ&#128;&#153;re also registering the Selenium node with the Selenium hub running in your Vagrant box using the `-hub http://my-vbox.local:4444/grid/register` option. Replace `my-vbox.local` with the host name or IP address of your Vagrant box.

Next youâ&#128;&#153;ll need to adjust your `behat.yml` file to register a Selenium Mink session, that points to the Selenium hub server running in the Vagrant box e.g.,

```
selenium2:
  wd_host: "http://my-vbox.local:4444/wd/hub"
```

So that your `behat.yml` file looks something like this:

```
default:
  suites:
    default:
      contexts:
        - FeatureContext
        - Drupal\DrupalExtension\Context\DrupalContext
        - Drupal\DrupalExtension\Context\MinkContext
  extensions:
    Behat\MinkExtension:
      goutte: ~
      selenium2:
        wd_host: "http://my-vbox.local:4444/wd/hub"
      base_url: http://my-vbox.local
      browser_name: 'chrome'
    Drupal\DrupalExtension:
      blackbox: ~
      api_driver: 'drupal'
      drush:
        alias: 'local'
      drupal:
        drupal_root: '/var/www/docroot'
```

With all that setup, youâ&#128;&#153;re finally ready to run your tests. From your Vagrant box, run:

```bash
$ ./vendor/bin/behat -v -c /path/to/behat.yml /path/to/behat/features
```

If everything worked out, youâ&#128;&#153;ll see Selenium open up a new instance of Chrome and drive it through the test suites:

## Further Reading

[Behat](http://behat.org/en/latest/)  
  
[Behat Mink Extension](https://github.com/Behat/MinkExtension/blob/master/doc/index.rst)  
  
[Mink Selenium 2 Driver](http://mink.behat.org/en/latest/drivers/selenium2.html)  
  
[Selenium](https://seleniumhq.org/)  
  
[Up and Running with Behat, Drupal, &amp; Vagrant](https://kevinquillen.com/and-running-behat-drupal-vagrant)  
  
[Testing Your Drupal Site with Behat](https://www.phparch.com/2016/08/testing-your-drupal-site-with-behat/)  
  
[simongrimes/vagrant-behat](https://github.com/simongrimes/vagrant-behat)

 Published in:

- [  Drupal Development   ](/topics/drupal-development)

 

   You must have JavaScript enabled to use this form.

## Get in touch with us

### Tell us about your project. We'd love to hear from you!

 First Name  

 Last Name  

 Your email address  

 Your organization  

 How you heard about us  

 Tell us more 

     Leave this field blank