Header logo Open Menu Close Menu
  • Live
  • Automate
  • App Live
  • App Automate
  • More
    • Enterprise
    • Screenshots
    • Responsive
  • Pricing
  • Resources Resources
      • Languages
      • Java
      • NodeJS
      • C#
      • Python
      • PHP
      • Ruby
      • Perl
      • Frameworks
        • Behat
        • Behave
        • Capybara
        • Codeception
        • Cucumber
        • Gauge
        • Intern
        • JBehave
        • JUnit
        • Lettuce
        • MbUnit
        • Nightwatch
        • NUnit
        • PHPUnit
        • PNUnit
        • Protractor
        • RSpec
        • Selenide
        • Serenity
        • Specflow
        • TestNG
        • WD
        • Webdriverio
      • Documentation
        • Continuous Integration
        • Jenkins Plugin
        • Travis CI add-on
        • TeamCity
        • Bamboo CI
        • Browsers & Devices
        • Physical Mobile Devices
        • Capabilities
        • Rest API
        • Status Badges
        • JS Testing
        • Timeouts
        • Local Testing
        • Debugging Tools
        • Parallel Testing
    • BrowserStack for Open Source
      Learn More
      • Features
      • Live Features
      • Browsers & Platforms
      • Developer Tools
      • Local Testing
      • Security
      • Mobile
      • Test on Right Devices
      • Mobile Features
    • BrowserStack for Open Source
      Learn More
  • Sign in
  • Free Trial
  • Resources
  • Features
  • Mobile
  • Features
  • Live Features
  • Browsers & Platforms
  • Developer Tools
  • Local Testing
  • Security
  • Enterprise Features
  • Open Source
  • Mobile
  • Test on Right Devices
  • Mobile Features

Support Automate PHPUnit

PHPUnit

Documentation for running Selenium Tests with BrowserStack.

Getting Started

Note: Refer our sample repo on Github: phpunit-browserstack

BrowserStack supports Selenium automated tests using PHPUnit, and running your tests on our cloud setup is simple and straightforward. Get started with a sample test, which opens Google's homepage, searches for ‘BrowserStack’, and asserts for the title of the search results page. For the code to run successfully on your machine, please ensure that the following libraries have been installed:

# Install using composer
php composer.phar install phpunit/phpunit-selenium

Here is a sample test case written for running with PHPUnit.

class SingleTest extends BrowserStackTest {

    public function testGoogle() {
        self::$driver->get("https://www.google.com/ncr");
        $element = self::$driver->findElement(WebDriverBy::name("q"));
        $element->sendKeys("BrowserStack");
        $element->submit();
        $this->assertEquals('BrowserStack - Google Search', self::$driver->getTitle());
    }

}

To actually run the test case, we need to integrate with BrowserStack as follows.

Integration with BrowserStack

Note: Running your PHPUnit tests on BrowserStack requires a username and an access key.

To obtain your username and access keys, sign up for a Free Trial or purchase a plan.

Integration of PHPUnit with BrowserStack is made possible by use of following module:

$config_file = getenv('CONFIG_FILE');
if(!$config_file) $config_file = 'config/single.conf.json';
$GLOBALS['CONFIG'] = json_decode(file_get_contents($config_file), true);

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

$GLOBALS['BROWSERSTACK_ACCESS_KEY'] = getenv('BROWSERSTACK_ACCESS_KEY');
if(!$GLOBALS['BROWSERSTACK_ACCESS_KEY']) $GLOBALS['BROWSERSTACK_ACCESS_KEY'] = $CONFIG['key'];

class BrowserStackTest extends PHPUnit_Framework_TestCase
{
    protected static $driver;
    protected static $bs_local;

    public static function setUpBeforeClass()
    {
        $CONFIG = $GLOBALS['CONFIG'];
        $task_id = getenv('TASK_ID') ? getenv('TASK_ID') : 0;

        $url = "https://" . $GLOBALS['BROWSERSTACK_USERNAME'] . ":" . $GLOBALS['BROWSERSTACK_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;
        }

        if(array_key_exists("browserstack.local", $caps) && $caps["browserstack.local"])
        {
            $bs_local_args = array("key" => $GLOBALS['BROWSERSTACK_ACCESS_KEY']);
            self::$bs_local = new BrowserStack\Local();
            self::$bs_local->start($bs_local_args);
        }

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

    public static function tearDownAfterClass()
    {
        self::$driver->quit();
        if(self::$bs_local) self::$bs_local->stop();
    }
}

The module reads from config file where you need to put the BrowserStack Hub URL and credentials.

{
  "server": "hub-cloud.browserstack.com",
  "user": "USERNAME",
  "key": "ACCESS_KEY",

  "capabilities": {
    "browserstack.debug": true
  },

  "environments": [{
    "browser": "chrome"
  }]
}

Run your test on BrowserStack using following command:

# Run using composer
composer single

Testing on Internal Networks

To test a private server with PHPUnit on BrowserStack, install local bindings.

# Install using composer
php composer.phar install browserstack/browserstack-local

Then update config file and set the browserstack.local capability to true.

{
  "server": "hub-cloud.browserstack.com",
  "user": "USERNAME",
  "key": "ACCESS_KEY",

  "capabilities": {
    "browserstack.local": true
  },

  "environments": [{
    "browser": "chrome"
  }]
}

Here is a sample test case written for running local with PHPUnit.

class LocalTest extends BrowserStackTest {

