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.
Lets create a composer.json file within our project with the following contents:
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 getcomposer.com and ensure its installed. Then run composer install. This will install all the required packages we set in our composer.json file
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.
Now lets configure behat. Start by creating a behat.yml file in the root of the project. It needs the following content:
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:
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:
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.
When is a little more engaging, We need to search for a string.
Now we need to use some asserttions.
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.