Behat, in combination with Mink, allows for running tests against a variety of browser emulators. One of these is Selenium. BrowserStack is a service that sits on top of Selenium, and allows for testing a wide variety of real browsers on real operating systems. Behat (by way of the Mink Extension) has natively supported BrowserStack for some time, but the documentation is limited and outdated.
To get started, you’ll need a BrowserStack username and access key (available at https://www.browserstack.com/accounts/automate once logged in). You’ll need the Selenium 2 driver, as the BrowserStack-specific items simply wrap that driver. Your composer.json file might look something like this:
If you’re using Drupal, and the Behat Drupal Extension, it can be as simple as:
…since the Behat Drupal Extension already requires the various drivers and extensions above.
Since the BrowserStack driver is a wrapper to the Selenium 2 driver, much of the configuration will be the same. Here is a sample behat.yml file with some BrowserStack-specific configurations:
Note the browserstack-tunnel option. This is needed if the site you are testing is running on your local server. BrowserStack has documentation on running browsers against local servers. Once you’ve downloaded the binary to enable this, that just needs to be running in a terminal while Behat runs (./BrowserStackLocal {ACCESS KEY}).
Once you have your tests running, they can be watched in real time (or played back later) via the Browserstack dashboard.
For instance, given this simple feature:
This is the corresponding video from BrowserStack:
In addition to all of the normal capabilities of the Selenium 2 driver, BrowserStack adds operating system, device, and others.
To test a wide variety of browser and version combinations, using a continuous integration server to automate this by way of environment variables will reduce the complexity of the behat.yml file. If this is not possible, using Behat suites to configure different browser combinations would also be an option.
If using Travis CI, this is an example from a .travis.yml file to setup multiple different browser versions:
This will run all tests in both Firefox and Chrome. Further combinations involving different operating systems could be added in a similar fashion.
For more information on Behat and Drupal, see the documentation and further reading here.