Account created. Keys set. Now let's run an actual test. The code is almost identical to what you wrote for Grid — just a different URL and some BrowserStack-specific capabilities.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class BrowserStackTest {
private static final String USERNAME =
System.getenv("BROWSERSTACK_USERNAME");
private static final String ACCESS_KEY =
System.getenv("BROWSERSTACK_ACCESS_KEY");
private static final String HUB_URL =
"https://" + USERNAME + ":" + ACCESS_KEY
+ "@hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception {
// Browser capabilities
ChromeOptions options = new ChromeOptions();
// BrowserStack-specific settings
Map<String, Object> bstackOptions = new HashMap<>();
bstackOptions.put("os", "Windows");
bstackOptions.put("osVersion", "11");
bstackOptions.put("browserVersion", "latest");
bstackOptions.put("projectName", "Banking Portal");
bstackOptions.put("buildName", "Regression #"
+ System.getenv("BUILD_NUMBER"));
bstackOptions.put("sessionName", "Login Test - Chrome");
// Debugging features
bstackOptions.put("debug", "true"); // Visual logs
bstackOptions.put("networkLogs", "true"); // HAR files
bstackOptions.put("consoleLogs", "verbose"); // JS console
options.setCapability("bstack:options", bstackOptions);
// Connect to BrowserStack — same RemoteWebDriver!
WebDriver driver = new RemoteWebDriver(
new URL(HUB_URL), options
);
try {
driver.get("https://your-app.com/login");
System.out.println("Title: " + driver.getTitle());
// ... your test steps here
} finally {
driver.quit();
}
}
}This is the killer feature. You can't install Safari on Windows or Linux. With BrowserStack, you just change the capabilities.
import org.openqa.selenium.safari.SafariOptions;
// Safari on macOS Sonoma
SafariOptions safariOpts = new SafariOptions();
Map<String, Object> bstackOptions = new HashMap<>();
bstackOptions.put("os", "OS X");
bstackOptions.put("osVersion", "Sonoma");
bstackOptions.put("browserVersion", "17.0");
bstackOptions.put("projectName", "Banking Portal");
bstackOptions.put("sessionName", "Login Test - Safari");
safariOpts.setCapability("bstack:options", bstackOptions);
WebDriver driver = new RemoteWebDriver(
new URL(HUB_URL), safariOpts
);Your app is running on localhost:3000. BrowserStack can't access it — it's on your machine. BrowserStack Local creates a secure tunnel between your machine and BrowserStack's cloud so their browsers can reach your localhost.
import com.browserstack.local.Local;
import java.util.HashMap;
import java.util.Map;
public class BrowserStackLocalExample {
private Local bsLocal;
public void startTunnel() throws Exception {
bsLocal = new Local();
Map<String, String> localOptions = new HashMap<>();
localOptions.put("key",
System.getenv("BROWSERSTACK_ACCESS_KEY"));
bsLocal.start(localOptions);
System.out.println("Tunnel running: " + bsLocal.isRunning());
}
public void stopTunnel() throws Exception {
if (bsLocal != null && bsLocal.isRunning()) {
bsLocal.stop();
}
}
}<!-- Add to pom.xml for BrowserStack Local -->
<dependency>
<groupId>com.browserstack</groupId>
<artifactId>browserstack-local-java</artifactId>
<version>1.1.1</version>
</dependency>Enable "debug: true" in BrowserStack capabilities. It records a video of your entire test session. When a test fails on Safari but passes on Chrome, that video shows you exactly what happened — invaluable for debugging.
Q: Have you used BrowserStack? How do you integrate it with your framework?
A: Yes. We use BrowserStack for testing on Safari (which we can't run locally on Linux CI) and real mobile devices. Integration is simple — we use RemoteWebDriver pointing to hub-cloud.browserstack.com with bstack:options capabilities specifying OS, browser version, and project name. Credentials come from environment variables. For testing our staging app on localhost, we use BrowserStack Local tunnel. We also enable debug mode for video recordings and network logs — these are crucial for debugging cross-browser failures.
Key Point: BrowserStack uses the same RemoteWebDriver pattern as Grid. Change the URL and add bstack:options capabilities. That's all the code change needed.