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.
- BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
- PHP installed on your machine.
- Git installed on your machine.
Run your first Local test
Configure your PHP tests for Local Testing using the following steps:
- Clone the sample php-selenium-browserstack repository using the following command:
git clone https://github.com/browserstack/php-selenium-browserstack.git
- 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
- 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);
- Verify that the
browserstack.local
capability is set totrue
in thescripts/local.php
file:$caps = array( ... "browserstack.local" => "true", ... );
- Run the PHP test using the following command:
cd scripts php local.php
- View your tests on your Automate Dashboard.
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.
<?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:
- Run multiple tests in parallel to speed up builds
- Test local websites that resides behind a proxy
- Manage Local Testing connections
- Set up your CI/CD: Jenkins, Bamboo, TeamCity, Azure, CircleCI, BitBucket, TravisCI, GitHub Actions, GoCD
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
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!