Skip to content

Commit 506acb2

Browse files
Add vanilla Java (Selenium) BrowserStack SDK Automate sample
1 parent 1485752 commit 506acb2

11 files changed

Lines changed: 481 additions & 59 deletions

File tree

.github/workflows/Semgrep.yml

Lines changed: 0 additions & 49 deletions
This file was deleted.

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Maven
2+
target/
3+
4+
# IDE
5+
.idea/
6+
*.iml
7+
.vscode/
8+
.project
9+
.classpath
10+
.settings/
11+
12+
# OS
13+
.DS_Store
14+
15+
# BrowserStack / logs
16+
local.log
17+
logs/
18+
log/
19+
20+
# Credentials — never commit
21+
.env

.npmrc

Lines changed: 0 additions & 7 deletions
This file was deleted.

CODEOWNERS

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,82 @@
1-
# vanilla-java-browserstack
2-
We require the following new public repositories under the browserstack GitHub organization to host customer-facing sample projects for the BrowserStack SDK.
1+
# Vanilla Java (Selenium) with BrowserStack
2+
3+
Run Selenium WebDriver tests on [BrowserStack Automate](https://automate.browserstack.com/)
4+
using the **BrowserStack Java SDK** — no test framework required. The SDK attaches as a
5+
`-javaagent:`, reads `browserstack.yml`, and runs your tests across the configured platform
6+
matrix with zero code changes for credentials, platforms, Test Observability, and Local.
7+
8+
This sample contains exactly two tests:
9+
10+
- **Sample test** (`BStackDemoTest`) — an add-to-cart flow on [bstackdemo.com](https://bstackdemo.com/).
11+
- **Local test** (`BStackLocalTest`) — verifies the BrowserStack Local tunnel by opening
12+
`http://bs-local.com:45454` and asserting the page title.
13+
14+
## Prerequisites
15+
16+
- A [BrowserStack account](https://www.browserstack.com/users/sign_up) (username + access key).
17+
- **JDK 11+** — Selenium 4.23 requires Java 11 (`maven.compiler.source/target = 11`).
18+
- **Maven 3.6+**.
19+
20+
## Setup
21+
22+
1. Clone this repository and change into it:
23+
```bash
24+
git clone https://github.com/browserstack/vanilla-java-browserstack.git
25+
cd vanilla-java-browserstack
26+
```
27+
2. Configure your BrowserStack credentials. Either edit `userName` / `accessKey` in
28+
`browserstack.yml`, or export them as environment variables (env vars take precedence):
29+
```bash
30+
export BROWSERSTACK_USERNAME="YOUR_USERNAME"
31+
export BROWSERSTACK_ACCESS_KEY="YOUR_ACCESS_KEY"
32+
```
33+
3. Resolve dependencies (downloads Selenium and the BrowserStack Java SDK):
34+
```bash
35+
mvn clean compile
36+
```
37+
38+
The `-javaagent:` jar is wired automatically: the `maven-dependency-plugin` resolves the
39+
SDK jar path into the `${com.browserstack:browserstack-java-sdk:jar}` property, and the
40+
`exec-maven-plugin` passes it as `-javaagent:` when launching each test class.
41+
42+
## Run Sample Test
43+
44+
Runs the bstackdemo.com add-to-cart flow across the platforms in `browserstack.yml`:
45+
46+
```bash
47+
mvn test
48+
```
49+
50+
Or, equivalently, via the exec goal:
51+
52+
```bash
53+
mvn compile exec:exec
54+
```
55+
56+
## Run Local Test
57+
58+
`browserstackLocal: true` in `browserstack.yml` tells the SDK to start a BrowserStack Local
59+
tunnel before the session, so the remote browser can reach `http://bs-local.com:45454`:
60+
61+
```bash
62+
mvn compile exec:exec -P sample-local
63+
```
64+
65+
To run both tests concurrently:
66+
67+
```bash
68+
mvn compile exec:exec -P run-parallel
69+
```
70+
71+
## Notes / Dashboard
72+
73+
- View runs, video, logs, and network traffic at
74+
[automate.browserstack.com](https://automate.browserstack.com/).
75+
- With `testObservability: true`, the same build also appears in
76+
[Test Observability](https://observability.browserstack.com/).
77+
- Platforms, parallelism, Local, Observability, and debugging flags are all controlled from
78+
`browserstack.yml` — no code changes needed to add browsers or devices.
79+
- Vanilla Java has no test runner, so `browserstack.yml` intentionally omits the `framework:`
80+
field. The `-javaagent:` still instruments the Selenium `RemoteWebDriver` so sessions are
81+
created and reported. Add `framework:` only when adopting a supported test framework
82+
(TestNG / JUnit 4 / JUnit 5 / Cucumber).

browserstack.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# =============================
2+
# Set BrowserStack Credentials
3+
# =============================
4+
# Add your BrowserStack userName and accessKey here or set BROWSERSTACK_USERNAME and
5+
# BROWSERSTACK_ACCESS_KEY as env variables
6+
userName: YOUR_USERNAME
7+
accessKey: YOUR_ACCESS_KEY
8+
9+
# ======================
10+
# BrowserStack Reporting
11+
# ======================
12+
projectName: BrowserStack Samples
13+
buildName: browserstack build
14+
buildIdentifier: '#${BUILD_NUMBER}'
15+
# NOTE: no `framework:` field for vanilla Java — there is no test runner for the SDK to
16+
# instrument. The `-javaagent:` jar still instruments the Selenium RemoteWebDriver
17+
# constructor, so sessions are created and reported. Set `framework:` only when using a
18+
# supported test framework (testng / junit4 / junit5 / cucumber).
19+
20+
# =======================================
21+
# Platforms (Browsers / Devices to test)
22+
# =======================================
23+
platforms:
24+
- os: OS X
25+
osVersion: Big Sur
26+
browserName: Chrome
27+
browserVersion: latest
28+
- os: Windows
29+
osVersion: 10
30+
browserName: Edge
31+
browserVersion: latest
32+
- deviceName: Samsung Galaxy S22 Ultra
33+
browserName: chrome
34+
osVersion: 12.0
35+
36+
# =======================
37+
# Parallels per Platform
38+
# =======================
39+
parallelsPerPlatform: 1
40+
41+
# ==========================================
42+
# BrowserStack Automate + Local
43+
# ==========================================
44+
browserstackAutomation: true
45+
browserstackLocal: true
46+
47+
source: vanilla-java-browserstack:sample-sdk:v1.0
48+
49+
# ======================
50+
# Test Observability
51+
# ======================
52+
testObservability: true
53+
54+
# ===================
55+
# Debugging features
56+
# ===================
57+
debug: false
58+
networkLogs: false
59+
consoleLogs: errors

pom.xml

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<groupId>com.browserstack</groupId>
5+
<artifactId>vanilla-java-browserstack</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
9+
<name>vanilla-java-browserstack</name>
10+
<url>https://github.com/browserstack/vanilla-java-browserstack</url>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<maven.compiler.source>11</maven.compiler.source>
15+
<maven.compiler.target>11</maven.compiler.target>
16+
<selenium.version>4.23.0</selenium.version>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.seleniumhq.selenium</groupId>
22+
<artifactId>selenium-java</artifactId>
23+
<version>${selenium.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.browserstack</groupId>
27+
<artifactId>browserstack-java-sdk</artifactId>
28+
<version>LATEST</version>
29+
<scope>compile</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>io.github.bonigarcia</groupId>
33+
<artifactId>webdrivermanager</artifactId>
34+
<version>5.9.2</version>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-compiler-plugin</artifactId>
43+
<version>3.8.1</version>
44+
<configuration>
45+
<source>${maven.compiler.source}</source>
46+
<target>${maven.compiler.target}</target>
47+
</configuration>
48+
</plugin>
49+
50+
<!-- Resolves the browserstack-java-sdk jar path so it can be wired as a -javaagent. -->
51+
<plugin>
52+
<artifactId>maven-dependency-plugin</artifactId>
53+
<executions>
54+
<execution>
55+
<id>getClasspathFilenames</id>
56+
<goals>
57+
<goal>properties</goal>
58+
</goals>
59+
</execution>
60+
</executions>
61+
</plugin>
62+
63+
<!-- Default `mvn exec:exec` (and `mvn test`) runs the add-to-cart sample test. -->
64+
<plugin>
65+
<groupId>org.codehaus.mojo</groupId>
66+
<artifactId>exec-maven-plugin</artifactId>
67+
<version>3.0.0</version>
68+
<executions>
69+
<execution>
70+
<phase>test</phase>
71+
<goals>
72+
<goal>exec</goal>
73+
</goals>
74+
</execution>
75+
</executions>
76+
<configuration>
77+
<executable>java</executable>
78+
<arguments>
79+
<argument>-javaagent:${com.browserstack:browserstack-java-sdk:jar}</argument>
80+
<argument>-cp</argument>
81+
<classpath />
82+
<argument>com.browserstack.tests.BStackDemoTest</argument>
83+
</arguments>
84+
</configuration>
85+
</plugin>
86+
</plugins>
87+
</build>
88+
89+
<profiles>
90+
<!-- mvn exec:exec -P sample-local → BrowserStack Local tunnel test -->
91+
<profile>
92+
<id>sample-local</id>
93+
<build>
94+
<plugins>
95+
<plugin>
96+
<groupId>org.codehaus.mojo</groupId>
97+
<artifactId>exec-maven-plugin</artifactId>
98+
<version>3.0.0</version>
99+
<executions>
100+
<execution>
101+
<goals>
102+
<goal>exec</goal>
103+
</goals>
104+
</execution>
105+
</executions>
106+
<configuration>
107+
<executable>java</executable>
108+
<arguments>
109+
<argument>-javaagent:${com.browserstack:browserstack-java-sdk:jar}</argument>
110+
<argument>-cp</argument>
111+
<classpath />
112+
<argument>com.browserstack.tests.BStackLocalTest</argument>
113+
</arguments>
114+
</configuration>
115+
</plugin>
116+
</plugins>
117+
</build>
118+
</profile>
119+
120+
<!-- mvn exec:exec -P run-parallel → run both tests concurrently -->
121+
<profile>
122+
<id>run-parallel</id>
123+
<build>
124+
<plugins>
125+
<plugin>
126+
<groupId>org.codehaus.mojo</groupId>
127+
<artifactId>exec-maven-plugin</artifactId>
128+
<version>3.0.0</version>
129+
<executions>
130+
<execution>
131+
<goals>
132+
<goal>exec</goal>
133+
</goals>
134+
</execution>
135+
</executions>
136+
<configuration>
137+
<executable>java</executable>
138+
<arguments>
139+
<argument>-javaagent:${com.browserstack:browserstack-java-sdk:jar}</argument>
140+
<argument>-cp</argument>
141+
<classpath />
142+
<argument>com.browserstack.runner.ParallelTestRunner</argument>
143+
</arguments>
144+
</configuration>
145+
</plugin>
146+
</plugins>
147+
</build>
148+
</profile>
149+
</profiles>
150+
</project>

0 commit comments

Comments
 (0)