Run Selenium Tests Against Localhost Using BrowserStack Local
Route Selenium tests from BrowserStack cloud browsers to your localhost or private server using the BrowserStack Local tunnel.
04 Aug 2025, 11:07 UTC

Problem and answer
BrowserStack Automate runs Selenium sessions on browsers hosted in BrowserStack’s cloud. Those browsers cannot reach a web server running on your machine, a staging host behind a firewall, or any other private-network URL by default. If your test navigates to http://localhost:8000, the cloud browser tries to resolve localhost inside BrowserStack’s network, where nothing is listening.
The practical fix is BrowserStack Local: a small outbound tunnel binary you run on your own network or CI runner. It connects to BrowserStack, and the remote browser forwards requests for internal hostnames through that tunnel instead of the public internet. You do not need to expose your local server with a public URL or change inbound firewall rules.
How the tunnel routes traffic
The Local binary opens an outbound connection from your machine to BrowserStack. When a Selenium capability marks the session as local, BrowserStack maps that session to your active tunnel. The cloud browser then sends requests for localhost, private hostnames, or internal IP ranges back through the tunnel to your network. Each binary process carries one tunnel; concurrent tests need separate binary instances or unique local identifiers.
Worked Java configuration
This example assumes Java 11 or later, Selenium 4, and the BrowserStack Automate hub endpoint. Replace placeholders with values injected from CI secrets; never commit them to source control.
1. Start the Local binary
Run this on the CI host or developer machine where your test suite executes. The user needs permission to execute the binary and outbound network access to BrowserStack.
chmod +x ./BrowserStackLocal
./BrowserStackLocal --key "$BROWSERSTACK_ACCESS_KEY" --local-identifier "ci-tunnel-1" --daemon start
--key authenticates the tunnel with your BrowserStack account. --local-identifier names this tunnel so multiple suites can run separate tunnels in parallel. --daemon start keeps the binary running in the background; omit it if you want to control the process directly from your test runner.
2. Configure Selenium to use the tunnel
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
public class LocalTest {
public static void main(String[] args) throws Exception {
String username = System.getenv("BROWSERSTACK_USERNAME");
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
String hubUrl = "https://hub-cloud.browserstack.com/wd/hub";
ChromeOptions options = new ChromeOptions();
options.setCapability("browserstack.local", "true");
options.setCapability("browserstack.localIdentifier", "ci-tunnel-1");
options.setCapability("browserstack.user", username);
options.setCapability("browserstack.key", accessKey);
options.setCapability("browserName", "Chrome");
options.setCapability("os", "Windows");
options.setCapability("osVersion", "10");
WebDriver driver = new RemoteWebDriver(new URL(hubUrl), options);
try {
driver.get("http://localhost:8000");
System.out.println("Page title: " + driver.getTitle());
} finally {
driver.quit();
}
}
}
Set BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY in CI secrets or your shell. The browserstack.localIdentifier value must match the identifier used when starting the binary.
3. Stop the tunnel after the suite
Because starting the binary changes system state, stop it when tests finish so you do not consume a concurrent tunnel slot or leave an orphaned connection.
./BrowserStackLocal --key "$BROWSERSTACK_ACCESS_KEY" --local-identifier "ci-tunnel-1" stop
If you started with --daemon start, use the matching stop command. If you launched the binary directly from your test process, destroy the process in a finally block or shutdown hook.
Limits and common mistakes
- Tunnel must exist before the session starts. Enabling
browserstack.localwithout a running binary typically produces session-creation timeouts or DNS failures, not a clear "tunnel missing" error. - One tunnel per binary process. Running many parallel suites through one Local binary and one local identifier can fail or serialize traffic. Use separate binary instances and unique identifiers for concurrency.
- Not a replacement for public endpoints. Large file uploads or media-heavy pages are slower through the tunnel than direct internet access because traffic traverses the outbound connection.
- localhost means the tunnel host, not the remote browser. The cloud browser reaches your machine through the tunnel; it does not start a local server inside BrowserStack.
- Credential leaks. Committing access keys to source control exposes your BrowserStack account. Inject keys through environment variables or a secrets manager.
Verify the result
- Start a simple HTTP server on the machine running the tunnel, for example
python3 -m http.server 8000, and confirm it responds locally. - Start BrowserStack Local with your access key and a local identifier.
- Run the Selenium session above against
http://localhost:8000. - If the remote browser returns the local page, the tunnel is working. If it fails, check BrowserStack Automate session logs for DNS or connection errors, confirm the binary process is active, and verify outbound egress to BrowserStack is not blocked by a firewall.
Treat tunnel connection failures as infrastructure issues, not browser bugs. Validate the binary version and network path before debugging test code.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.