Behat is a behavior-driven development test framework. Its intention is to help in the aid of communication between developers, clients and stakeholders during the software development process. The standout of Behat over other testing frameworks is to write tests using normal human-like language.
Tests written in Behat can be integrated into Selenium as well as other browser emulation tools, so failures can be captured in the form of screenshots. Test scenarios are written in Gherkin using a “Given, When, Then” syntax, that explains the business logic to be tested.
With a complex framework such as Magento 2, it is a great idea to use some sort of automated testing tool as it allows you to be remain confident during the development process, so you always know that deployed code is in a successful state.
The set-up is pretty easy. We are going to first install the required dependencies with Composer, then download & run the Selenium standalone server with ChromeDriver, initiate Behat to set up some base classes, and write some scenarios to test the full setup.
In order to get access to the behat binaries, we need to first install them with Composer. We can do that by executing the following lines at our bash prompt:
The last line installs the Behat Magento 2 extension, which allows you to easily inject Magento services into Behat code. This isn’t a normal Magento module though, as you don’t need to enable it or run database upgrade scripts. We’ll just be using the source code from it in our test scripts.
If using Docker, you’ll also need to add DNS entries to the redis & db services in order for Behat to be able to connect to them from your host machine. You can do this by executing the following lines:
The Selenium Standalone server
Selenium Standalone server is a Java-based application that is used to start the Selenium server. It is basically a proxy that pipes Selenium tests & commands to remote browser instances. This provides an easy way to run tests in parallel from multiple machines.
Move the chromedriver binary to a location on your host machine, and then make it executable:
Adding it to the /usr/local/bin folder allows you to run the chromedriver command globally from any terminal prompt.
Now navigate to Magento source code directory and create a behat.yml file there that contains the following content:
❗️ Be sure to update the value for the base_url property to the value of your Magento store’s base URL!
Then, run behat with the --init flag to create the features folder and initialize the base classes:
This should output something similar to the following:
Edit the FeatureContext class of the newly created file at features/bootstrap/FeatureContext.php to make it extend MinkContext. This adds a lot of functionality to your in-browser testing, and you can further customize this class as you see fit.
To help kick things off, I also created the spin() and scrollIntoView() methods below that you may also find useful when writing your own scenario step implementations:
❓ Where does MinkContext come from? It is derived from Mink, which removes the differences between the many different browser emulators. This provides an easy way to write code for many emulators. To get more info on Mink and all of the available methods, check out the Mink documentation.
Finally, we need to start the Selenium server with a java command prompt, in order for the client to be able to connect to it:
To ensure Selenium runs correctly, you should see output similar to the following:
Writing features with Behat
Feature files are written in Gherkin language and contain scenarios for that specific feature. To learn more, you can read about features and scenarios in the Behat documentation.
Features are stored in the features directory and have the file extension .feature. In order to test your Behat setup, create a file at features/catalog.feature file with the following content:
This is that “Given, When, Then” syntax that I talked about before. It’s a simple human language that is used to right up the different testing scenarios.
Note the @javascript tag. This tells Behat to switch the driver used to be able to handle execution of javascript as defined in behat.yml. In order to implement functionality in different Context classes, I use traits and then add them to the FeatureContext class. This allows the usage of the MinkContext methods in contexts other than FeatureContext.
For example, here is the CatalogContext trait located at features/bootstrap/CatalogContext.php, and it is used for the above catalog.feature file.
Note that it is also possible to use a PHPUnit Assert class within your context traits:
This trait would then be imported into the FeatureContext class with a use statement:
Run vendor/bin/behat features/catalog.feature and voilà: your automated test should execute!
Debugging
On Mac, it’s possible additional security policies of that operating system will prevent chromedriver from opening:
In this case, go to the /usr/local/bin directory within Finder, Right+Click, and select Open. Then, click OK to approve the security notice. Your tests should now execute on the following attempt.
Wrap-up
You can quickly notice that using Behat can dramatically help with daily Magento development. It will make you feel confident pushing out new code, knowing that regression tests are in place. Regression tests allow you to push out new features & functionality, knowing this new code that was introduced won’t break pre-existing code & functionality.
Prepared to uncover more about Magento? Check out these 3 solutions I can offer: