Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON serialization for ShadowRoot #13680

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,4 @@ bazel-testlogs
javascript/node/selenium-webdriver/.vscode/settings.json

dotnet-bin
.metadata/
10 changes: 10 additions & 0 deletions java/spotbugs-excludes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,14 @@
<Class name="org.openqa.selenium.testing.StaticResources"/>
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/>
</Match>

<Match>
<Class name="org.openqa.selenium.remote.WebElementToJsonConverter" />
<Bug pattern="NM_SAME_SIMPLE_NAME_AS_INTERFACE" />
</Match>

<Match>
<Class name="org.openqa.selenium.remote.internal.WebElementToJsonConverter" />
<Bug pattern="NM_SAME_SIMPLE_NAME_AS_SUPERCLASS" />
</Match>
</FindBugsFilter>
1 change: 0 additions & 1 deletion java/src/org/openqa/selenium/remote/RemoteWebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.remote.http.ConnectionFailedException;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.internal.WebElementToJsonConverter;
import org.openqa.selenium.remote.tracing.TracedHttpClient;
import org.openqa.selenium.remote.tracing.Tracer;
import org.openqa.selenium.remote.tracing.opentelemetry.OpenTelemetryTracer;
Expand Down
93 changes: 93 additions & 0 deletions java/src/org/openqa/selenium/remote/WebElementToJsonConverter.java
sbabcoc marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.remote;

import static java.util.stream.Collectors.toList;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.openqa.selenium.WrapsElement;

