Skip to content

Commit

Permalink
[java] Remove circular dependency when using RemoteWebElement in BiDi…
Browse files Browse the repository at this point in the history
… classes (#13463)
  • Loading branch information
pujagani authored Mar 13, 2024
1 parent 9fa8a62 commit eddd906
Show file tree
Hide file tree
Showing 44 changed files with 307 additions and 31 deletions.
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/bidi/BiDi.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public <X> void addListener(Event<X> event, Consumer<X> handler) {
connection.addListener(event, handler);
}

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

<X> void addListener(Set<String> browsingContextIds, Event<X> event, Consumer<X> handler) {
public <X> void addListener(Set<String> browsingContextIds, Event<X> event, Consumer<X> handler) {
Require.nonNull("List of browsing context ids", browsingContextIds);
Require.nonNull("Event to listen for", event);
Require.nonNull("Handler to call", handler);
Expand Down
27 changes: 27 additions & 0 deletions java/src/org/openqa/selenium/bidi/browsingcontext/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "java_library")

java_library(
name = "browsingcontext",
srcs = glob(
[
"*.java",
],
),
visibility = [
"//java/src/org/openqa/selenium/bidi:__subpackages__",
"//java/src/org/openqa/selenium/firefox:__subpackages__",
"//java/src/org/openqa/selenium/remote:__pkg__",
"//java/test/org/openqa/selenium/bidi:__subpackages__",
"//java/test/org/openqa/selenium/grid:__subpackages__",
],
deps = [
"//java/src/org/openqa/selenium:core",
"//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"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
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 @@ -35,13 +37,16 @@
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 {

private static final Json JSON = new Json();

private final String id;
private final BiDi bidi;
private final WebDriver driver;
private static final String CONTEXT = "context";
private static final String RELOAD = "browsingContext.reload";
private static final String HANDLE_USER_PROMPT = "browsingContext.handleUserPrompt";
Expand Down Expand Up @@ -83,6 +88,7 @@ public BrowsingContext(WebDriver driver, String id) {

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

this.driver = driver;
this.bidi = ((HasBiDi) driver).getBiDi();
this.id = id;
}
Expand All @@ -94,6 +100,7 @@ public BrowsingContext(WebDriver driver, WindowType type) {
throw new IllegalArgumentException("WebDriver instance must support BiDi protocol");
}

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

this.driver = driver;
this.bidi = ((HasBiDi) driver).getBiDi();
this.id = this.create(type, referenceContextId);
}
Expand Down Expand Up @@ -389,10 +397,44 @@ public RemoteValue locateNode(Locator locator) {
return remoteValues.get(0);
}

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());
}
}
22 changes: 22 additions & 0 deletions java/src/org/openqa/selenium/bidi/log/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("//java:defs.bzl", "java_library")

java_library(
name = "log",
srcs = glob(
[
"*.java",
],
),
visibility = [
"//java/src/org/openqa/selenium/bidi:__subpackages__",
"//java/src/org/openqa/selenium/remote:__pkg__",
"//java/test/org/openqa/selenium/bidi:__subpackages__",
"//java/test/org/openqa/selenium/grid:__subpackages__",
],
deps = [
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/bidi",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote/http",
],
)
30 changes: 30 additions & 0 deletions java/src/org/openqa/selenium/bidi/module/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "java_library")