    public function testLocal() {
      self::$driver->get("http://bs-local.com:45691/check");
      $this->assertContains('Up and running', self::$driver->getPageSource(), '', true);
    }

}

Run your local test on BrowserStack using following command:

# Run using composer
composer local

Speed up testing

To run tests on multiple browsers in parallel with PHPUnit on BrowserStack, modify the config file as below:

{
  "server": "hub-cloud.browserstack.com",
  "user": "USERNAME",
  "key": "ACCESS_KEY",

  "capabilities": {
    "browserstack.debug": true
  },

  "environments": [{
    "browser": "chrome"
  },{
    "browser": "firefox"
  },{
    "browser": "safari"
  },{
    "browser": "internet explorer"
  }]
}

Capabilities for each environment can be customised as explained earlier.

You need the following custom script to launch the tests in parallel.

$config_file = getenv('CONFIG_FILE');
if(!$config_file) $config_file = 'config/single.conf.json';
$GLOBALS['CONFIG'] = json_decode(file_get_contents($config_file), true);

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

$GLOBALS['BROWSERSTACK_ACCESS_KEY'] = getenv('BROWSERSTACK_ACCESS_KEY');
if(!$GLOBALS['BROWSERSTACK_ACCESS_KEY']) $GLOBALS['BROWSERSTACK_ACCESS_KEY'] = $CONFIG['key'];

$CONFIG = $GLOBALS['CONFIG'];
$procs = array();

foreach ($CONFIG['environments'] as $key => $value) {
    $cmd = "TASK_ID=$key vendor/bin/phpunit tests/single_test.php 2>&1\n";
    print_r($cmd);

    $procs[$key] = popen($cmd, "r");
}

foreach ($procs as $key => $value) {
    while (!feof($value)) { 
        print fgets($value, 4096);
    }
    pclose($value);
}

Run your tests in parallel on BrowserStack using following command:

# Run using composer
composer parallel

Note: Achieve your test coverage and build execution time goals by using our calculator to understand how many parallel sessions you need.

Configuring capabilities

To run your tests on BrowserStack Automate, the tests have to be run on remote machines. Therefore, the capabilities of the WebDriver have to be changed accordingly.

Run tests on desktop and mobile browsers

Using the drop-down menus, select a combination of operating system, browser, and screen resolution. To see the order of precedence for the capabilities, please read about parameter override rules here.

You can additionally run your Selenium test scripts on real Android and iOS devices in our datacenters.

Look for the icon to select a real device.

1. Select an OS
iOS
Mobile
  • iOS
  • Android
Desktop
  • Windows 10
  • Windows 8.1
  • Windows 8
  • Windows 7
  • Windows XP
  • OS X High Sierra
  • OS X Sierra
  • OS X El Capitan
  • OS X Yosemite
  • OS X Mavericks
  • OS X Mountain Lion
  • OS X Lion
  • OS X Snow Leopard
2. Select a browser
Windows XP
2. Select a device
iOS
3. Select a resolution
1024 x 768
Resolution

    Note: Testing on real devices requires the Automate Mobile plan

    "environments": [{
      "os": "Windows",
      "os_version": "7",
      "browser": "IE",
      "browser_version": "8.0",
      "resolution": "1024x768"
    }]
    
    "environments": [{ "os": "Windows", "os_version": "7", "browser": "IE", "browser_version": "8.0", "resolution": "1024x768" }]
    "environments": [{ "browserName": "iPhone", "platform": "MAC", "device": "iPhone 5" }]
    "environments": [{ "device": "iPhone 5", "realMobile": "true", "os_version": "9.0" }]

    Note: If browser_version capability is not set, the test will run on the latest version of the browser set by browser capability.

