Skip to content

Commit 328e241

Browse files
committed
[bidi][java] Add user friendly methods to locate nodes
1 parent c9bc81a commit 328e241

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,37 @@ public List<RemoteValue> locateNodes(LocateNodeParameters parameters) {
358358
}));
359359
}
360360

361+
public List<RemoteValue> locateNodes(Locator locator) {
362+
return this.bidi.send(
363+
new Command<>(
364+
"browsingContext.locateNodes",
365+
Map.of("context", id, "locator", locator.toMap()),
366+
jsonInput -> {
367+
Map<String, Object> result = jsonInput.read(Map.class);
368+
try (StringReader reader = new StringReader(JSON.toJson(result.get("nodes")));
369+
JsonInput input = JSON.newInput(reader)) {
370+
return input.read(new TypeToken<List<RemoteValue>>() {}.getType());
371+
}
372+
}));
373+
}
374+
375+
public RemoteValue locateNode(Locator locator) {
376+
List<RemoteValue> remoteValues =
377+
this.bidi.send(
378+
new Command<>(
379+
"browsingContext.locateNodes",
380+
Map.of("context", id, "locator", locator.toMap(), "maxNodeCount", 1),
381+
jsonInput -> {
382+
Map<String, Object> result = jsonInput.read(Map.class);
383+
try (StringReader reader = new StringReader(JSON.toJson(result.get("nodes")));
384+
JsonInput input = JSON.newInput(reader)) {
385+
return input.read(new TypeToken<List<RemoteValue>>() {}.getType());
386+
}
387+
}));
388+
389+
return remoteValues.get(0);
390+
}
391+
361392
public void close() {
362393
// This might need more clean up actions once the behavior is defined.
363394
// Specially when last tab or window is closed.

java/test/org/openqa/selenium/bidi/browsingcontext/LocateNodesTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,38 @@ void canLocateNodes() {
7171
assertThat(elements.size()).isEqualTo(13);
7272
}
7373

74+
@Test
75+
@NotYetImplemented(SAFARI)
76+
@NotYetImplemented(IE)
77+
@NotYetImplemented(CHROME)
78+
@NotYetImplemented(EDGE)
79+
@NotYetImplemented(FIREFOX)
80+
void canLocateNodesWithJustLocator() {
81+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
82+
assertThat(browsingContext.getId()).isNotEmpty();
83+
84+
driver.get(pages.xhtmlTestPage);
85+
86+
List<RemoteValue> elements = browsingContext.locateNodes(Locator.css("div"));
87+
assertThat(elements.size()).isEqualTo(13);
88+
}
89+
90+
@Test
91+
@NotYetImplemented(SAFARI)
92+
@NotYetImplemented(IE)
93+
@NotYetImplemented(CHROME)
94+
@NotYetImplemented(EDGE)
95+
@NotYetImplemented(FIREFOX)
96+
void canLocateNode() {
97+
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
98+
assertThat(browsingContext.getId()).isNotEmpty();
99+
100+
driver.get(pages.xhtmlTestPage);
101+
102+
RemoteValue element = browsingContext.locateNode(Locator.css("div"));
103+
assertThat(element.getType()).isEqualTo("node");
104+
}
105+
74106
@Test
75107
@NotYetImplemented(SAFARI)
76108
@NotYetImplemented(IE)

0 commit comments

Comments
 (0)