Skip to content

Commit

Permalink
Merge branch 'trunk' into PEP-484/webdriver-remote-errorhandler
Browse files Browse the repository at this point in the history
  • Loading branch information
AutomatedTester authored Jul 2, 2021
2 parents 1d9e530 + 223a4f6 commit 41201c0
Show file tree
Hide file tree
Showing 46 changed files with 738 additions and 2,023 deletions.
96 changes: 48 additions & 48 deletions dotnet/CHANGELOG

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions java/client/src/org/openqa/selenium/WrapsDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* This interface indicates that the implementing class knows about the driver that contains it and
* can export it.
*/
@FunctionalInterface
public interface WrapsDriver {
/**
* @return The driver that contains this element.
Expand Down
1 change: 1 addition & 0 deletions java/client/src/org/openqa/selenium/WrapsElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
/**
* Indicates that there is an underlying element that can be used
*/
@FunctionalInterface
public interface WrapsElement {
WebElement getWrappedElement();
}
23 changes: 0 additions & 23 deletions java/client/src/org/openqa/selenium/remote/IsRemoteWebDriver.java

This file was deleted.

29 changes: 0 additions & 29 deletions java/client/src/org/openqa/selenium/remote/IsRemoteWebElement.java

This file was deleted.

17 changes: 15 additions & 2 deletions java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,26 @@
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.NoSuchFrameException;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Pdf;
import org.openqa.selenium.Platform;
import org.openqa.selenium.Point;
import org.openqa.selenium.PrintsPage;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.interactions.HasInputDevices;
import org.openqa.selenium.interactions.Interactive;
import org.openqa.selenium.interactions.Keyboard;
import org.openqa.selenium.interactions.Mouse;
import org.openqa.selenium.interactions.Sequence;
Expand All @@ -57,6 +63,7 @@
import org.openqa.selenium.remote.tracing.Tracer;
import org.openqa.selenium.remote.tracing.opentelemetry.OpenTelemetryTracer;
import org.openqa.selenium.virtualauthenticator.Credential;
import org.openqa.selenium.virtualauthenticator.HasVirtualAuthenticator;
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticator;
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticatorOptions;

Expand Down Expand Up @@ -87,7 +94,14 @@
import static org.openqa.selenium.remote.CapabilityType.SUPPORTS_JAVASCRIPT;

@Augmentable
public class RemoteWebDriver implements IsRemoteWebDriver {
public class RemoteWebDriver implements WebDriver,
JavascriptExecutor,
HasInputDevices,
HasCapabilities,
HasVirtualAuthenticator,
Interactive,
PrintsPage,
TakesScreenshot {

// TODO: This static logger should be unified with the per-instance localLogs
private static final Logger logger = Logger.getLogger(RemoteWebDriver.class.getName());
Expand Down Expand Up @@ -218,7 +232,6 @@ public void setFileDetector(FileDetector detector) {
fileDetector = detector;
}

@Override
public SessionId getSessionId() {
return sessionId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
import org.openqa.selenium.Point;
import org.openqa.selenium.Rectangle;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WrapsDriver;
import org.openqa.selenium.WrapsElement;
import org.openqa.selenium.interactions.Coordinates;
import org.openqa.selenium.interactions.Locatable;
import org.openqa.selenium.io.Zip;

import java.io.File;
Expand All @@ -42,7 +45,7 @@
import static org.openqa.selenium.remote.DriverCommand.FIND_CHILD_ELEMENT;
import static org.openqa.selenium.remote.DriverCommand.FIND_CHILD_ELEMENTS;

public class RemoteWebElement implements IsRemoteWebElement {
public class RemoteWebElement implements WebElement, Locatable, TakesScreenshot, WrapsDriver {

private String foundBy;
protected String id;
Expand All @@ -57,7 +60,6 @@ public void setParent(RemoteWebDriver parent) {
this.parent = parent;
}

@Override
public String getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@

package org.openqa.selenium.remote.internal;

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

import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.WrapsElement;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.remote.IsRemoteWebElement;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

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

/**
* Converts {@link RemoteWebElement} objects, which may be
* {@link WrapsElement wrapped}, into their JSON representation as defined by
Expand All @@ -53,10 +51,10 @@ public Object apply(Object arg) {
arg = ((WrapsElement) arg).getWrappedElement();
}

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

if (arg.getClass().isArray()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@
import org.openqa.selenium.Beta;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WrapsDriver;
import org.openqa.selenium.WrapsElement;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticator;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -230,9 +234,11 @@ public Object call(Decorated<?> target, Method method, Object[] args) throws Thr

public void afterCall(Decorated<?> target, Method method, Object[] args, Object res) {}

public Object onError(Decorated<?> target, Method method, Object[] args,
InvocationTargetException e) throws Throwable
{
public Object onError(
Decorated<?> target,
Method method,
Object[] args,
InvocationTargetException e) throws Throwable {
throw e.getTargetException();
}

Expand Down Expand Up @@ -275,6 +281,7 @@ private Object decorateResult(Object toDecorate) {
protected final <Z> Z createProxy(final Decorated<Z> decorated) {
Set<Class<?>> decoratedInterfaces = extractInterfaces(decorated);
Set<Class<?>> originalInterfaces = extractInterfaces(decorated.getOriginal());
Map<Class<?>, InvocationHandler> derivedInterfaces = deriveAdditionalInterfaces(decorated.getOriginal());

final InvocationHandler handler = (proxy, method, args) -> {
try {
Expand All @@ -288,6 +295,10 @@ protected final <Z> Z createProxy(final Decorated<Z> decorated) {
decorated.afterCall(method, result, args);
return result;
}
if (derivedInterfaces.containsKey(method.getDeclaringClass())) {
return derivedInterfaces.get(method.getDeclaringClass()).invoke(proxy, method, args);
}

return method.invoke(decorated.getOriginal(), args);
} catch (InvocationTargetException e) {
return decorated.onError(method, e, args);
Expand All @@ -297,6 +308,7 @@ protected final <Z> Z createProxy(final Decorated<Z> decorated) {
Set<Class<?>> allInterfaces = new HashSet<>();
allInterfaces.addAll(decoratedInterfaces);
allInterfaces.addAll(originalInterfaces);
allInterfaces.addAll(derivedInterfaces.keySet());
Class<?>[] allInterfacesArray = allInterfaces.toArray(new Class<?>[0]);

return (Z) Proxy.newProxyInstance(
Expand Down Expand Up @@ -329,4 +341,47 @@ private static void extractInterfaces(final Set<Class<?>> collector, final Class
}
extractInterfaces(collector, clazz.getSuperclass());
}

private Map<Class<?>, InvocationHandler> deriveAdditionalInterfaces(Object object) {
Map<Class<?>, InvocationHandler> handlers = new HashMap<>();

if (object instanceof WebDriver && !(object instanceof WrapsDriver)) {
handlers.put(WrapsDriver.class, (proxy, method, args) -> {
if ("getWrappedDriver".equals(method.getName())) {
return object;
}
throw new UnsupportedOperationException(method.getName());
});
}

if (object instanceof WebElement && !(object instanceof WrapsElement)) {
handlers.put(WrapsElement.class, (proxy, method, args) -> {
if ("getWrappedElement".equals(method.getName())) {
return object;
}
throw new UnsupportedOperationException(method.getName());
});
}

try {
Method toJson = object.getClass().getDeclaredMethod("toJson");
toJson.setAccessible(true);

handlers.put(JsonSerializer.class, ((proxy, method, args) -> {
if ("toJson".equals(method.getName())) {
return toJson.invoke(object);
}
throw new UnsupportedOperationException(method.getName());
}));
} catch (NoSuchMethodException e) {
// Fine. Just fall through
}

return handlers;
}

@FunctionalInterface
interface JsonSerializer {
Object toJson();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ java_test_suite(
),
deps = [
"//java/client/src/org/openqa/selenium:core",
"//java/client/src/org/openqa/selenium/json",
"//java/client/src/org/openqa/selenium/remote",
"//java/client/src/org/openqa/selenium/support/decorators",
"//java/client/test/org/openqa/selenium/testing:annotations",
Expand Down
Loading

0 comments on commit 41201c0

Please sign in to comment.