    For a list of all supported devices, visit the Browsers and Platforms page.

    Builds and projects

    Keep track of all your automated tests using the build and project capabilities. Group your tests into builds, and builds further into projects.

    "capabilities": {
      "build": "version1",
      "project": "newintropage"
    }
    

    Note: Allowed characters include uppercase and lowercase letters, digits, spaces, colons, periods, and underscores. Other characters, like hyphens or slashes are not allowed.

    Self-signed certificates

    To avoid invalid certificate errors while testing on BrowserStack Automate, set the acceptSslCerts capability in your test to true.

    "capabilities": {
      "acceptSslCerts": true
    }
    

    Enable and Disable Pop-ups

    IE

    To enable the popups in IE, use the browserstack.ie.enablePopups capability.

    "capabilities": {
      "browserstack.ie.enablePopups": true
    }
    
    Safari

    To enable the popups in Safari, use the browserstack.safari.enablePopups capability.

    "capabilities": {
      "browserstack.safari.enablePopups": true
    }
    

    Debugging

    Logs

    To debug failed tests, BrowserStack provides you with raw logs, which are the console logs from the browser tests; visual logs, which capture successive screenshots of the test; and text logs to display all the steps that were performed by the test.

    Live screencast

    With live screencast, view ongoing tests to debug the functionality of features that are being tested. The generated videos can be recorded and downloaded for later viewing.

    Logs and the live screencast help you to compare and detect any changes that may have occurred since a similar test was last run. Debugging is set to false by default. To enable logs, set the BrowserStack custom capability browserstack.debug to true.

    "capabilities": {
      "browserstack.debug": true
    }
    
    Video recording

    Videos are recorded for every BrowserStack Automate test. This feature helps you to verify and debug failed tests, confirm the functionality of tested features, and download videos for later viewing.

    Note: Video recording increases test execution time slightly. You can disable this feature by setting the browserstack.video capability to false.

    "capabilities": {
      "browserstack.video": false
    }
    

    Other capabilities

    BrowserStack supports the full complement of Selenium capabilities, as well as, some custom ones.

    Additional Notes

    REST API

    It is possible to mark tests as either a pass or a fail, using the following snippet:

    file_get_contents('https://USERNAME:ACCESS_KEY@api.browserstack.com/automate/sessions/<session-id>.json', false, stream_context_create(array('http'=>array('method'=>'PUT','header'=>'Content-type: application/json', 'content'=>'{"status":"completed","reason":""}'))));
    

    The two potential values for status can either be completed or error. Optionally, a reason can also be passed.

    Queuing

    With queuing, you can launch an additional number of parallel tests with different browser configurations that will be queued in a sequence, for a seamless parallel execution. For instance, if you want to run 5 additional tests, apart from your subscribed limit of 2 parallel tests, BrowserStack will queue the additional 5 tests until one of the 2 initial tests finish, and a slot is available for execution. With queuing, you can be less bothered about managing your tests, and it can save development time.

    With this feature, accounts up to 5 parallel tests can queue 5 tests. Beyond 5 parallel tests, an equivalent number of tests will be queued.

    Note: The wait limit for the execution of a pending queued job is 15 minutes and will be cancelled if exceeded.

    We have provided examples of parallel testing implementation using popular testing frameworks. In order to increase the number of tests, purchase more parallel tests of your Automate or Automate Pro plan to get access to more tests.

    In This Article

    • Getting Started
    • Integration with BrowserStack
    • Testing on Internal Networks
    • Speed up testing
    • Configuring Capabilities
      • Run tests on desktop and mobile browsers
      • Builds and projects
      • Self-signed certificates
      • Enable and Disable Pop-ups
      • Debugging
      • Other capabilities
    • Additional Notes
      • REST API
      • Queuing

    Languages & Frameworks

    PHP

    Behat

    Codeception

    Related Articles

    Debugging Tools

    Browsers & Devices

    Capabilities

    Timeouts

    REST API

    JS Testing

    Local Testing

    Continuous Integration

    Travis CI

    TeamCity

    Products
    • Live
    • Automate
    • App Live New
    • App Automate New
    • Screenshots
    • Responsive
    • Enterprise
    Mobile
    • Test on Right Devices
    • Mobile Features
    • Mobile Emulators
    • Test on iPhone
    • Test on iPad
    • Test on Galaxy
    Other Links
    • Open Source
    • Test in IE
    • Careers We're hiring!
    • Support
    • Contact
    • Company
    • News
    Social
    Header logo

    © 2011-2018 BrowserStack - A cross-browser testing tool.

    • Terms of Service
    • Privacy Policy