Running Behat tests on BrowserStack

Sara Olson, Marketing Analyst
#Testing | Posted

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:

  1. {
  2. "require": {
  3. "behat/mink": "~1.5",
  4. "behat/mink-goutte-driver": "~1.0",
  5. "behat/mink-selenium2-driver": "~1.1",
  6. "behat/behat": "~3.0,>=3.0.5",
  7. "behat/mink-extension": "~2.0"
  8. }
  9. }

If you're using Drupal, and the Behat Drupal Extension, it can be as simple as:

  1. {
  2. "require": {
  3. "drupal/drupal-extension": "~3.1"
  4. }
  5. }

...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:

  1. default:
  2. suites:
  3. default:
  4. contexts:
  5. - FeatureContext
  6. - Drupal\DrupalExtension\Context\DrupalContext
  7. - Drupal\DrupalExtension\Context\MinkContext
  8. extensions:
  9. Behat\MinkExtension:
  10. show_cmd: "open %s"
  11. base_url: http://local.dev
  12. browser_stack:
  13. username: 'USERNAME'
  14. access_key: 'ACCESS KEY'
  15. capabilities:
  16. browser: 'firefox'
  17. browserVersion: 42
  18. # This is required for local testing.
  19. browserstack-tunnel: true
  20. goutte: ~
  21. javascript_session: 'browser_stack'
  22. Drupal\DrupalExtension:
  23. blackbox: ~
  24. drush:
  25. alias: 'local'
  26. drupal:
  27. drupal_root: './build/html'
  28. api_driver: 'drupal'

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:

  1. Feature: Demonstrate Browserstack capabilities
  2.  
  3. @api @javascript
  4. Scenario: Administrators can manually run cron
  5. Given I am logged in as a user with the "administrator" role
  6. And I am on the homepage
  7. And I click "Manage"
  8. And I click "Reports"
  9. And I click "Status report"
  10. Then I should see the link "run cron manually"

This is the corresponding video from BrowserStack:

[video width="1024" height="768" mp4="https://www.phase2technology.com/wp-content/uploads/2015/12/browserstac…"][/video]

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:

  1. env:
  2. - BEHAT_PARAMS='{"extensions": {"Behat\\MinkExtension": {"browser_stack": {"capabilities": {"browser": "Chrome", "browserVersion": 45}}}}}'
  3. - BEHAT_PARAMS='{"extensions": {"Behat\\MinkExtension": {"browser_stack": {"capabilities": {"browser": "Firefox", "browserVersion": 42}}}}}'

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.

Sara Olson

Marketing Analyst