Lets start by looking at what Behat is.. This will help set the foundations for using Behat within Magento. Lets start by saying that Behat is just a tool within the bigger BDD ( Behaviour driven development ) suite. Yes its great that we can automate the process of testing this in a browser but we should never loose track and think that we can ignore communication with people. Always favour conversation to generation the feature files over automation.

What is Behat ?

Behat in its basic forms takes feature context files writen in Gherkin and executes them against a domain that you specify. In our example we will show selenium and the web browser but the same can work for CLI scripts or API's. Feature files are processed via Feature Contexts so how you implement them is up to you. behat just provides a nice interface around it all. ** Perhaps rephrase this **

Whats the Problem ?

So what do we need ? Well lets start by identifying the problem we want to test. For this example we will search Wikipedia for the string Behaviour Driven Development. We will then check that the first H1 tag is "Behaviour-Driven Development". Nothing overly complex but by introducing the concepts now such as Behat, Feature context, Selenium and Raw Mink context it will make your journey into BDD with Magento a lot easier.

Composer.JSON creation

Lets create a composer.json file within our project with the following contents:

 1 {
 2   "require-dev": {
 3     "behat/behat": "~3.0.13",
 4     "behat/mink": "~1.6.0",
 5     "behat/mink-extension": "~2.0.0",
 6     "behat/mink-goutte-driver": "~1.1.0",
 7     "behat/mink-selenium2-driver": "*",
 8     "peridot-php/webdriver-manager": "dev-master",
 9     "bossa/phpspec2-expect": "1.*"
10       },
11  "config": {
12      "bin-dir" : "bin"
13  }
14 }

What are we installing here ? Well for the development environment we want: Behat, Mink, Selenium driver, the web driver manager ( Useful for managing selenium) and PHPExpect a simple assertion library.

Im assuming that you have composer installed on your system, If not you will need to visit and ensure its installed. Then run composer install. This will install all the required packages we set in our composer.json file

Installing Selenium

Once we have installed all of our packages lets use the web driver manager to download a version of selenium for use later. Because we set the config directory to be /bin we can run ./bin/manager install this will download a version of selenium for us.

Configuring Behat.yml

Now lets configure behat. Start by creating a behat.yml file in the root of the project. It needs the following content:

1 default:
2     extensions:
3         Behat\MinkExtension:
4             base_url: ''
5             sessions:
6                 default:
7                   selenium2:
8                     wd_host: http://localhost:4444/wd/hub

So what have we actually configured here ? Well to start with we are telling Behat that we want to set some default options. We want to include the MinkExtension and set the base_url to be wikipedia we also want to use selenium as our session driver this requires the web driver host URL.

Starting behat and configuring the context file

Next up we need to initialise Behat. run ./bin/behat --init this will create us a features folder and a feature context file that will be used to run our scenarios.

Lets edit the FeatureContext file that was created for us and add: use Behat\MinkExtension\Context\RawMinkContext; and then extends RawMinkContext this will give us access to the mink context that provides helper methods to interact with the pages. Helpers such as visit, click, upload etc.

Writing the first feature

Lets write our first feature file:

1 Feature: Search wikipedia for BDD
2   In order to learn about BDD
3   As a passionate developer
4   I need to be able to search wikipedia to find out more information on BDD
6 Scenario: Search Wikipedia for BDD
7   Given I am on wikipedia
8  When I search for "Behaviour driven development"
9  Then the first heading will be "Behaviour-driven development"

Now when we run behat is has detected the feature file and provides us with boilerplate code required to map this feature file to the php process. To save copy and pasting we can run behat with ./bin/behat --append-snippets this adds all of the example code into our feature context for us. Running behat again now shows pending exceptions so we need to write some code to get our features working.

Making the context do something

So now lets make our context do something. Given is just setting the state. Here we say we are on the page. So lets add that in.

1 <?php
2 /**

3 * @Given I am on wikipedia

4 */
5 public function iAmOnWikipedia(){
6     $this->visitPath("/");
7 }


When is a little more engaging, We need to search for a string.

1 <?php
2 /**

3 * @When I search for :searchString

4 */
5 public function iSearchFor($searchString){
6     $this->getSession()->getPage()->fillField('searchInput', $searchString);
7     $this->getSession()->getPage()->find('css', '.searchButton')->click();
8 }


Now we need to use some asserttions.

1 <?php
2 /**

3 * @Then the first heading should read :heading

4 */
5 public function theFirstHeadingShouldRead($heading){
6     $PageHeading = $this->getSession()->getPage()->find('css', '.firstHeading');

7     expect($PageHeading->getText())->toBe($heading);
8 }


As you can see when we run Behat now all of our steps are passing. So we know we can run this feature again and again and have confidence that the first heading will always be what we expect.

During this video we covered the basics of Behat, How to install and use Selenium and where Mink fits into this. How to configure Behat and create features and code generation as well as an overview of Expect the assertion library. The purpose of this video was not to make you a master of Behat and functional testing. In the next episodes we will look at how we can use Behat within the Magento framework and its important to have a grounded knowledge first before we jump into Magento and BehatMage.

comments powered by Disqus
Comment Permalink