Alex Budurovici

London software engineer

Behat, Vagrant and Selenium

Behat, Selenium and Vagrant

Don’t run Selenium server in your Vagrant box, run it in your host.

On one of my laptops I have a Vagrant box which is a replica of a live CentOS web server (provisioned via Ansible), and there is where all my code resides and naturally where I run my tests from also, whilst my host is completely clean (14.04 Ubuntu and SublimeText as IDE).

Just before getting to the steps, I don’t know whether you read already or not the Behat And Selenium in Vagrant article, but it depicts how to run the tests inside Vagrant, by having to install firefox, xvfb and headless jre in your Vagrant box. Well, I don’t think it’s the right way to do it. **Leave your box intact** And the reason is that, unless you run in a complete CLI environment, most certainly is that your host has almost everything you need to run Selenium there (maybe missing JRE).

Right. In order to run my Behat/Mink Selenium tests and see the results in my host browser for a Phalcon PHP project, this is what I ended up doing

1. Download Selenium on my host machine from their official page
2. Adjust my project’s behat.yml parameters for Selenium to call to my host:

    wd_host: HOST.IP.HERE.PLEASE:4444/wd/hub
        browser: firefox
        version: ANY

so for example, you’d end up with something like:

        '': %paths.base%/tests/features/bootstrap
            base_url: ''
            goutte: ~
                    browser: firefox
                    version: ANY
            default_session: goutte
            javascript_session: selenium2
            browser_name: firefox
            paths:    [ %paths.base%/tests/features/auth ]
            contexts: [ AuthContext ]

3. Start Selenium server inside your host:

java -jar /path/to/selenium-server-standalone-2.52.0.jar

4. Run your tests inside vagrant:

cd /my/project/path && vendor/bin/behat

5. See Firefox opening and tests being executed.

Remember to tag the scenario you want to test with Selenium, with @javascript, eg.:

Scenario: Accessing the registration page and seeing the registration form inputs
    Given I am on "/"
    When I follow "Sign Up"
    And I am on "/sign-up2
    Then I should see a "email" "input" type "text"
    And I should see a "firstName" "input" type "text"
    And I should see a "lastName" "input" type "text"
    And I should see a "password" "input" type "password"
    And I should see a "confirmPassword" "input" type "password"
    And I should see a "howFind" "select"
    And I should see a "termsText" "textfield"
    And I should see a "terms" "input" type "checkbox"
    And I should see a "optOut" "input" type "checkbox"


And there you go. No need to overcomplicate your Vagrant box, having to install additional tools, setting up X11 forward, and so on.


Alexandru Budurovici • February 16, 2016

Previous Post