From a7595ac94783440ff8e01670b8e3d80145bdfd11 Mon Sep 17 00:00:00 2001 From: Aakash Hotchandani Date: Fri, 5 Jun 2026 12:44:21 +0530 Subject: [PATCH] Fix: Gauge runner + W3C session creation on sdk The sdk branch could not create any BrowserStack session: - gauge-java 0.6.0 mismatched the installed Gauge runtime -> 'Failed to start gauge API: Timed out connecting to java'. Bumped to 0.12.0. - selenium-java 3.0.1 -> 4.27.0 for JDK 21+ compatibility; added explicit junit 4.13.2 (no longer transitive from Selenium 4). - DriverFactory set flat JSON-wire caps (browser/os/...) which Selenium 4 rejects with 'Illegal key values seen in w3c capabilities'. Nested BrowserStack caps under bstack:options; build name now read from BROWSERSTACK_BUILD_NAME; null-safe teardown. - env chrome/default used Chrome 53 / Win 8.1 (incompatible chromedriver). Updated to Chrome latest / Windows 11. Verified: 4 BrowserStack Automate sessions created and status=done (Chrome 148 / Windows 11). Co-Authored-By: Claude Opus 4.8 --- env/chrome/browserstack.properties | 4 +- env/default/browserstack.properties | 4 +- pom.xml | 9 +++- .../gauge/pages/DriverFactory.java | 51 +++++++++++-------- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/env/chrome/browserstack.properties b/env/chrome/browserstack.properties index 1580dc2..8e0cbc6 100644 --- a/env/chrome/browserstack.properties +++ b/env/chrome/browserstack.properties @@ -1,4 +1,4 @@ BROWSER = Chrome -BROWSER_VERSION = 53 +BROWSER_VERSION = latest OS = Windows -OS_VERSION = 8.1 +OS_VERSION = 11 diff --git a/env/default/browserstack.properties b/env/default/browserstack.properties index 1580dc2..8e0cbc6 100644 --- a/env/default/browserstack.properties +++ b/env/default/browserstack.properties @@ -1,4 +1,4 @@ BROWSER = Chrome -BROWSER_VERSION = 53 +BROWSER_VERSION = latest OS = Windows -OS_VERSION = 8.1 +OS_VERSION = 11 diff --git a/pom.xml b/pom.xml index e068a88..bdaea04 100644 --- a/pom.xml +++ b/pom.xml @@ -75,12 +75,17 @@ com.thoughtworks.gauge gauge-java - 0.6.0 + 0.12.0 org.seleniumhq.selenium selenium-java - 3.0.1 + 4.27.0 + + + junit + junit + 4.13.2 diff --git a/src/test/java/com/browserstack/gauge/pages/DriverFactory.java b/src/test/java/com/browserstack/gauge/pages/DriverFactory.java index 7ccfb0a..ff3c9bd 100644 --- a/src/test/java/com/browserstack/gauge/pages/DriverFactory.java +++ b/src/test/java/com/browserstack/gauge/pages/DriverFactory.java @@ -2,13 +2,15 @@ import com.thoughtworks.gauge.AfterSpec; import com.thoughtworks.gauge.BeforeSpec; +import org.openqa.selenium.MutableCapabilities; import org.openqa.selenium.WebDriver; -import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import java.net.MalformedURLException; import java.net.URL; -import java.util.concurrent.TimeUnit; +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; public class DriverFactory { private static final String USERNAME = System.getenv("BROWSERSTACK_USERNAME"); @@ -21,34 +23,40 @@ public static WebDriver getDriver() { return driver; } + private static String envOr(String key, String fallback) { + String value = System.getenv(key); + return (value != null && !value.isEmpty()) ? value : fallback; + } + @BeforeSpec public void setUp() { try { - DesiredCapabilities caps = new DesiredCapabilities(); + // Selenium 4 enforces W3C. BrowserStack vendor capabilities must be + // nested under the "bstack:options" map rather than set as flat keys. + MutableCapabilities caps = new MutableCapabilities(); + Map bstackOptions = new HashMap<>(); - // Capabilities from environment - if(System.getenv("DEVICE") != null){ + if (System.getenv("DEVICE") != null) { caps.setCapability("browserName", System.getenv("BROWSERNAME")); - caps.setCapability("platform", System.getenv("PLATFORM")); - caps.setCapability("device", System.getenv("DEVICE")); - } - else { - caps.setCapability("browser", System.getenv("BROWSER")); - caps.setCapability("browser_version", System.getenv("BROWSER_VERSION")); - - caps.setCapability("os", System.getenv("OS")); - caps.setCapability("os_version", System.getenv("OS_VERSION")); + bstackOptions.put("deviceName", System.getenv("DEVICE")); + bstackOptions.put("osVersion", System.getenv("PLATFORM")); + bstackOptions.put("realMobile", "true"); + } else { + caps.setCapability("browserName", envOr("BROWSER", "Chrome")); + bstackOptions.put("browserVersion", envOr("BROWSER_VERSION", "latest")); + bstackOptions.put("os", envOr("OS", "Windows")); + bstackOptions.put("osVersion", envOr("OS_VERSION", "11")); } - // Hardcoded capabilities - caps.setCapability("build", "browserstack build"); - caps.setCapability("browserstack.debug", "true"); - caps.setCapability("name", "BStack Sample Gauge"); + bstackOptions.put("buildName", envOr("BROWSERSTACK_BUILD_NAME", "browserstack build")); + bstackOptions.put("sessionName", "BStack Sample Gauge"); + bstackOptions.put("debug", "true"); + caps.setCapability("bstack:options", bstackOptions); URL remoteURL = new URL(URL); driver = new RemoteWebDriver(remoteURL, caps); - driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); + driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); } catch (MalformedURLException e) { @@ -59,7 +67,8 @@ public void setUp() { @AfterSpec public void tearDown() { - driver.close(); - driver.quit(); + if (driver != null) { + driver.quit(); + } } }