Skip to content

Commit eddd906

Browse files
authored
[java] Remove circular dependency when using RemoteWebElement in BiDi classes (#13463)
1 parent 9fa8a62 commit eddd906

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+307
-31
lines changed

java/src/org/openqa/selenium/bidi/BiDi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public <X> void addListener(Event<X> event, Consumer<X> handler) {
6262
connection.addListener(event, handler);
6363
}
6464

65-
<X> void addListener(String browsingContextId, Event<X> event, Consumer<X> handler) {
65+
public <X> void addListener(String browsingContextId, Event<X> event, Consumer<X> handler) {
6666
Require.nonNull("Event to listen for", event);
6767
Require.nonNull("Browsing context id", browsingContextId);
6868
Require.nonNull("Handler to call", handler);
@@ -79,7 +79,7 @@ <X> void addListener(String browsingContextId, Event<X> event, Consumer<X> handl
7979
connection.addListener(event, handler);
8080
}
8181

82-
<X> void addListener(Set<String> browsingContextIds, Event<X> event, Consumer<X> handler) {
82+
public <X> void addListener(Set<String> browsingContextIds, Event<X> event, Consumer<X> handler) {
8383
Require.nonNull("List of browsing context ids", browsingContextIds);
8484
Require.nonNull("Event to listen for", event);
8585
Require.nonNull("Handler to call", handler);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
load("@rules_jvm_external//:defs.bzl", "artifact")
2+
load("//java:defs.bzl", "java_library")
3+
4+
java_library(
5+
name = "browsingcontext",
6+
srcs = glob(
7+
[
8+
"*.java",
9+
],
10+
),
11+
visibility = [
12+
"//java/src/org/openqa/selenium/bidi:__subpackages__",
13+
"//java/src/org/openqa/selenium/firefox:__subpackages__",
14+
"//java/src/org/openqa/selenium/remote:__pkg__",
15+
"//java/test/org/openqa/selenium/bidi:__subpackages__",
16+
"//java/test/org/openqa/selenium/grid:__subpackages__",
17+
],
18+
deps = [
19+
"//java/src/org/openqa/selenium:core",
20+
"//java/src/org/openqa/selenium/bidi",
21+
"//java/src/org/openqa/selenium/bidi/script",
22+
"//java/src/org/openqa/selenium/json",
23+
"//java/src/org/openqa/selenium/remote:api",
24+
"//java/src/org/openqa/selenium/remote/http",
25+
artifact("com.google.auto.service:auto-service-annotations"),
26+
],
27+
)

java/src/org/openqa/selenium/bidi/browsingcontext/BrowsingContext.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.function.Function;
27+
import java.util.stream.Collectors;
2728
import org.openqa.selenium.WebDriver;
29+
import org.openqa.selenium.WebElement;
2830
import org.openqa.selenium.WindowType;
2931
import org.openqa.selenium.bidi.BiDi;
3032
import org.openqa.selenium.bidi.Command;
@@ -35,13 +37,16 @@
3537
import org.openqa.selenium.json.JsonInput;
3638
import org.openqa.selenium.json.TypeToken;
3739
import org.openqa.selenium.print.PrintOptions;
40+
import org.openqa.selenium.remote.RemoteWebDriver;
41+
import org.openqa.selenium.remote.RemoteWebElement;
3842

3943
public class BrowsingContext {
4044

4145
private static final Json JSON = new Json();
4246

4347
private final String id;
4448
private final BiDi bidi;
49+
private final WebDriver driver;
4550
private static final String CONTEXT = "context";
4651
private static final String RELOAD = "browsingContext.reload";
4752
private static final String HANDLE_USER_PROMPT = "browsingContext.handleUserPrompt";
@@ -83,6 +88,7 @@ public BrowsingContext(WebDriver driver, String id) {
8388

8489
Require.precondition(!id.isEmpty(), "Browsing Context id cannot be empty");
8590

91+
this.driver = driver;
8692
this.bidi = ((HasBiDi) driver).getBiDi();
8793
this.id = id;
8894
}
@@ -94,6 +100,7 @@ public BrowsingContext(WebDriver driver, WindowType type) {
94100
throw new IllegalArgumentException("WebDriver instance must support BiDi protocol");
95101
}
96102

103+
this.driver = driver;
97104
this.bidi = ((HasBiDi) driver).getBiDi();
98105
this.id = this.create(type);
99106
}
@@ -108,6 +115,7 @@ public BrowsingContext(WebDriver driver, WindowType type, String referenceContex
108115
throw new IllegalArgumentException("WebDriver instance must support BiDi protocol");
109116
}
110117

118+
this.driver = driver;
111119
this.bidi = ((HasBiDi) driver).getBiDi();
112120
this.id = this.create(type, referenceContextId);
113121
}
@@ -389,10 +397,44 @@ public RemoteValue locateNode(Locator locator) {
389397
return remoteValues.get(0);
390398
}
391399

400+
public WebElement locateElement(Locator locator) {
401+
List<RemoteValue> remoteValues =
402+
this.bidi.send(
403+
new Command<>(
404+
"browsingContext.locateNodes",
405+
Map.of("context", id, "locator", locator.toMap(), "maxNodeCount", 1),
406+
jsonInput -> {
407+
Map<String, Object> result = jsonInput.read(Map.class);
408+
try (StringReader reader = new StringReader(JSON.toJson(result.get("nodes")));
409+
JsonInput input = JSON.newInput(reader)) {
410+
return input.read(new TypeToken<List<RemoteValue>>() {}.getType());
411+
}
412+
}));
413+
414+
List<WebElement> elements = nodeRemoteValueToWebElementConverter(remoteValues);
415+
return elements.get(0);
416+
}
417+
392418
public void close() {
393419
// This might need more clean up actions once the behavior is defined.
394420
// Specially when last tab or window is closed.
395421
// Refer: https://github.com/w3c/webdriver-bidi/issues/187
396422
this.bidi.send(new Command<>("browsingContext.close", Map.of(CONTEXT, id)));
397423
}
424+
425+
private List<WebElement> nodeRemoteValueToWebElementConverter(List<RemoteValue> remoteValues) {
426+
return remoteValues.stream()
427+
.map(
428+
remoteValue -> {
429+
WebElement element = new RemoteWebElement();
430+
((RemoteWebElement) element).setParent(((RemoteWebDriver) this.driver));
431+
((RemoteWebElement) element)
432+
.setFileDetector(((RemoteWebDriver) this.driver).getFileDetector());
433+
remoteValue
434+
.getSharedId()
435+
.ifPresent(sharedId -> ((RemoteWebElement) element).setId(sharedId));
436+
return element;
437+
})
438+
.collect(Collectors.toList());
439+
}
398440
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
load("//java:defs.bzl", "java_library")
2+
3+
java_library(
4+
name = "log",
5+
srcs = glob(
6+
[
7+
"*.java",
8+
],
9+
),
10+
visibility = [
11+
"//java/src/org/openqa/selenium/bidi:__subpackages__",
12+
"//java/src/org/openqa/selenium/remote:__pkg__",
13+
"//java/test/org/openqa/selenium/bidi:__subpackages__",
14+
"//java/test/org/openqa/selenium/grid:__subpackages__",
15+
],
16+
deps = [
17+
"//java/src/org/openqa/selenium:core",
18+
"//java/src/org/openqa/selenium/bidi",
19+
"//java/src/org/openqa/selenium/json",
20+
"//java/src/org/openqa/selenium/remote/http",
21+
],
22+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
load("@rules_jvm_external//:defs.bzl", "artifact")
2+
load("//java:defs.bzl", "java_library")
3+
4+
java_library(
5+
name = "module",
6+
srcs = glob(
7+
[
8+
"*.java",
9+
],
10+
),
11+
visibility = [
12+
"//java/src/org/openqa/selenium/bidi:__subpackages__",
13+
"//java/src/org/openqa/selenium/firefox:__subpackages__",
14+
"//java/src/org/openqa/selenium/remote:__pkg__",
15+
"//java/test/org/openqa/selenium/bidi:__subpackages__",
16+
"//java/test/org/openqa/selenium/grid:__subpackages__",
17+
],
18+
deps = [
19+
"//java/src/org/openqa/selenium:core",
20+
"//java/src/org/openqa/selenium/bidi",
21+
"//java/src/org/openqa/selenium/bidi/browsingcontext",
22+
"//java/src/org/openqa/selenium/bidi/log",
23+
"//java/src/org/openqa/selenium/bidi/network",
24+
"//java/src/org/openqa/selenium/bidi/script",
25+
"//java/src/org/openqa/selenium/bidi/storage",
26+
"//java/src/org/openqa/selenium/json",
27+
"//java/src/org/openqa/selenium/remote/http",
28+
artifact("com.google.auto.service:auto-service-annotations"),
29+
],
30+
)

java/src/org/openqa/selenium/bidi/Browser.java renamed to java/src/org/openqa/selenium/bidi/module/Browser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package org.openqa.selenium.bidi;
18+
package org.openqa.selenium.bidi.module;
1919

2020
import java.util.ArrayList;
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.function.Function;
2424
import org.openqa.selenium.WebDriver;
25+
import org.openqa.selenium.bidi.BiDi;
26+
import org.openqa.selenium.bidi.Command;
27+
import org.openqa.selenium.bidi.HasBiDi;
2528
import org.openqa.selenium.json.JsonInput;
2629

2730
public class Browser {

java/src/org/openqa/selenium/bidi/BrowsingContextInspector.java renamed to java/src/org/openqa/selenium/bidi/module/BrowsingContextInspector.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package org.openqa.selenium.bidi;
18+
package org.openqa.selenium.bidi.module;
1919

2020
import java.io.StringReader;
2121
import java.util.Collections;
@@ -25,6 +25,9 @@
2525
import java.util.function.Consumer;
2626
import java.util.function.Function;
2727
import org.openqa.selenium.WebDriver;
28+
import org.openqa.selenium.bidi.BiDi;
29+
import org.openqa.selenium.bidi.Event;
30+
import org.openqa.selenium.bidi.HasBiDi;
2831
import org.openqa.selenium.bidi.browsingcontext.BrowsingContextInfo;
2932
import org.openqa.selenium.bidi.browsingcontext.NavigationInfo;
3033
import org.openqa.selenium.bidi.browsingcontext.UserPromptClosed;

java/src/org/openqa/selenium/bidi/Input.java renamed to java/src/org/openqa/selenium/bidi/module/Input.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package org.openqa.selenium.bidi;
18+
package org.openqa.selenium.bidi.module;
1919

2020
import java.lang.reflect.InvocationTargetException;
2121
import java.util.Collection;
@@ -24,6 +24,9 @@
2424
import java.util.stream.Collectors;
2525
import org.openqa.selenium.WebDriver;
2626
import org.openqa.selenium.WebElement;
27+
import org.openqa.selenium.bidi.BiDi;
28+
import org.openqa.selenium.bidi.Command;
29+
import org.openqa.selenium.bidi.HasBiDi;
2730
import org.openqa.selenium.interactions.Sequence;
2831

2932
public class Input {

java/src/org/openqa/selenium/bidi/LogInspector.java renamed to java/src/org/openqa/selenium/bidi/module/LogInspector.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package org.openqa.selenium.bidi;
18+
package org.openqa.selenium.bidi.module;
1919

2020
import java.util.Collections;
2121
import java.util.HashSet;
2222
import java.util.Set;
2323
import java.util.concurrent.atomic.AtomicReference;
2424
import java.util.function.Consumer;
2525
import org.openqa.selenium.WebDriver;
26+
import org.openqa.selenium.bidi.BiDi;
27+
import org.openqa.selenium.bidi.HasBiDi;
2628
import org.openqa.selenium.bidi.log.BaseLogEntry;
2729
import org.openqa.selenium.bidi.log.ConsoleLogEntry;
2830
import org.openqa.selenium.bidi.log.FilterBy;

java/src/org/openqa/selenium/bidi/Network.java renamed to java/src/org/openqa/selenium/bidi/module/Network.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package org.openqa.selenium.bidi;
18+
package org.openqa.selenium.bidi.module;
1919

2020
import java.util.Collections;
2121
import java.util.HashSet;
@@ -24,6 +24,10 @@
2424
import java.util.function.Consumer;
2525
import org.openqa.selenium.UsernameAndPassword;
2626
import org.openqa.selenium.WebDriver;
27+
import org.openqa.selenium.bidi.BiDi;
28+
import org.openqa.selenium.bidi.Command;
29+
import org.openqa.selenium.bidi.Event;
30+
import org.openqa.selenium.bidi.HasBiDi;
2731
import org.openqa.selenium.bidi.network.AddInterceptParameters;
2832
import org.openqa.selenium.bidi.network.BeforeRequestSent;
2933
import org.openqa.selenium.bidi.network.FetchError;

0 commit comments

Comments
 (0)