Skip to content

Commit

Permalink
[bidi][java] Add user friendly methods to locate nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Jan 16, 2024
1 parent c9bc81a commit 328e241
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,37 @@ public List<RemoteValue> locateNodes(LocateNodeParameters parameters) {
}));
}

public List<RemoteValue> locateNodes(Locator locator) {
return this.bidi.send(
new Command<>(
"browsingContext.locateNodes",
Map.of("context", id, "locator", locator.toMap()),
jsonInput -> {
Map<String, Object> result = jsonInput.read(Map.class);
try (StringReader reader = new StringReader(JSON.toJson(result.get("nodes")));
JsonInput input = JSON.newInput(reader)) {
return input.read(new TypeToken<List<RemoteValue>>() {}.getType());
}
}));
}

public RemoteValue locateNode(Locator locator) {
List<RemoteValue> remoteValues =
this.bidi.send(
new Command<>(
"browsingContext.locateNodes",
Map.of("context", id, "locator", locator.toMap(), "maxNodeCount", 1),
jsonInput -> {
Map<String, Object> result = jsonInput.read(Map.class);
try (StringReader reader = new StringReader(JSON.toJson(result.get("nodes")));
JsonInput input = JSON.newInput(reader)) {
return input.read(new TypeToken<List<RemoteValue>>() {}.getType());
}
}));

return remoteValues.get(0);
}

public void close() {
// This might need more clean up actions once the behavior is defined.
// Specially when last tab or window is closed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,38 @@ void canLocateNodes() {
assertThat(elements.size()).isEqualTo(13);
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(CHROME)
@NotYetImplemented(EDGE)
@NotYetImplemented(FIREFOX)
void canLocateNodesWithJustLocator() {
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
assertThat(browsingContext.getId()).isNotEmpty();

driver.get(pages.xhtmlTestPage);

List<RemoteValue> elements = browsingContext.locateNodes(Locator.css("div"));
assertThat(elements.size()).isEqualTo(13);
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
@NotYetImplemented(CHROME)
@NotYetImplemented(EDGE)
@NotYetImplemented(FIREFOX)
void canLocateNode() {
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
assertThat(browsingContext.getId()).isNotEmpty();

driver.get(pages.xhtmlTestPage);

RemoteValue element = browsingContext.locateNode(Locator.css("div"));
assertThat(element.getType()).isEqualTo("node");
}

@Test
@NotYetImplemented(SAFARI)
@NotYetImplemented(IE)
Expand Down

0 comments on commit 328e241

Please sign in to comment.