/**
* Converts {@link RemoteWebElement} objects, which may be {@link WrapsElement wrapped}, into their
* JSON representation as defined by the WebDriver wire protocol. This class will recursively
* convert Lists and Maps to catch nested references.
*
* @see <a
* href="https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#webelement-json-object">
* WebDriver JSON Wire Protocol</a>
*/
public class WebElementToJsonConverter implements Function<Object, Object> {
@Override
public Object apply(Object arg) {
if (arg == null || arg instanceof String || arg instanceof Boolean || arg instanceof Number) {
return arg;
}

while (arg instanceof WrapsElement) {
arg = ((WrapsElement) arg).getWrappedElement();
}

if (arg instanceof RemoteWebElement) {
return Map.of(Dialect.W3C.getEncodedElementKey(), ((RemoteWebElement) arg).getId());
}

if (arg instanceof ShadowRoot) {
return Map.of(Dialect.W3C.getShadowRootElementKey(), ((ShadowRoot) arg).getId());
}
sbabcoc marked this conversation as resolved.
Show resolved Hide resolved

if (arg.getClass().isArray()) {
arg = arrayToList(arg);
}

if (arg instanceof Collection<?>) {
Collection<?> args = (Collection<?>) arg;
return args.stream().map(this).collect(toList());
}

if (arg instanceof Map<?, ?>) {
Map<?, ?> args = (Map<?, ?>) arg;
Map<String, Object> converted = new HashMap<>(args.size());
for (Map.Entry<?, ?> entry : args.entrySet()) {
Object key = entry.getKey();
if (!(key instanceof String)) {
throw new IllegalArgumentException(
"All keys in Map script arguments must be strings: " + key.getClass().getName());
}
converted.put((String) key, apply(entry.getValue()));
}
return converted;
}

throw new IllegalArgumentException(
"Argument is of an illegal type: " + arg.getClass().getName());
}

private static List<Object> arrayToList(Object array) {
List<Object> list = new ArrayList<>();
for (int i = 0; i < Array.getLength(array); i++) {
list.add(Array.get(array, i));
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
import java.util.stream.Stream;
import org.openqa.selenium.InvalidSelectorException;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.WebElementToJsonConverter;
import org.openqa.selenium.remote.codec.AbstractHttpCommandCodec;
import org.openqa.selenium.remote.internal.WebElementToJsonConverter;

/**
* A command codec that adheres to the W3C's WebDriver wire protocol.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,75 +17,12 @@

package org.openqa.selenium.remote.internal;

import static java.util.stream.Collectors.toList;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.openqa.selenium.WrapsElement;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.RemoteWebElement;

/**
* Converts {@link RemoteWebElement} objects, which may be {@link WrapsElement wrapped}, into their
* JSON representation as defined by the WebDriver wire protocol. This class will recursively
* convert Lists and Maps to catch nested references.
* {@inheritDoc}
*
* @see <a
* href="https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#webelement-json-object">
* WebDriver JSON Wire Protocol</a>
* @deprecated This class has moved to a {@link org.openqa.selenium.remote.WebElementToJsonConverter
* new location}.
*/
public class WebElementToJsonConverter implements Function<Object, Object> {
@Override
public Object apply(Object arg) {
if (arg == null || arg instanceof String || arg instanceof Boolean || arg instanceof Number) {
return arg;
}

while (arg instanceof WrapsElement) {
arg = ((WrapsElement) arg).getWrappedElement();
}

if (arg instanceof RemoteWebElement) {
return Map.of(Dialect.W3C.getEncodedElementKey(), ((RemoteWebElement) arg).getId());
}

if (arg.getClass().isArray()) {
arg = arrayToList(arg);
}

if (arg instanceof Collection<?>) {
Collection<?> args = (Collection<?>) arg;
return args.stream().map(this).collect(toList());
}

if (arg instanceof Map<?, ?>) {
Map<?, ?> args = (Map<?, ?>) arg;
Map<String, Object> converted = new HashMap<>(args.size());
for (Map.Entry<?, ?> entry : args.entrySet()) {
Object key = entry.getKey();
if (!(key instanceof String)) {
throw new IllegalArgumentException(
"All keys in Map script arguments must be strings: " + key.getClass().getName());
}
converted.put((String) key, apply(entry.getValue()));
}
return converted;
}

throw new IllegalArgumentException(
"Argument is of an illegal type: " + arg.getClass().getName());
}

private static List<Object> arrayToList(Object array) {
List<Object> list = new ArrayList<>();
for (int i = 0; i < Array.getLength(array); i++) {
list.add(Array.get(array, i));
}
return list;
}
}
@Deprecated
public class WebElementToJsonConverter
extends org.openqa.selenium.remote.WebElementToJsonConverter {}
1 change: 1 addition & 0 deletions java/test/org/openqa/selenium/remote/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ java_test_suite(
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote",
"//java/src/org/openqa/selenium/support",
"//java/test/org/openqa/selenium:helpers",
"//java/test/org/openqa/selenium/testing:annotations",
artifact("org.assertj:assertj-core"),
artifact("com.google.guava:guava"),
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.remote.internal;
package org.openqa.selenium.remote;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -27,10 +27,10 @@
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WrappedWebElement;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.RemoteWebElement;

class WebElementToJsonConverterTest {

Expand Down Expand Up @@ -218,6 +218,15 @@ void convertsAnArrayWithAWebElement() {
ImmutableMap.of(Dialect.W3C.getEncodedElementKey(), "abc123"));
}

@Test
void shouldConvertShadowRoots() {
ShadowRoot context = new ShadowRoot(createIdleDriver(), "abc123");
Object value = CONVERTER.apply(new Object[] {context});
assertContentsInOrder(
new ArrayList<>((Collection<?>) value),
ImmutableMap.of(Dialect.W3C.getShadowRootElementKey(), "abc123"));
}

private static WrappedWebElement wrapElement(WebElement element) {
return new WrappedWebElement(element);
}
Expand All @@ -235,4 +244,13 @@ private static void assertContentsInOrder(List<?> list, Object... expectedConten
List<Object> expected = asList(expectedContents);
assertThat(list).isEqualTo(expected);
}

private static RemoteWebDriver createIdleDriver() {
return new RemoteWebDriver(cmd -> new Response(), new ImmutableCapabilities()) {
@Override
protected void startSession(Capabilities capabilities) {
// Do nothing
}
};
}
}
21 changes: 1 addition & 20 deletions java/test/org/openqa/selenium/remote/internal/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "JUNIT5_DEPS", "java_library", "java_test_suite")
load("//java:defs.bzl", "JUNIT5_DEPS", "java_library")

java_library(
name = "test-lib",
Expand Down Expand Up @@ -30,22 +30,3 @@ java_library(
artifact("io.netty:netty-transport"),
] + JUNIT5_DEPS,
)

java_test_suite(
name = "SmallTests",
size = "small",
srcs = glob(["*Test.java"]),
tags = [
"no-sandbox",
],
deps = [
":test-lib",
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/remote",
"//java/src/org/openqa/selenium/remote/http",
"//java/test/org/openqa/selenium:helpers",
artifact("org.assertj:assertj-core"),
artifact("com.google.guava:guava"),
artifact("org.junit.jupiter:junit-jupiter-api"),
] + JUNIT5_DEPS,
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.remote.internal.WebElementToJsonConverter;
import org.openqa.selenium.remote.WebElementToJsonConverter;
import org.openqa.selenium.support.ByIdOrName;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
Expand Down
Loading