Skip to content

Commit 3d0a440

Browse files
committed
#16573 unwrap double-wrapped webdriver
1 parent e487a36 commit 3d0a440

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

java/src/org/openqa/selenium/support/decorators/WebDriverDecorator.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ protected static class Definition {
195195

196196
public Definition(Decorated<?> decorated) {
197197
this.decoratedClass = decorated.getClass();
198-
this.originalClass = decorated.getOriginal().getClass();
198+
this.originalClass = unwrapOriginal(decorated).getClass();
199199
}
200200

201201
@Override
@@ -217,6 +217,17 @@ public int hashCode() {
217217
}
218218
}
219219

220+
private static Object unwrapOriginal(Decorated<?> decorated) {
221+
return unwrapWebdriver(decorated.getOriginal());
222+
}
223+
224+
private static Object unwrapWebdriver(Object webDriver) {
225+
if (webDriver instanceof WebDriver && webDriver instanceof WrapsDriver) {
226+
return unwrapWebdriver(((WrapsDriver) webDriver).getWrappedDriver());
227+
}
228+
return webDriver;
229+
}
230+
220231
public interface HasTarget<Z> {
221232
Decorated<Z> getTarget();
222233

@@ -480,12 +491,15 @@ private static void extractInterfaces(final Set<Class<?>> collector, final Class
480491
private <Z> Map<Class<?>, Function<Z, InvocationHandler>> deriveAdditionalInterfaces(Z sample) {
481492
Map<Class<?>, Function<Z, InvocationHandler>> handlers = new HashMap<>();
482493

483-
if (sample instanceof WebDriver && !(sample instanceof WrapsDriver)) {
494+
if (sample instanceof WebDriver) {
484495
handlers.put(
485496
WrapsDriver.class,
486497
(instance) ->
487498
(proxy, method, args) -> {
488499
if ("getWrappedDriver".equals(method.getName())) {
500+
return unwrapWebdriver(instance);
501+
}
502+
if (sample instanceof WrapsDriver) {
489503
return instance;
490504
}
491505
throw new UnsupportedOperationException(method.getName());

java/test/org/openqa/selenium/support/decorators/DecoratedRemoteWebDriverTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ void shouldImplementWrapsDriverToProvideAccessToUnderlyingDriver() {
5555
RemoteWebDriver underlying =
5656
(RemoteWebDriver) ((WrapsDriver) decoratedDriver).getWrappedDriver();
5757
assertThat(underlying.getSessionId()).isEqualTo(sessionId);
58+
assertThat(underlying).isSameAs(originalDriver);
59+
}
60+
61+
@Test
62+
void canWrapDriverMultipleTimes() {
63+
SessionId sessionId = new SessionId(UUID.randomUUID());
64+
RemoteWebDriver originalDriver = mock(RemoteWebDriver.class);
65+
when(originalDriver.getSessionId()).thenReturn(sessionId);
66+
67+
RemoteWebDriver decorated1 =
68+
new WebDriverDecorator<>(RemoteWebDriver.class).decorate(originalDriver);
69+
RemoteWebDriver decorated2 =
70+
new WebDriverDecorator<>(RemoteWebDriver.class).decorate(decorated1);
71+
RemoteWebDriver decoratedDriver =
72+
new WebDriverDecorator<>(RemoteWebDriver.class).decorate(decorated2);
73+
74+
assertThat(decoratedDriver.getSessionId()).isEqualTo(sessionId);
75+
76+
RemoteWebDriver underlying =
77+
(RemoteWebDriver) ((WrapsDriver) decoratedDriver).getWrappedDriver();
78+
assertThat(underlying.getSessionId()).isEqualTo(sessionId);
79+
assertThat(underlying).isSameAs(originalDriver);
5880
}
5981

6082
@Test

0 commit comments

Comments
 (0)