BrowserStack Local Python
The `browserstack-local` Python library provides bindings for the BrowserStack Local testing feature. It allows users to test websites hosted on local development environments, staging environments, or behind firewalls/proxies, by establishing a secure tunnel between the user's machine and the BrowserStack cloud. The library is currently at version 1.2.14 and maintains an active development status with a positive release cadence, often releasing new versions within three months.
Common errors
-
Another BrowserStack Local Instance Running
cause An existing BrowserStack Local process is already active on the machine, potentially from a previous test run, a different user, or an older version of the application. Using the same `localIdentifier` for multiple connections can also trigger this.fixTerminate all existing BrowserStack Local processes. If starting multiple local connections, ensure each uses a unique `localIdentifier` in `bs_local_args`. You can also use the `force: true` option in `bs_local_args` to automatically kill other instances. -
BrowserStackLocal binary is corrupt
cause The downloaded BrowserStack Local binary is damaged or could not be downloaded completely due to network instability, firewall restrictions, or insufficient file permissions.fixEnsure stable internet connectivity and appropriate firewall/proxy settings. The library (from v1.2.1) attempts to re-download corrupt binaries automatically. If the issue persists, manually delete the binary (typically located in a temporary directory specific to the library) to force a fresh download. -
Local connection not established (or tests failing with 'Host unreachable')
cause The secure tunnel between your local machine and the BrowserStack cloud could not be established. This often points to network instability, incorrect proxy settings, or firewall blocks preventing outgoing connections from the BrowserStack Local binary.fixVerify your internet connection. If you are behind a corporate proxy, ensure that `proxyHost`, `proxyPort`, `proxyUser`, and `proxyPass` are correctly configured in your `bs_local_args`. Check your system's firewall rules to ensure they allow outgoing connections from the BrowserStack Local binary.
Warnings
- gotcha Binary download issues or corruption: The `browserstack-local` library automatically downloads and manages the underlying BrowserStack Local binary. Network issues, restrictive firewalls, or insufficient file permissions can prevent successful download or corrupt the binary, leading to connection failures.
- gotcha Multiple Local instances: Running multiple instances of BrowserStack Local without unique `localIdentifier`s will cause older connections to drop, leading to unstable tests or connection failures.
- gotcha Incorrect `binary_path` option: While `v1.2.2` addressed issues with the `binary_path` option, incorrectly configuring it or pointing to an inaccessible path can still prevent the local tunnel from starting.
- gotcha Environment variable for access key: Prior to `v1.1.0`, there were known issues with environment variable handling for the `BROWSERSTACK_ACCESS_KEY`, which could lead to authentication failures.
Install
-
pip install browserstack-local
Imports
- Local
from browserstack.local import Local
Quickstart
import os
from browserstack.local import Local
bs_local = Local()
# Replace <browserstack-accesskey> with your key or set as environment variable BROWSERSTACK_ACCESS_KEY
access_key = os.environ.get('BROWSERSTACK_ACCESS_KEY', 'YOUR_BROWSERSTACK_ACCESS_KEY')
bs_local_args = {
"key": access_key,
"verbose": "true" # Optional: enable verbose logging
}
try:
# Start the Local instance
bs_local.start(**bs_local_args)
print("BrowserStack Local started.")
# Check if BrowserStack Local instance is running
if bs_local.isRunning():
print("BrowserStack Local is running.")
# Your test logic here that uses the local tunnel
else:
print("BrowserStack Local is NOT running.")
finally:
# Stop the Local instance
if bs_local.isRunning():
bs_local.stop()
print("BrowserStack Local stopped.")