Skip to content

Commit

Permalink
[bidi][java] Remove the method that converts node value to RemoteWebE…
Browse files Browse the repository at this point in the history
…lement

Adding dependency on "remote" in build file is causing circular dependency. Tried a few approaches but could not find a feasible solution. Also, this part can be added in the BiDiDelegator in the future when we port the findElement method.
  • Loading branch information
pujagani committed Jun 27, 2024
1 parent e8745c6 commit 164bf79
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ java_library(
"//java/src/org/openqa/selenium/bidi",
"//java/src/org/openqa/selenium/bidi/script",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote:api",
"//java/src/org/openqa/selenium/remote/http",
artifact("com.google.auto.service:auto-service-annotations"),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.Command;
Expand All @@ -37,8 +35,6 @@
import org.openqa.selenium.json.JsonInput;
import org.openqa.selenium.json.TypeToken;
import org.openqa.selenium.print.PrintOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;

public class BrowsingContext {

Expand Down Expand Up @@ -415,44 +411,10 @@ public RemoteValue locateNode(Locator locator) {
return remoteValues.get(0);

This comment has been minimized.

Copy link
@joerg1985

joerg1985 Jun 28, 2024

Member

@pujagani this is not directly related to the change, but i stumbled into this when looking at this.

There is no code to handle an empty list here, so this will raise an IndexOutOfBoundsException.
As i read the spec the returned list can be empty.

How about using an Optional<> here?

}

public WebElement locateElement(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());
}
}));

List<WebElement> elements = nodeRemoteValueToWebElementConverter(remoteValues);
return elements.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.
// Refer: https://github.com/w3c/webdriver-bidi/issues/187
this.bidi.send(new Command<>("browsingContext.close", Map.of(CONTEXT, id)));
}

private List<WebElement> nodeRemoteValueToWebElementConverter(List<RemoteValue> remoteValues) {
return remoteValues.stream()
.map(
remoteValue -> {
WebElement element = new RemoteWebElement();
((RemoteWebElement) element).setParent(((RemoteWebDriver) this.driver));
((RemoteWebElement) element)
.setFileDetector(((RemoteWebDriver) this.driver).getFileDetector());
remoteValue
.getSharedId()
.ifPresent(sharedId -> ((RemoteWebElement) element).setId(sharedId));
return element;
})
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.module.Script;
import org.openqa.selenium.bidi.script.EvaluateResult;
import org.openqa.selenium.bidi.script.EvaluateResultSuccess;
Expand Down Expand Up @@ -243,18 +242,7 @@ void canLocateNodesInAGivenSandbox() {

String sharedId = (String) ((RemoteValue) sharedIdMap.get("sharedId")).getValue().get();
assertThat(sharedId).isEqualTo(nodeId);
}

@Test
void canFindElement() {
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle());
assertThat(browsingContext.getId()).isNotEmpty();

driver.get(pages.xhtmlTestPage);

WebElement element = browsingContext.locateElement(Locator.css("p"));
assertThat(element.getText()).isEqualTo("Open new window");
}
}

@AfterEach
public void quitDriver() {
Expand Down

0 comments on commit 164bf79

Please sign in to comment.