behat Getting started with behat Testing JavaScript with Mink and Selenium


If we want to test JavaScript on a website, we'll need to use something a bit more powerful than Goutte (which is just cURL via Guzzle). There are a couple of options such as ZombieJS, Selenium and Sahi. For this example I'll use Selenium.

First you'll need to install the drivers for Mink:

$ composer require --dev behat/mink-selenium2-driver="^1.2"

And you'll also need to download the Selenium standalone server jar file and start it:

$ java -jar selenium-server-standalone-2.*.jar

We'll also need to tell Behat that when we use the @javascript tag to use the Selenium driver and provide the Selenium standalone server's location.

# ./behat.yml
    # …
        base_url: "[your website URL]"
            # …
                    browser: "firefox"
                    wd_host: http://localhost:4444/wd/hub

Then, for each test you want to be run using browser emulation, you just need to add a @javascript (or @selenium2) tag to the beginning of the feature or scenario.

# ./features/TestSomeJavascriptThing.feature
@javascript # or we could use @selenium2
Feature: This test will be run with browser emulation

The test can then be run via Behat (as any other test). The one difference is, when the test is run it should spawn a browser window on the computer running the Selenium standalone server which will then perform the tests described.