Run Selenium WebDriver tests on BrowserStack Automate
using the BrowserStack Java SDK — no test framework required. The SDK attaches as a
-javaagent:, reads browserstack.yml, and runs your tests across the configured platform
matrix with zero code changes for credentials, platforms, Test Observability, and Local.
This sample contains exactly two tests:
- Sample test (
BStackDemoTest) — an add-to-cart flow on bstackdemo.com. - Local test (
BStackLocalTest) — verifies the BrowserStack Local tunnel by openinghttp://bs-local.com:45454and asserting the page title.
- A BrowserStack account (username + access key).
- JDK 11+ — Selenium 4.23 requires Java 11 (
maven.compiler.source/target = 11). - Maven 3.6+.
- Clone this repository and change into it:
git clone https://github.com/browserstack/vanilla-java-browserstack.git cd vanilla-java-browserstack - Configure your BrowserStack credentials. Either edit
userName/accessKeyinbrowserstack.yml, or export them as environment variables (env vars take precedence):export BROWSERSTACK_USERNAME="YOUR_USERNAME" export BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"
- Resolve dependencies (downloads Selenium and the BrowserStack Java SDK):
mvn clean compile
The -javaagent: jar is wired automatically: the maven-dependency-plugin resolves the
SDK jar path into the ${com.browserstack:browserstack-java-sdk:jar} property, and the
exec-maven-plugin passes it as -javaagent: when launching each test class.
Runs the bstackdemo.com add-to-cart flow across the platforms in browserstack.yml:
mvn testOr, equivalently, via the exec goal:
mvn compile exec:execbrowserstackLocal: true in browserstack.yml tells the SDK to start a BrowserStack Local
tunnel before the session, so the remote browser can reach http://bs-local.com:45454:
mvn compile exec:exec -P sample-localTo run both tests concurrently:
mvn compile exec:exec -P run-parallel- View runs, video, logs, and network traffic at automate.browserstack.com.
- With
testObservability: true, the same build also appears in Test Observability. - Platforms, parallelism, Local, Observability, and debugging flags are all controlled from
browserstack.yml— no code changes needed to add browsers or devices. - Vanilla Java has no test runner, so
browserstack.ymlintentionally omits theframework:field. The-javaagent:still instruments the SeleniumRemoteWebDriverso sessions are created and reported. Addframework:only when adopting a supported test framework (TestNG / JUnit 4 / JUnit 5 / Cucumber).