Search by

Run your first test

BrowserStack App Automate enables you to test native and hybrid mobile applications using Appium automation framework. Its easy to run your Appium tests using PHPUnit on real Android and iOS devices on BrowserStack. This guide will help you get started with your first test.

1. Setup

Ensure you have PHPUnit libraries installed.

# Install using composer
composer require phpunit/phpunit-selenium
composer require appium/php-client

2. Upload your app

Upload your Android app (.apk or .aab file) or iOS app (.ipa file) to BrowserStack servers using our REST API. Here is an example cURL request to upload the app :

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/upload" \
-F "file=@/path/to/app/file/Application-debug.apk"

A sample response for the above request is shown below:

{
    "app_url":"bs://j3c874f21852ba57957a3fdc33f47514288c4ba4"
}

Note the value of app_url returned in the API response (bs://j3c874f21852b..... in the above example). This value will be used later to set the app capability to specify application under test in your Appium test scripts.

Note:
  1. App upload will take a few seconds to about a minute depending on the size of your app. Do not interrupt the cURL command until you get the response back.
  2. If you upload an iOS app, we will re-sign the app with our own provisioning profile to be able to install your app on our devices during test execution.

3. Configure and run the test

In this step, you will learn how to configure your Appium test script using desired capabilities to test remotely on BrowserStack’s real device cloud. Here is a sample test case written for running with PHPUnit:

If you are using our Sample App, the sample test below will install the Sample App (Wikipedia App) on the device, search for ‘browserstack’ and asserts for the list of results. If you are using your own app, modify the code as per your test cases.

class AppAutomateTest extends BrowserStackTest {

    public function testNativeApplication() {
        $el = $this->byAccessibilityId('Search Wikipedia');
        $el->click();
        $this->keys("BrowserStack");
        sleep(2);
        $textElements = $this->elements($this->using('class name')->value('android.widget.TextView'));
        $this->assertTrue(sizeof($textElements) > 0);
        $this->assertInstanceOf('PHPUnit_Extensions_AppiumTestCase_Element', $el);
    }
}

If you are using our Sample App, the sample test below will install the Sample App (WordPress App) on the device, navigate to the Login screen, enters the login email and check whether the email is registered on WordPress. If you are using your own app, modify the code as per your test cases.

class AppAutomateTest extends BrowserStackTest {

    public function testNativeApplication() {
        $el = $this->byAccessibilityId('Text Button');
        $el->click();
        sleep(2);
        $el2 = $this->byAccessibilityId('Text Input');
        $el2->click();
        sleep(2);
        $this->keys("hello@browserstack.com\n");
        sleep(5);
        $el3 = $this->byAccessibilityId('Text Output');
        printf("\n%s\n", $el3->text());
        $this->assertInstanceOf('PHPUnit_Extensions_AppiumTestCase_Element', $el);
        $this->assertTrue(strpos($el3->text(), "browserstack.com") != FALSE);
    }
}

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

Integration with BrowserStack

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

<?php

require 'vendor/autoload.php';
require 'lib/globals.php';
require_once('PHPUnit/Extensions/AppiumTestCase.php');
require_once('PHPUnit/Extensions/AppiumTestCase/Element.php');

class BrowserStackTest extends PHPUnit_Extensions_AppiumTestCase
{
    protected static $bs_local;
    public static $browsers = array(array( 'browserName' => '', 'desiredCapabilities' => array()));
    public function setupSpecificBrowser($params)
    {
        $CONFIG = $GLOBALS['CONFIG'];
        $task_id = getenv('TASK_ID') ? getenv('TASK_ID') : 0;

        $host = $GLOBALS['BROWSERSTACK_USERNAME'] . ":" . $GLOBALS['BROWSERSTACK_ACCESS_KEY'] . "@" . $CONFIG['server'];
        $caps = $this->getDesiredCapabilities();
        if(array_key_exists("browserstack.local", $GLOBALS['CONFIG']['capabilities']) && $GLOBALS['CONFIG']['capabilities']["browserstack.local"])
        {
            $bs_local_args = array("key" => $GLOBALS['BROWSERSTACK_ACCESS_KEY']);
            self::$bs_local = new BrowserStackLocal();
            self::$bs_local->start($bs_local_args);
        }
        $this->setHost($host);
        $this->setPort(443);
        $this->setSecure(TRUE);
        $caps = isset($params['desiredCapabilities']) ? $params['desiredCapabilities'] : array();
        if (!$local && !$host && isset($params['sessionStrategy'])) {
            $params['sessionStrategy'] = 'isolated';
        }
        $this->setDesiredCapabilities($caps);
        $this->setUpSessionStrategy($params);
    }

    public static function tearDownAfterClass()
    {
        if(self::$bs_local) self::$bs_local->stop();
    }
}
foreach($GLOBALS['CONFIG']['capabilities'] as $key => $value) {
    BrowserStackTest::$browsers[0]['desiredCapabilities'][$key] = $value;
}
foreach($GLOBALS['CONFIG']['devices'] as $device) {
    BrowserStackTest::$browsers[0]['desiredCapabilities']['device'] = $device["device"];
}
?>

The module reads from config file where you need to put the BrowserStack Hub URL and credentials. If the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY are set in Environment variables, then that will take a preference. Also, update the app_url(bs://….) obtained after uploading the App to the BrowserStack cloud in Step 2.

{
  "server": "hub-cloud.browserstack.com",
  "user": "YOUR_USERNAME",
  "key": "YOUR_ACCESS_KEY",

  "capabilities": {
    "project": "PHPUnit_AppAutomate",
    "build": "phpunit-app_automate-browserstack",
    "name": "android_single",
    "browserstack.debug": true,
    "app":"bs://<hashed appid>"
  },
  "devices": [{
    "device": "Samsung Galaxy S8"
  }]
}
{
  "server": "hub-cloud.browserstack.com",
  "user": "YOUR_USERNAME",
  "key": "YOUR_ACCESS_KEY",

  "capabilities": {
    "project": "PHPUnit_AppAutomate",
    "build": "phpunit-app_automate-browserstack",
    "name": "ios_single",
    "browserstack.debug": true,
    "app":"bs://<hashed appid>"
  },
  "devices": [{
    "device": "iPhone 7"
  }]
}

Run your test on BrowserStack using following command:

# Run using composer

  # For Android
  composer android_single

  # For IOS
  composer ios_single
Note: Refer to our GitHub repository for a complete example: BrowserStack - PHPUnit

4. Viewing test results

You can access results of your test sessions on the App Automate dashboard as well as using our REST API. You can drill down into the details of a specific test session to view its execution details and debugging information such as video recording, network logs and device logs.