Skip to content

Commit 1eee757

Browse files
[java][bidi] Fix BiDi detection and readiness state mapping
- Guard BiDi path with instanceof HasBiDi check to prevent IllegalArgumentException on plain RemoteWebDriver instances - Check webSocketUrl instanceof String (not just != null) to avoid false positive on Boolean.TRUE during session setup - Handle PageLoadStrategy as both enum and String when mapping to ReadinessState to fix eager/none being silently ignored Addresses review feedback on #13995
1 parent e06bc92 commit 1eee757

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

java/src/org/openqa/selenium/remote/RemoteWebDriver.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.openqa.selenium.JavascriptExecutor;
6363
import org.openqa.selenium.MutableCapabilities;
6464
import org.openqa.selenium.NoAlertPresentException;
65+
import org.openqa.selenium.PageLoadStrategy;
6566
import org.openqa.selenium.NoSuchFrameException;
6667
import org.openqa.selenium.NoSuchWindowException;
6768
import org.openqa.selenium.OutputType;
@@ -373,19 +374,32 @@ public Capabilities getCapabilities() {
373374

374375
@Override
375376
public void get(String url) {
376-
if (getCapabilities().getCapability("webSocketUrl") != null) {
377+
if (isBiDiEnabled()) {
377378
new BrowsingContext(this, getWindowHandle())
378379
.navigate(url, getReadinessState());
379380
} else {
380381
execute(DriverCommand.GET(url));
381382
}
382383
}
383384

385+
// BiDi is active when the driver implements HasBiDi and the session returned a WebSocket URL
386+
// (a String), not just the boolean request capability that was sent at session creation.
387+
private boolean isBiDiEnabled() {
388+
return this instanceof HasBiDi
389+
&& getCapabilities().getCapability("webSocketUrl") instanceof String;
390+
}
391+
384392
private ReadinessState getReadinessState() {
385-
Object strategy = getCapabilities().getCapability(CapabilityType.PAGE_LOAD_STRATEGY);
386-
if ("eager".equals(strategy)) {
393+
Object raw = getCapabilities().getCapability(CapabilityType.PAGE_LOAD_STRATEGY);
394+
// The capability may be a PageLoadStrategy enum (set locally) or a String (deserialized from
395+
// JSON), so normalise to the enum via toString() before comparing.
396+
PageLoadStrategy strategy =
397+
raw instanceof PageLoadStrategy
398+
? (PageLoadStrategy) raw
399+
: PageLoadStrategy.fromString(raw == null ? null : raw.toString());
400+
if (PageLoadStrategy.EAGER.equals(strategy)) {
387401
return ReadinessState.INTERACTIVE;
388-
} else if ("none".equals(strategy)) {
402+
} else if (PageLoadStrategy.NONE.equals(strategy)) {
389403
return ReadinessState.NONE;
390404
}
391405
return ReadinessState.COMPLETE;
@@ -1235,7 +1249,7 @@ private class RemoteNavigation implements Navigation {
12351249

12361250
@Override
12371251
public void back() {
1238-
if (getCapabilities().getCapability("webSocketUrl") != null) {
1252+
if (isBiDiEnabled()) {
12391253
new BrowsingContext(RemoteWebDriver.this, getWindowHandle()).back();
12401254
} else {
12411255
execute(DriverCommand.GO_BACK);
@@ -1244,7 +1258,7 @@ public void back() {
12441258

12451259
@Override
12461260
public void forward() {
1247-
if (getCapabilities().getCapability("webSocketUrl") != null) {
1261+
if (isBiDiEnabled()) {
12481262
new BrowsingContext(RemoteWebDriver.this, getWindowHandle()).forward();
12491263
} else {
12501264
execute(DriverCommand.GO_FORWARD);
@@ -1263,7 +1277,7 @@ public void to(URL url) {
12631277

12641278
@Override
12651279
public void refresh() {
1266-
if (getCapabilities().getCapability("webSocketUrl") != null) {
1280+
if (isBiDiEnabled()) {
12671281
new BrowsingContext(RemoteWebDriver.this, getWindowHandle())
12681282
.reload(getReadinessState());
12691283
} else {

0 commit comments

Comments
 (0)