|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * See the NOTICE file distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.appium.java_client.proxy; |
| 18 | + |
| 19 | +import net.bytebuddy.matcher.ElementMatchers; |
| 20 | +import org.openqa.selenium.WebDriver; |
| 21 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 22 | +import org.openqa.selenium.remote.RemoteWebElement; |
| 23 | + |
| 24 | +import java.lang.reflect.Method; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.List; |
| 27 | +import java.util.concurrent.Callable; |
| 28 | +import java.util.stream.Collectors; |
| 29 | + |
| 30 | +import static io.appium.java_client.proxy.Helpers.OBJECT_METHOD_NAMES; |
| 31 | +import static io.appium.java_client.proxy.Helpers.createProxy; |
| 32 | +import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; |
| 33 | + |
| 34 | +public class ElementAwareWebDriverListener implements MethodCallListener, ProxyAwareListener { |
| 35 | + private WebDriver parent; |
| 36 | + |
| 37 | + /** |
| 38 | + * Attaches the WebDriver proxy instance to this listener. |
| 39 | + * <p> |
| 40 | + * The listener stores the WebDriver instance to associate it as parent to RemoteWebElement proxies. |
| 41 | + * |
| 42 | + * @param proxy A proxy instance of {@link WebDriver}. |
| 43 | + */ |
| 44 | + @Override |
| 45 | + public void attachProxyInstance(Object proxy) { |
| 46 | + if (proxy instanceof WebDriver) { |
| 47 | + this.parent = (WebDriver) proxy; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Intercepts method calls on a proxied WebDriver. |
| 53 | + * <p> |
| 54 | + * If the result of the method call is a {@link RemoteWebElement}, |
| 55 | + * it is wrapped with a proxy to allow further interception of RemoteWebElement method calls. |
| 56 | + * If the result is a list, each item is checked, and all RemoteWebElements are |
| 57 | + * individually proxied. All other return types are passed through unmodified. |
| 58 | + * Avoid overriding this method, it will alter the behaviour of the listener. |
| 59 | + * |
| 60 | + * @param obj The object on which the method was invoked. |
| 61 | + * @param method The method being invoked. |
| 62 | + * @param args The arguments passed to the method. |
| 63 | + * @param original A {@link Callable} that represents the original method execution. |
| 64 | + * @return The (possibly wrapped) result of the method call. |
| 65 | + * @throws Throwable if the original method or any wrapping logic throws an exception. |
| 66 | + */ |
| 67 | + @Override |
| 68 | + public Object call(Object obj, Method method, Object[] args, Callable<?> original) throws Throwable { |
| 69 | + Object result = original.call(); |
| 70 | + |
| 71 | + if (result instanceof RemoteWebElement) { |
| 72 | + return wrapElement((RemoteWebElement) result); |
| 73 | + } |
| 74 | + |
| 75 | + if (result instanceof List) { |
| 76 | + return ((List<?>) result).stream() |
| 77 | + .map(item -> item instanceof RemoteWebElement ? wrapElement( |
| 78 | + (RemoteWebElement) item) : item) |
| 79 | + .collect(Collectors.toList()); |
| 80 | + } |
| 81 | + |
| 82 | + return result; |
| 83 | + } |
| 84 | + |
| 85 | + private RemoteWebElement wrapElement( |
| 86 | + RemoteWebElement original |
| 87 | + ) { |
| 88 | + RemoteWebElement proxy = createProxy( |
| 89 | + RemoteWebElement.class, |
| 90 | + new Object[]{}, |
| 91 | + new Class[]{}, |
| 92 | + Collections.singletonList(this), |
| 93 | + ElementMatchers.not( |
| 94 | + namedOneOf( |
| 95 | + OBJECT_METHOD_NAMES.toArray(new String[0])) |
| 96 | + .or(ElementMatchers.named("setId").or(ElementMatchers.named("setParent"))) |
| 97 | + ) |
| 98 | + ); |
| 99 | + |
| 100 | + proxy.setId(original.getId()); |
| 101 | + |
| 102 | + proxy.setParent((RemoteWebDriver) parent); |
| 103 | + |
| 104 | + return proxy; |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments