Test Your Apps Locally With BrowserStack
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:
-
Via adding dependency (language bindings): The BrowserStackLocal binary is installed by adding it as a dependency in the
pom.xml
file. 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. Using language bindings is the recommended way to integrate localhost testing on BrowserStack. - 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
Install the BrowserStackLocal
language bindings by adding it as a dependency in the pom.xml
file.
<!-- Install using maven -->
<dependency>
<groupId>com.browserstack</groupId>
<artifactId>browserstack-local-java</artifactId>
<version>1.0.6</version>
</dependency>
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 |
---|---|
local.start(options) |
Expects options object. Returns a callback when the tunnel has started successfully. Your test script should start executing after this callback has been invoked. |
local.stop() |
Call this method after your test suite is complete. |
local.isRunning() |
Check if the BrowserStack local instance is running. |
// Start BrowserStack local binary
if(browserstackOptions.get("local") != null && browserstackOptions.get("local").toString() == "true"){
local = new Local();
Map<String, String> LocalOptions = new HashMap<String, String>();
LocalOptions.put("key", accessKey);
local.start(LocalOptions);
}
// Test case for the BrowserStack sample app.
// Invoke driver.quit() after the test is done to indicate that the test is completed.
driver.quit()
// Stop BrowserStack local binary
if(local != null) local.stop();
Add desired capabilities to enable local
Update your TestNG test (local.conf.json
) 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
.
"bstack:options": {
// Set the local capability as true
"local" : true
}
"capabilities": {
// Set the 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.
In your terminal/command-line, navigate to either android
or the ios
diectory and run the adjacent command.
# for Android
cd android
mvn test -P local
# for iOS
cd ios
mvn test -P local
Run your first local test
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 testng-browserstack repository and install required dependencies using the following commands:
Set BrowserStack credentials
Set userName
and accessKey
in the local.conf.json
config file. Skip this step if you have already set credentials as environment variables.
"capabilities": {
"bstack:options": {
"userName": "YOUR_USERNAME",
"accessKey": "YOUR_ACCESS_KEY",
}
}
Set local capability to true
Verify that the local
capability in the local.conf.json
file is set to true
.
"capabilities": {
"bstack:options": {
"local": true
}
},
Execute build on BrowserStack
From the root directory of this project, run the following command:
# Run using maven
mvn compile
mvn test -P local
View the test result on your App Automate Dashboard.
Next Steps
After you have successfully run your first test using BrowserStack Local, you can explore the following sections:
- Testing your local site that might be behind a proxy
- Local flags to customize your local testing setup
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!