What is Behat?
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.
Assumptions
This article assumes you have the Java Runtime binary installed on your host machine, as well as a running & functional Magento development environment. If you do not, check out the popular docker-magento project to easily set one up.
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.
In order to get things working, you’ll need to download the Selenium standalone server. You will also need to download ChromeDriver.
Move the chromedriver
binary to a location on your host machine, and then make it executable:
mv ~/Downloads/chromedriver /usr/local/bin/
mv ~/Downloads/selenium-server-standalone-2.43.1.jar /usr/local/bin/
chmod u+x /usr/local/bin/chromedriver /usr/local/bin/selenium-server-standalone-2.43.1.jar
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:
10:14:30.290 INFO - Launching a standalone server
Setting system property webdriver.chrome.driver to “/usr/local/bin/chromedriver”
10:14:30.325 INFO - Java: Oracle Corporation 25.321-b07
10:14:30.325 INFO - OS: Mac OS X 12.1 x86_64
10:14:30.334 INFO - v2.43.1, with Core v2.43.1. Built from revision 5163bce
10:14:30.390 INFO - Default driver org.openqa.selenium.ie.InternetExplorerDriver registration is skipped: registration capabilities Capabilities [{ensureCleanSession=true, browserName=internet explorer, version=, platform=WINDOWS}] does not match with current platform: MAC
10:14:30.450 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
10:14:30.451 INFO - Version Jetty/5.1.x
10:14:30.451 INFO - Started HttpContext[/selenium-server,/selenium-server]
10:14:30.493 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@6debcae2
10:14:30.493 INFO - Started HttpContext[/wd,/wd]
10:14:30.493 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
10:14:30.493 INFO - Started HttpContext[/,/]
10:14:30.497 INFO - Started SocketListener on 0.0.0.0:4444
10:14:30.497 INFO - Started org.openqa.jetty.jetty.Server@b1bc7ed
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:
- Explore Magento 2 fundamentals & best practices course (1,000+ students)
- Grow your Magento expertise with all courses & lessons (700+ students)
- Learn visually with blocks of code & inline comments (3,000+ students)