java_library(
name = "module",
srcs = glob(
[
"*.java",
],
),
visibility = [
"//java/src/org/openqa/selenium/bidi:__subpackages__",
"//java/src/org/openqa/selenium/firefox:__subpackages__",
"//java/src/org/openqa/selenium/remote:__pkg__",
"//java/test/org/openqa/selenium/bidi:__subpackages__",
"//java/test/org/openqa/selenium/grid:__subpackages__",
],
deps = [
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/bidi",
"//java/src/org/openqa/selenium/bidi/browsingcontext",
"//java/src/org/openqa/selenium/bidi/log",
"//java/src/org/openqa/selenium/bidi/network",
"//java/src/org/openqa/selenium/bidi/script",
"//java/src/org/openqa/selenium/bidi/storage",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote/http",
artifact("com.google.auto.service:auto-service-annotations"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi;
package org.openqa.selenium.bidi.module;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.Command;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.json.JsonInput;

public class Browser {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi;
package org.openqa.selenium.bidi.module;

import java.io.StringReader;
import java.util.Collections;
Expand All @@ -25,6 +25,9 @@
import java.util.function.Consumer;
import java.util.function.Function;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.Event;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.bidi.browsingcontext.BrowsingContextInfo;
import org.openqa.selenium.bidi.browsingcontext.NavigationInfo;
import org.openqa.selenium.bidi.browsingcontext.UserPromptClosed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi;
package org.openqa.selenium.bidi.module;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
Expand All @@ -24,6 +24,9 @@
import java.util.stream.Collectors;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.Command;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.interactions.Sequence;

public class Input {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi;
package org.openqa.selenium.bidi.module;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.bidi.log.BaseLogEntry;
import org.openqa.selenium.bidi.log.ConsoleLogEntry;
import org.openqa.selenium.bidi.log.FilterBy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi;
package org.openqa.selenium.bidi.module;

import java.util.Collections;
import java.util.HashSet;
Expand All @@ -24,6 +24,10 @@
import java.util.function.Consumer;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.Command;
import org.openqa.selenium.bidi.Event;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.bidi.network.AddInterceptParameters;
import org.openqa.selenium.bidi.network.BeforeRequestSent;
import org.openqa.selenium.bidi.network.FetchError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi;
package org.openqa.selenium.bidi.module;

import java.io.Closeable;
import java.io.StringReader;
Expand All @@ -29,6 +29,10 @@
import java.util.function.Consumer;
import java.util.function.Function;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.Command;
import org.openqa.selenium.bidi.Event;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.bidi.script.ChannelValue;
import org.openqa.selenium.bidi.script.EvaluateResult;
import org.openqa.selenium.bidi.script.EvaluateResultExceptionValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.bidi;
package org.openqa.selenium.bidi.module;

import java.io.StringReader;
import java.util.Map;
import java.util.function.Function;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.Command;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.bidi.storage.DeleteCookiesParameters;
import org.openqa.selenium.bidi.storage.GetCookiesParameters;
import org.openqa.selenium.bidi.storage.GetCookiesResult;
Expand Down
25 changes: 25 additions & 0 deletions java/src/org/openqa/selenium/bidi/network/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "java_library")

java_library(
name = "network",
srcs = glob(
[
"*.java",
],
),
visibility = [
"//java/src/org/openqa/selenium/bidi:__subpackages__",
"//java/src/org/openqa/selenium/firefox:__subpackages__",
"//java/src/org/openqa/selenium/remote:__pkg__",
"//java/test/org/openqa/selenium/bidi:__subpackages__",
"//java/test/org/openqa/selenium/grid:__subpackages__",
],
deps = [
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/bidi/log",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote/http",
artifact("com.google.auto.service:auto-service-annotations"),
],
)
25 changes: 25 additions & 0 deletions java/src/org/openqa/selenium/bidi/script/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "java_library")

java_library(
name = "script",
srcs = glob(
[
"*.java",
],
),
visibility = [
"//java/src/org/openqa/selenium/bidi:__subpackages__",
"//java/src/org/openqa/selenium/firefox:__subpackages__",
"//java/src/org/openqa/selenium/remote:__pkg__",
"//java/test/org/openqa/selenium/bidi:__subpackages__",
"//java/test/org/openqa/selenium/grid:__subpackages__",
],
deps = [
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/bidi/log",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote/http",
artifact("com.google.auto.service:auto-service-annotations"),
],
)
Loading

0 comments on commit eddd906

Please sign in to comment.