Skip to main content
Introducing the Automate SDK! Get your entire test suite running on BrowserStack in minutes! Learn More.

Test on Internal Networks

BrowserStack enables you to run your PHP automated tests on your internal development environments, on localhost, and from behind a corporate firewall. This feature is called “Local Testing.”

Local Testing establishes a secure connection between your machine and the BrowserStack cloud. Once you set up Local Testing, all URLs work out of the box, including HTTPS URLs and those behind a proxy or firewall.

In this section, you’ll learn:

Prerequisites

If you have already run your first test, you can skip the prerequisites.

Run your first Local test

Configure your PHP tests for Local Testing using the following steps:

  1. Clone the sample php-selenium-browserstack repository using the following command:
     git clone https://github.com/browserstack/php-selenium-browserstack.git
    
  2. Run the following commands in your command-line to install the required dependencies:
     ## Navigate to the cloned repository
     cd php-selenium-browserstack
    
     # If you don't already use Composer, you can download the composer.phar binary:
     curl -sS https://getcomposer.org/installer | php
    
     # Include the binding:
     php composer.phar require browserstack/local:dev-master
    
     # Install all the dependencies:
     php composer.phar install
    
  3. Set your BrowserStack credentials in the scripts/local.php file as follows:
       // set your Access Key
       $bs_local_args = array("key" => "YOUR_ACCESS_KEY");
       //...
       // set your Username and Access Key when initializing the web driver
       $web_driver = RemoteWebDriver::create("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub", $caps);
    
  4. Verify that the browserstack.local capability is set to true in the scripts/local.php file:
     $caps = array(
       ...
       "browserstack.local" => "true",
       ...
     );
    
  5. Run the PHP test using the following command:
     cd scripts
     php local.php
    
  6. View your tests on your Automate Dashboard.
Protip: Check out our capability builder and select from a range of custom capabilities that BrowserStack supports.

Understand your Local test script

When you run the php local.php command, the local.php file within the scripts directory is executed. When the test is triggered, it:

  • Starts Local Testing connection
  • Opens http://bs-local.com:45691/check
  • Checks whether the web page contains the Up and running text
  • Marks the test as passed or failed based on the availability of the text
  • Stops the Local Testing connection.
local.php
  <?php
  require_once("vendor/autoload.php");
  use Facebook\WebDriver\Remote\RemoteWebDriver;
  use Facebook\WebDriver\WebDriverBy;
  use Facebook\WebDriver\WebDriverExpectedCondition;
  use BrowserStack\Local;

  # Creates an instance of Local
  $bs_local = new Local();

  # You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
  $bs_local_args = array("key" => "YOUR_ACCESS_KEY");
  # Starts the Local instance with the required arguments
  $bs_local->start($bs_local_args);

  # Check if BrowserStack local instance is running
  echo $bs_local->isRunning();
  $caps = array(
      "browserName" => "iPhone",
      "device" => "iPhone 11",
      "realMobile" => "true",
      "os_version" => "14.0",
      "browserstack.local" => "true",
      "name" => "BStack-[Php] Sample Test", // test name
      "build" => "BStack Build Number 1" // CI/CD job or build name
  );
  $web_driver = RemoteWebDriver::create("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",$caps);
  try{
      $web_driver->get("http://bs-local.com:45691/check");
      $body_text = $web_driver->wait(10000)->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::cssSelector("body")))->getText();
      # Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page starts with 'BrowserStack'
      if ($body_text == "Up and running"){
          $web_driver->executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Local test ran successfully"}}' );
      }  else {
          $web_driver->executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Failed to load local test"}}');
      }
  }
  catch(Exception $e){
      echo 'Message: ' .$e->getMessage();
  }
  $web_driver->quit();
    # Stop the Local instance
  $bs_local->stop();
?>

Next steps

After you have successfully run your first test using BrowserStack Local, you can explore the following sections:

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