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.
In this guide, you will learn:
Prerequisites
You need to have 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.
Configure BrowserStack Local
You can setup localhost testing using the following two ways:
-
Recommended: Via PIP (language bindings): The BrowserStack Local binary is installed using the
pip
command. It internally establishes a secure tunnel with BrowserStack when you run your test case. The package provides hooks that let you control the lifecycle of the tunnel. - Via CLI Interface (binary): You can also download the BrowserStack Local binary and setup the tunnel via your terminal. This method is useful when you don’t want to make any code changes in your test suite and want to manage the tunnel lifecycle independently.
Install the package
Add browserstack-local
to your project using the pip
command.
pip install browserstack-local
Set the access key and use available methods in your test script
Set your access key that is used to create the secure tunnel. Use the following methods provided by the local library to manage your local connection:
Method | Description |
---|---|
bs_local.start(**bs_local_args) |
Expects **bs_local_args object. Returns a callback when the tunnel has started successfully. Your test script should start executing after this callback has been invoked. |
bs_local.stop() |
Call this method after your test suite is complete. |
def start_local():
global bs_local
bs_local = Local()
bs_local_args = { "key": "YOUR_ACCESS_KEY", "forcelocal": "true" }
bs_local.start(**bs_local_args)
def stop_local():
global bs_local
bs_local.stop()
# Start BrowserStack local binary
start_local()
# Test case for the BrowserStack sample Android/iOS app.
# Invoke driver.quit() after the test is done to indicate that the test is completed.
driver.quit()
stop_local()
Add desired capabilities to enable local
Update your Python test (browserstack_sample_local.py
) file to set the local
capability to true
. You may face errors running your test script if any other capability is enabled before setting up local
.
desired_cap = {
"bstack:options": {
#Set the local capability as true
"local" : "true"
}
}
desired_caps = {
#Set BrowserStack Local capability as True
"browserstack.local": "true"
}
If your staging environment is behind a proxy or firewall, additional arguments, such as proxy username, proxy password, etc, need to be set. Check out Local Binary parameters to learn about additional arguments.
Execute build on BrowserStack
You are now ready to execute your build on BrowserStack. On your local machine, open the terminal/command prompt. Navigate to either the android
or ios
directory and run the test script.
# Using python
python3 browserstack_sample_local.py
Check BrowserStack Local connectivity
Run a sample test to check if BrowserStack Local is working as expected and can connect to BrowserStack servers.
Clone sample Git repository
Clone the sample python-browserstack repository and install required dependencies using the following commands:
# clone the repo
git clone https://github.com/browserstack/python-appium-app-browserstack
# go to the sample repo
cd python-appium-app-browserstack
# install required dependencies
pip3 install -r requirements.txt
Set BrowserStack credentials
Set userName
and accessKey
in the browserstack_sample_local.py
config file. Skip this step if you have already set credentials as environment variables.
userName = "YOUR_USERNAME"
accessKey = "YOUR_ACCESS_KEY"
Set local capability to true
Verify that the local
capability in the browserstack_sample_local.py
file is set to true
.
"bstack:options": {
"local" : "true"
}
Execute build on BrowserStack
From the root directory of this project, run the following command:
python3 browserstack_sample.py
View the test result on your App Automate Dashboard.
If the test fails, refer to the sections below for additional configuration that may be needed.
Additional configuration
BrowserStack Local works out of the box in most cases. However, depending on the URL from where your app fetches data, and your network setup, some additional configuration may be required. The following are the most frequently occurring scenarios:
The URL where app’s data resides is accessible from the internet but serves different content within your internal network.
This is common with staging environments set up as sub-domains and configured to work only in your private/VPN network. In this case, add the following entry in your browserstack_sample_local.py
file.
"forceLocal": "true"
The forceLocal
capability will resolve all network traffic through the BrowserStack Local tunnel.
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!