Skip to content

Commit b287e73

Browse files
authored
[java] Add remote web driver related interfaces for dynamic proxy creation (#9466)
Fixes #9416. It is related to the motivation of #8346 (which will help create a Proxy due to the interface).
1 parent bb805a6 commit b287e73

File tree

8 files changed

+149
-17
lines changed

8 files changed

+149
-17
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.openqa.selenium.remote;
2+
3+
import org.openqa.selenium.HasCapabilities;
4+
import org.openqa.selenium.JavascriptExecutor;
5+
import org.openqa.selenium.PrintsPage;
6+
import org.openqa.selenium.TakesScreenshot;
7+
import org.openqa.selenium.WebDriver;
8+
import org.openqa.selenium.interactions.HasInputDevices;
9+
import org.openqa.selenium.interactions.Interactive;
10+
import org.openqa.selenium.virtualauthenticator.HasVirtualAuthenticator;
11+
12+
public interface IsRemoteWebDriver extends WebDriver,
13+
JavascriptExecutor,
14+
HasInputDevices,
15+
HasCapabilities,
16+
Interactive,
17+
TakesScreenshot,
18+
HasVirtualAuthenticator,
19+
PrintsPage {
20+
21+
public SessionId getSessionId();
22+
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.remote;
19+
20+
import org.openqa.selenium.TakesScreenshot;
21+
import org.openqa.selenium.WebElement;
22+
import org.openqa.selenium.WrapsDriver;
23+
import org.openqa.selenium.interactions.Locatable;
24+
25+
public interface IsRemoteWebElement extends WebElement, WrapsDriver, TakesScreenshot, Locatable {
26+
27+
public String getId();
28+
29+
}

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@
2525
import org.openqa.selenium.Capabilities;
2626
import org.openqa.selenium.Cookie;
2727
import org.openqa.selenium.Dimension;
28-
import org.openqa.selenium.HasCapabilities;
2928
import org.openqa.selenium.ImmutableCapabilities;
3029
import org.openqa.selenium.InvalidArgumentException;
31-
import org.openqa.selenium.JavascriptExecutor;
3230
import org.openqa.selenium.MutableCapabilities;
3331
import org.openqa.selenium.NoSuchElementException;
3432
import org.openqa.selenium.NoSuchFrameException;
@@ -37,16 +35,12 @@
3735
import org.openqa.selenium.Pdf;
3836
import org.openqa.selenium.Platform;
3937
import org.openqa.selenium.Point;
40-
import org.openqa.selenium.PrintsPage;
4138
import org.openqa.selenium.SearchContext;
4239
import org.openqa.selenium.SessionNotCreatedException;
43-
import org.openqa.selenium.TakesScreenshot;
4440
import org.openqa.selenium.WebDriver;
4541
import org.openqa.selenium.WebDriverException;
4642
import org.openqa.selenium.WebElement;
4743
import org.openqa.selenium.WindowType;
48-
import org.openqa.selenium.interactions.HasInputDevices;
49-
import org.openqa.selenium.interactions.Interactive;
5044
import org.openqa.selenium.interactions.Keyboard;
5145
import org.openqa.selenium.interactions.Mouse;
5246
import org.openqa.selenium.interactions.Sequence;
@@ -65,7 +59,6 @@
6559
import org.openqa.selenium.remote.tracing.Tracer;
6660
import org.openqa.selenium.remote.tracing.opentelemetry.OpenTelemetryTracer;
6761
import org.openqa.selenium.virtualauthenticator.Credential;
68-
import org.openqa.selenium.virtualauthenticator.HasVirtualAuthenticator;
6962
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticator;
7063
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticatorOptions;
7164

@@ -96,9 +89,7 @@
9689
import static org.openqa.selenium.remote.CapabilityType.SUPPORTS_JAVASCRIPT;
9790

9891
@Augmentable
99-
public class RemoteWebDriver implements WebDriver, JavascriptExecutor, HasInputDevices,
100-
HasCapabilities, Interactive, TakesScreenshot,
101-
HasVirtualAuthenticator, PrintsPage {
92+
public class RemoteWebDriver implements IsRemoteWebDriver {
10293

10394
// TODO: This static logger should be unified with the per-instance localLogs
10495
private static final Logger logger = Logger.getLogger(RemoteWebDriver.class.getName());
@@ -240,6 +231,7 @@ public void setFileDetector(FileDetector detector) {
240231
fileDetector = detector;
241232
}
242233

234+
@Override
243235
public SessionId getSessionId() {
244236
return sessionId;
245237
}

java/client/src/org/openqa/selenium/remote/RemoteWebElement.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@
2626
import org.openqa.selenium.Point;
2727
import org.openqa.selenium.Rectangle;
2828
import org.openqa.selenium.SearchContext;
29-
import org.openqa.selenium.TakesScreenshot;
3029
import org.openqa.selenium.WebDriver;
3130
import org.openqa.selenium.WebDriverException;
3231
import org.openqa.selenium.WebElement;
33-
import org.openqa.selenium.WrapsDriver;
3432
import org.openqa.selenium.WrapsElement;
3533
import org.openqa.selenium.interactions.Coordinates;
36-
import org.openqa.selenium.interactions.Locatable;
3734
import org.openqa.selenium.io.Zip;
3835

3936
import java.io.File;
@@ -43,7 +40,7 @@
4340
import java.util.Map;
4441
import java.util.stream.Collectors;
4542

46-
public class RemoteWebElement implements WebElement, WrapsDriver, TakesScreenshot, Locatable {
43+
public class RemoteWebElement implements IsRemoteWebElement {
4744

4845
private String foundBy;
4946
protected String id;
@@ -58,6 +55,7 @@ public void setParent(RemoteWebDriver parent) {
5855
this.parent = parent;
5956
}
6057

58+
@Override
6159
public String getId() {
6260
return id;
6361
}

java/client/src/org/openqa/selenium/remote/internal/WebElementToJsonConverter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openqa.selenium.WrapsElement;
2525
import org.openqa.selenium.remote.Dialect;
2626
import org.openqa.selenium.remote.RemoteWebElement;
27+
import org.openqa.selenium.remote.IsRemoteWebElement;
2728

2829
import java.util.Arrays;
2930
import java.util.Collection;
@@ -52,10 +53,10 @@ public Object apply(Object arg) {
5253
arg = ((WrapsElement) arg).getWrappedElement();
5354
}
5455

55-
if (arg instanceof RemoteWebElement) {
56+
if (arg instanceof IsRemoteWebElement) {
5657
return ImmutableMap.of(
57-
Dialect.OSS.getEncodedElementKey(), ((RemoteWebElement) arg).getId(),
58-
Dialect.W3C.getEncodedElementKey(), ((RemoteWebElement) arg).getId());
58+
Dialect.OSS.getEncodedElementKey(), ((IsRemoteWebElement) arg).getId(),
59+
Dialect.W3C.getEncodedElementKey(), ((IsRemoteWebElement) arg).getId());
5960
}
6061

6162
if (arg.getClass().isArray()) {

java/client/test/org/openqa/selenium/support/decorators/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ java_test_suite(
1111
),
1212
deps = [
1313
"//java/client/src/org/openqa/selenium:core",
14+
"//java/client/src/org/openqa/selenium/remote",
1415
"//java/client/src/org/openqa/selenium/support/decorators",
1516
"//java/client/test/org/openqa/selenium/testing:annotations",
1617
artifact("junit:junit"),
18+
artifact("com.google.guava:guava"),
1719
artifact("org.assertj:assertj-core"),
1820
artifact("org.mockito:mockito-core"),
1921
],
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.support.decorators;
19+
20+
import com.google.common.collect.ImmutableMap;
21+
import org.junit.Test;
22+
import org.junit.experimental.categories.Category;
23+
import org.openqa.selenium.By;
24+
import org.openqa.selenium.WebDriver;
25+
import org.openqa.selenium.WebElement;
26+
import org.openqa.selenium.remote.Dialect;
27+
import org.openqa.selenium.remote.IsRemoteWebDriver;
28+
import org.openqa.selenium.remote.IsRemoteWebElement;
29+
import org.openqa.selenium.remote.RemoteWebDriver;
30+
import org.openqa.selenium.remote.RemoteWebElement;
31+
import org.openqa.selenium.remote.SessionId;
32+
import org.openqa.selenium.remote.internal.WebElementToJsonConverter;
33+
import org.openqa.selenium.testing.UnitTests;
34+
35+
import java.util.UUID;
36+
37+
import static org.assertj.core.api.Assertions.assertThat;
38+
import static org.mockito.ArgumentMatchers.any;
39+
import static org.mockito.Mockito.mock;
40+
import static org.mockito.Mockito.verify;
41+
import static org.mockito.Mockito.when;
42+
import static org.openqa.selenium.remote.Dialect.OSS;
43+
44+
@Category(UnitTests.class)
45+
public class DecoratedRemoteWebDriverTest {
46+
47+
@Test
48+
public void canConvertDecoratedToRemoteWebDriverInterface() {
49+
SessionId sessionId = new SessionId(UUID.randomUUID());
50+
RemoteWebDriver originalDriver = mock(RemoteWebDriver.class);
51+
when(originalDriver.getSessionId()).thenReturn(sessionId);
52+
53+
IsRemoteWebDriver decoratedDriver = (IsRemoteWebDriver) new WebDriverDecorator().decorate(originalDriver);
54+
55+
assertThat(decoratedDriver.getSessionId()).isEqualTo(sessionId);
56+
}
57+
58+
@Test(expected = ClassCastException.class)
59+
public void cannotConvertDecoratedToRemoteWebDriver() {
60+
SessionId sessionId = new SessionId(UUID.randomUUID());
61+
RemoteWebDriver originalDriver = mock(RemoteWebDriver.class);
62+
when(originalDriver.getSessionId()).thenReturn(sessionId);
63+
64+
RemoteWebDriver decoratedDriver = (RemoteWebDriver) new WebDriverDecorator().decorate(originalDriver);
65+
}
66+
67+
@Test
68+
public void canConvertDecoratedRemoteWebElementToJson() {
69+
RemoteWebDriver originalDriver = mock(RemoteWebDriver.class);
70+
RemoteWebElement originalElement = new RemoteWebElement();
71+
String elementId = UUID.randomUUID().toString();
72+
originalElement.setParent(originalDriver);
73+
originalElement.setId(elementId);
74+
75+
when(originalDriver.findElement(any())).thenReturn(originalElement);
76+
77+
WebDriver decoratedDriver = new WebDriverDecorator().decorate(originalDriver);
78+
79+
WebElement element = decoratedDriver.findElement(By.id("test"));
80+
WebElementToJsonConverter converter = new WebElementToJsonConverter();
81+
ImmutableMap<String, String> result = (ImmutableMap<String, String>) converter.apply(element);
82+
83+
assertThat(result.get(Dialect.OSS.getEncodedElementKey())).isEqualTo(elementId);
84+
}
85+
86+
}

java/client/test/org/openqa/selenium/support/decorators/DecoratedWebDriverTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.openqa.selenium.WebDriver;
3838
import org.openqa.selenium.WebElement;
3939
import org.openqa.selenium.interactions.Interactive;
40+
import org.openqa.selenium.remote.RemoteWebDriver;
4041
import org.openqa.selenium.testing.UnitTests;
4142
import org.openqa.selenium.virtualauthenticator.HasVirtualAuthenticator;
4243
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticator;

0 commit comments

Comments
 (0)