Features

Behat Automated Testing

See our Behat 2 example or Behat 3 example 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.

Installation

Make sure you have Composer installed:

Copy code
curl http://getcomposer.org/installer | php

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

Copy code
php composer.phar install

Next, let's create the necessary files:

composer.json:
Copy code
{
    "require": {
    	"behat/behat": "2.5.*@stable",
	    "facebook/webdriver": "dev-master",
	    "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
Copy code
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"
lib/TestingBotContext.php:
Copy code
<?php

require 'vendor/autoload.php';

class TestingBotContext extends Behat\Behat\Context\BehatContext
{
    protected $CONFIG;
    protected static $driver;

    public function __construct($parameters){
        $GLOBALS['CONFIG'] = $parameters["testingbot"];

        $GLOBALS['USERNAME'] = getenv('TB_KEY');
        if(!$GLOBALS['USERNAME']) $GLOBALS['USERNAME'] = $GLOBALS['CONFIG']['user'];

        $GLOBALS['ACCESS_KEY'] = getenv('TB_SECRET');
        if(!$GLOBALS['ACCESS_KEY']) $GLOBALS['ACCESS_KEY'] = $GLOBALS['CONFIG']['key'];
    }

    /** @BeforeFeature */
    public static function setup()
    {
        $CONFIG = $GLOBALS['CONFIG'];
        $task_id = getenv('TASK_ID') ? getenv('TASK_ID') : 0;

        $url = "https://" . $GLOBALS['USERNAME'] . ":" . $GLOBALS['ACCESS_KEY'] . "@" . $CONFIG['server'] ."/wd/hub";
        $caps = $CONFIG['environments'][$task_id];

        foreach ($CONFIG["capabilities"] as $key => $value) {
            if (!array_key_exists($key, $caps))
                $caps[$key] = $value;
        }

        self::$driver = RemoteWebDriver::create($url, $caps);
    }

    /** @AfterFeature */
    public static function tearDown()
    {
        self::$driver->quit();
    }
}
?>
features/bootstrap/FeatureContext.php:
Copy code
<?php

require "vendor/autoload.php";

use Behat\Behat\Context\BehatContext,
  Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;

class FeatureContext extends TestingBotContext {
  /** @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 get title as "([^"]*)"$/ */
  public function iShouldGet($string) {
    $title = self::$driver->getTitle();
    if ((string)  $string !== $title) {
      throw new Exception("Expected title: '". $string. "'' Actual is: '". $title. "'");
    }
  }

  /** @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 is: '". $source. "'");
    }
  }
}

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

Copy code
default:
   paths:
       features: '../features/single'
       bootstrap:  '../features/bootstrap'
   context:
       parameters:
           testingbot:
               server: "hub.testingbot.com"
               user: "key"
               key: "secret"

               capabilities:
                   build: "behat-selenium-sample"
                   name: "single-behat-test"


               environments:
                   -
                       browserName: chrome
                       version: latest
                       platform: Win10

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

Copy code
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.

Copy code

Testing Internal Websites

We've built TestingBot Tunnel, to provide you with a secure way to run tests against your staged/internal webapps.
Please see our TestingBot Tunnel documentation 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 our tunnel and start the tunnel:

Copy code
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:

Copy code
default:
   paths:
       features: '../features/single'
       bootstrap:  '../features/bootstrap'
   context:
       parameters:
           testingbot:
               server: "localhost:4445"
               user: "key"
               key: "secret"

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

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

  • Codeception

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

  • Laravel Dusk

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

  • 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

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