Installing Behat 3 / Mink / Selenium for Browser Testing – Windows – Part 2

Did you miss Part 1?
Behat is a tool you can use to automate testing of your website with behaviour driven tests.

Sample FeatureContext.php

The FeatureContext.php is the file holding your custom test commands. In your file structure, it is at D:\Behat\features\bootstrap\FeatureContext.php.
This is what mine looks like. Including in full because this was one piece of the puzzle that caused stress for me. For a vanilla file, remove all functions after __construct().

<?php
use Behat\Behat\Context\Context;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
/**
 * Defines application features from the specific context.
 */
class FeatureContext extends MinkContext implements Context
{
    /**
     * Initializes context.
     *
     * Every scenario gets its own context instance.
     * You can also pass arbitrary arguments to the
     * context constructor through behat.yml.
     */
    public function __construct()
    {

    }

    /**
     * @When I wait for :text to appear
     * @Then I should see :text appear
     * @param $text
     * @throws \Exception
     */
    public function iWaitForTextToAppear($text)
    {
        $this->spin(function(FeatureContext $context) use ($text) {
            try {
                $context->assertPageContainsText($text);
                return true;
            }
            catch(ResponseTextException $e) {
                // NOOP
            }
            return false;
        });
    }


    /**
     * @When I wait for :text to disappear
     * @Then I should see :text disappear
     * @param $text
     * @throws \Exception
     */
    public function iWaitForTextToDisappear($text)
    {
        $this->spin(function(FeatureContext $context) use ($text) {
            try {
                $context->assertPageContainsText($text);
            }
            catch(ResponseTextException $e) {
                return true;
            }
            return false;
        });
    }

    /**
     * Based on Behat's own example
     * @see http://docs.behat.org/en/v2.5/cookbook/using_spin_functions.html#adding-a-timeout
     * @param $lambda
     * @param int $wait
     * @throws \Exception
     */
    public function spin($lambda, $wait = 30)
    {
        $time = time();
        $stopTime = $time + $wait;
        while (time() < $stopTime)
        {
            try {
                if ($lambda($this)) {
                    return;
                }
            } catch (\Exception $e) {
                // do nothing
            }

            usleep(5000);
        }

        throw new \Exception("Spin function timed out after {$wait} seconds");
    }

    /**
     * @Then page should have body class :text 
     * @param $text
     * @throws \Exception
     */
    public function pageShouldHaveBodyClass($text)
    {
        $session = $this->getSession();
        $page = $session->getPage();
        $el = $page->find('css', 'body');
        try {
            $outer = $el->getOuterHtml();
            if ($el->hasClass($text)) {
                return;
            }
        } catch (\Exception $e) {
            // do nothing
        }

        throw new \Exception ("Body does not have {$text} class.");
    }
}

Sample Feature File

Your feature file holds your tests. Best practice is to have one feature file per feature of your site. e.g. recipes.feature
If you put @javascript on the first line, Behat will attempt to run the tests with Selenium -> Firefox. Otherwise Behat will run the tests with Goutte. Note: Goutte runs tests MUCH faster than Firefox does. Don’t be put off by the weird name. Give Goutte a try.

@recipes 
 Feature: 
  Publish my best recipes

 @cake
 Scenario: Cake main page
  Given I go to "https://mysite/cake"
  Then I should see "Torte"
  And I should see "Muffin"
  And I should see "Sponge"

Don’t forget, you can type “behat -dl” at the command line to see all the commands that are enabled by Mink Extension.

Run your tests

From the command line at D:\Behat, all you need to do is type “behat” and all tests in your feature files will run.
Use

--tag

to limit which feature files run. e.g.

behat --tags=recipes

combined with @recipes at the start of the feature file will select those tests only.


Let me know if this helped you test with Behat in the comments below.

By |2017-05-02T16:08:41+00:00May 2nd, 2017|Developer Tools|2 Comments

About the Author:

Melissa Freeman is a freelance WordPress developer in the Lower Blue Mountains, west of Sydney.

2 Comments

  1. Max August 8, 2017 at 10:52 pm - Reply

    Hi Melissa,

    thx for sharing your knowledge. I got it running in Windows 10 now, but unfortunately there were still problems at the end when running a test:
    “When I go to “” # FeatureContext::visit()
    Mink instance has not been set on Mink context class. Have you enabled the Mink Extension? (RuntimeException)”

    I have enabled this extension for sure. I now created a linux VM, set up everything and there it was working. probably Windows is not made for this.

    Anyway cheers and thx again!

    max

    • Melissa Freeman August 9, 2017 at 3:40 pm - Reply

      I think it’s probably the behat.yml file. Did you just copy and paste it? Like I said in the post, it was a pain to get that right.

Leave A Comment