Local Testing with App Automate

Local Testing is a BrowserStack feature that helps you test mobile apps that access resources hosted in development or testing environments during automated test execution. This page will guide you through enabling Local Testing for App Automate sessions, and then using it to test apps that retrieve data from servers on your local machine, CI/CD machines/nodes, and other private network configurations.

Enabling Local Testing with PHPUnit is a simple three-step process:

  • Establishing a Local Testing connection.
  • Configuring the PHPUnit config file so they run through the Local Testing connection.
  • Running your local test on BrowserStack.
Note: In order to try a local test, you can upload our Local Sample App for Android and iOS

1. Establishing a Local Testing connection

You can start a Local Testing connection through your command-line interface by following the steps below:

  1. Download the appropriate binary for your system:

    Note: The download links are secure. The binaries are digitally signed, identifying the publisher as ‘BrowserStack Ltd’.

  2. Unzip the binary to a folder/directory on your machine.
  3. Open your command-line interface and navigate to the folder containing the Local binary.
  4. Run the binary using the following command
BrowserStackLocal.exe --key YOUR_ACCESS_KEY
./BrowserStackLocal --key YOUR_ACCESS_KEY

2. Configuring the PHPUnit config file to run with Local Testing connection

Once the local bindings are installed and local testing connection is established you need to update the config file and set the browserstack.local capability to true.

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

  "capabilities": {
    "project": "PHPUnit_AppAutomate",
    "build": "phpunit-app_automate-browserstack",
    "name": "android_local",
    "browserstack.debug": true,
    "browserstack.local": true,
    "app":"<app_url>"
  },
  "devices": [{
    "device": "Samsung Galaxy S8"
  }]
}

<?php
    
     //code to start browserstack local before start of test
    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 BrowserStack\Local();
        self::$bs_local->start($bs_local_args);
    }
    
    // code to stop browserstack local after end of test
    public static function tearDownAfterClass()
    {
        if(self::$bs_local) self::$bs_local->stop();
    }
?>
{
  "server": "hub-cloud.browserstack.com",
  "user": "YOUR_USERNAME",
  "key": "YOUR_ACCESS_KEY",

  "capabilities": {
    "project": "PHPUnit_AppAutomate",
    "build": "phpunit-app_automate-browserstack",
    "name": "android_local",
    "browserstack.debug": true,
    "browserstack.local": true,
    "app":"<app_url>"
  },
  "devices": [{
    "device": "iPhone 7"
  }]
}

<?php
    //code to start browserstack local before start of test
    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 BrowserStack\Local();
        self::$bs_local->start($bs_local_args);
    }
    
    // code to stop browserstack local after end of test
    public static function tearDownAfterClass()
    {
        if(self::$bs_local) self::$bs_local->stop();
    }
?>

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

class AppAutomateTest extends BrowserStackTest {

    public function testNativeApplication() {
        $el = $this->byId('com.example.android.basicnetworking:id/test_action');
        $el->click();
        sleep(2);
        $textElements = $this->elements($this->using('class name')->value('android.widget.TextView'));
        $testElement = null;
        foreach($textElements as $element) {
          if(strpos($element->text(), 'The active connection is')) {
            $testElement = $element;
          }
        }
        if($testElement == null) {
          $screenshotPath = getcwd() . '/screenshot.png';
          $handle = fopen($screenshotPath, 'w');
          $data = $this->currentScreenshot();
          fwrite($handle, $data);
          fclose($handle);
          printf("Screenshot stored at %s\n", $screenshotPath);
          printf("Cannot find the needed text view\n");
        }
        else {
          $matchedString = $testElement->text();
          $this->assertTrue(strpos($matchedString, 'The active connection is wifi') != false);
          $this->assertTrue(strpos($matchedString, 'Up and running') != false);
        }
    }
}

3. Running your local test on BrowserStack

Run your local test on BrowserStack using following command:

# Run using composer

# android
composer android_local

# ios
composer ios_local
Note: Refer to our GitHub repository for a complete example: BrowserStack - PHPUnit

We're sorry to hear that. Please share your feedback so we can do better







Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better







Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Talk to an Expert