---
title: Run Behat and Mink Selenium tests in the Cloud.
description: Cross browser testing with Mink and Behat on Selenium.
source_url:
  html: https://testingbot.com/support/web-automate/selenium/php/behat-mink
  md: https://testingbot.com/support/web-automate/selenium/php/behat-mink/index.md
---
# Behat Automated Testing

See our [Behat 2 example](https://github.com/testingbot/behat-selenium-sample) or [Behat 3 example](https://github.com/testingbot/behat-selenium-sample/tree/behat3) for some simple examples on how to run Behat tests on TestingBot.

TestingBot supports Selenium tests using Behat. It's very easy to create or convert Behat tests to use our Selenium Grid.   
 Behat is a Behavior-Driven Development framework for PHP. You can find more information on the [Behat Documentation Pages](https://docs.behat.org/en/latest/).

## Installation

Make sure you have `Composer` installed:

    curl https://getcomposer.org/installer | php

First make sure you've installed Behat. See this example with PHP Composer:

    php composer.phar install

Next, let's create the necessary files:

**composer.json** : 

    {
        "require": {
            "behat/behat": "^3.0",
            "php-webdriver/webdriver": "^1.14",
            "symfony/yaml": "*"
        },
        "config": {
            "bin-dir": "bin/"
        },
        "scripts": {
            "test": "composer single && composer parallel",
            "single": "./vendor/behat/behat/bin/behat --config=config/single.conf.yml",
            "parallel": "CONFIG_FILE=config/parallel.conf.yml /usr/bin/env php lib/parallel.php"
        },
        "autoload": {
            "classmap": ["lib/"]
        }
    }

Now we can create a simple testcase, written in Behat:

**features/single/single.feature**

    Feature: Google Search Functionality
    
      Scenario: Can find search results
        Given I am on "https://www.google.com/ncr"
        When I search for "Google"
        Then I should see "Google"

**features/bootstrap/FeatureContext.php** : 

    <?php
    
    require 'vendor/autoload.php';
    
    use Behat\Behat\Context\Context;
    use Behat\Behat\Hook\Scope\BeforeFeatureScope;
    use Behat\Behat\Hook\Scope\AfterFeatureScope;
    use Facebook\WebDriver\Remote\DesiredCapabilities;
    use Facebook\WebDriver\Remote\RemoteWebDriver;
    use Facebook\WebDriver\WebDriverBy;
    
    class FeatureContext implements Context
    {
        protected static $driver;
    
        public function __construct(array $testingbot)
        {
            $GLOBALS['CONFIG'] = $testingbot;
            $GLOBALS['USERNAME'] = getenv('TESTINGBOT_KEY') ?: $testingbot['user'];
            $GLOBALS['ACCESS_KEY'] = getenv('TESTINGBOT_SECRET') ?: $testingbot['secret'];
        }
    
        /** @BeforeFeature */
        public static function setupBrowser(BeforeFeatureScope $scope)
        {
            $config = $GLOBALS['CONFIG'];
            $taskId = (int) (getenv('TASK_ID') ?: 0);
            $url = 'https://' . $GLOBALS['USERNAME'] . ':' . $GLOBALS['ACCESS_KEY'] . '@' . $config['server'] . '/wd/hub';
            $envCaps = $config['environments'][$taskId];
    
            $caps = DesiredCapabilities::create($envCaps);
            $caps->setCapability('tb:options', $config['capabilities'] ?? []);
    
            self::$driver = RemoteWebDriver::create($url, $caps);
        }
    
        /** @AfterFeature */
        public static function teardownBrowser(AfterFeatureScope $scope)
        {
            if (self::$driver) {
                self::$driver->quit();
            }
        }
    
        /** @Given /^I am on "([^"]*)"$/ */
        public function iAmOnSite($url)
        {
            self::$driver->get($url);
        }
    
        /** @When /^I search for "([^"]*)"$/ */
        public function iSearchFor($searchText)
        {
            $element = self::$driver->findElement(WebDriverBy::name('q'));
            $element->sendKeys($searchText);
            $element->submit();
            sleep(5);
        }
    
        /** @Then /^I should see "([^"]*)"$/ */
        public function iShouldSee($string)
        {
            $source = self::$driver->getPageSource();
            if (strpos($source, $string) === false) {
                throw new Exception("Expected to see: '" . $string . "', actual page did not contain it.");
            }
        }
    }

Note: this is a **Behat 3** example. `Behat\Behat\Context\Context` is the Behat 3 interface, the older `BehatContext` base class only existed in Behat 2 and won't resolve under the `behat/behat ^3.0` requirement above.

To actually run this test, we first need to add some TestingBot settings to `config/single.conf.yml`

    default:
      suites:
        default:
          paths:
            - '%paths.base%/features/single'
          contexts:
            - FeatureContext:
                testingbot:
                  server: "hub.testingbot.com"
                  user: "key"
                  secret: "secret"
                  capabilities:
                    build: "behat-selenium-sample"
                    name: "single-behat-test"
                  environments:
                    -
                      browserName: chrome
                      browserVersion: latest
                      platformName: WIN11

You can now run this test on TestingBot using the following command:

    php composer.phar single

## Specify Browsers & Devices

To let TestingBot know on which browser/platform/device you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.

  ![OS selected](https://testingbot.com/assets/environments/svg/windows11-0e1b28bc0fdd5034d3e4d3dc8d346c500a8c6522facf4b45d0da56537c1f1c6d.svg) Windows 11 › ![Browser Selected](https://testingbot.com/assets/environments/svg/chrome-c4081ff447d2d898d4afcb8f074a907c960e6f007716c1a1d119eee6803c4042.svg) Chrome 139 

Loading environments...

Please wait while we load the available browsers and platforms.

    

## Testing Internal Websites

We've built [TestingBot Tunnel](https://testingbot.com/support/tunnel), to provide you with a secure way to run tests against your staged/internal webapps.  
Please see our [TestingBot Tunnel documentation](https://testingbot.com/support/tunnel) for more information about this easy to use tunneling solution.

The example below shows how to easily run a PHP WebDriver test with our Tunnel:

1. [Download TestingBot Tunnel](https://testingbot.com/support/tunnel) and start the tunnel:

    java -jar testingbot-tunnel.jar key secret

2. Adjust your test: instead of pointing to `'hub.testingbot.com/wd/hub'` like the example above - change it to point to your tunnel's IP address.   
 Assuming you run the tunnel on the same machine you run your tests, change to `'localhost:4445/wd/hub'`. localhost is the machine running the tunnel, 4445 is the default port of the tunnel.

This way your test will go securely through the tunnel to TestingBot and back. Update `config/single.conf.yml` so the `server` points at the tunnel:

    default:
      suites:
        default:
          paths:
            - '%paths.base%/features/single'
          contexts:
            - FeatureContext:
                testingbot:
                  server: "localhost:4445"
                  user: "key"
                  secret: "secret"
                  capabilities:
                    name: "behat-tunnel-test"
                  environments:
                    -
                      browserName: chrome
                      browserVersion: latest
                      platformName: WIN11

## Run tests in Parallel

Parallel Testing means running the same test, or multiple tests, simultaneously. This greatly reduces your total testing time.

You can run the same tests on all different browser configurations or run different tests all on the same browser configuration.   
 TestingBot has a large grid of machines and browsers, which means you can use our service to do efficient parallel testing. It is one of the key features we provide to greatly cut down on your total testing time.

### Queuing

Every plan we provide comes with a limit of parallel tests.   
 If you exceed the number of parallel tests assigned to your account, TestingBot will queue the additional tests (for up to 6 minutes) and run the tests as soon as slots become available.

## Other PHP Framework examples

- [Behat](https://testingbot.com/support/web-automate/selenium/php/behat-mink)

Behat is a BDD framework which runs on PHP.   
 Mink is used for its browser emulation and works nicely together with Behat.

- [Codeception](https://testingbot.com/support/web-automate/selenium/php/codeception)

Codeception is a BDD-styled PHP testing framework.   
 This testing framework offers good Selenium support.

- [Laravel Dusk](https://testingbot.com/support/web-automate/selenium/php/laravel-dusk)

Laravel Dusk provides an easy-to-use browser automation testing framework.

- [PHPUnit](https://testingbot.com/support/web-automate/selenium/php/phpunit)

PHPUnit is the most popular unit testing framework for PHP.   
 It comes with good Selenium WebDriver support and is easy to set up.

- [SimpleTest](https://testingbot.com/support/web-automate/selenium/php/simpletest)

SimpleTest is a framework for unit testing, web site testing and mock objects for PHP.

### Looking for more help?

Have questions or need more information? Reach out via email or Slack.

[Email us](https://testingbot.com/contact/new)[Slack Join our Slack](https://join.slack.com/t/testingb0t/shared_invite/zt-3bcw9xch-jk19~6XPs_xBrsAgAedkCw)
