io.appium.java_client.events.DefaultAspect Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-client Show documentation
Show all versions of java-client Show documentation
Java client for Appium Mobile Webdriver
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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 io.appium.java_client.events;
import static io.appium.java_client.events.DefaultBeanConfiguration.COMPONENT_BEAN;
import com.google.common.collect.ImmutableList;
import io.appium.java_client.events.api.Listener;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.openqa.selenium.Alert;
import org.openqa.selenium.ContextAware;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.context.support.AbstractApplicationContext;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@SuppressWarnings("unused")
@Aspect
class DefaultAspect {
private static final List> listenable = ImmutableList.of(WebDriver.class,
WebElement.class, WebDriver.Navigation.class, WebDriver.TargetLocator.class,
ContextAware.class, Alert.class, WebDriver.Options.class, WebDriver.Window.class);
private static final String EXECUTION_NAVIGATION_TO = "execution(* org.openqa.selenium.WebDriver."
+ "Navigation.get(..)) || "
+ "execution(* org.openqa.selenium.WebDriver.Navigation.to(..)) || "
+ "execution(* org.openqa.selenium.WebDriver.get(..))";
private static final String EXECUTION_NAVIGATION_BACK = "execution(* org.openqa.selenium.WebDriver."
+ "Navigation.back(..))";
private static final String EXECUTION_NAVIGATION_FORWARD = "execution(* org.openqa.selenium.WebDriver."
+ "Navigation.forward(..))";
private static final String EXECUTION_NAVIGATION_REFRESH = "execution(* org.openqa.selenium.WebDriver."
+ "Navigation.refresh(..))";
private static final String EXECUTION_SEARCH = "execution(* org.openqa.selenium.SearchContext."
+ "findElement(..)) || "
+ "execution(* org.openqa.selenium.SearchContext.findElements(..))";
private static final String EXECUTION_CLICK = "execution(* org.openqa.selenium.WebElement.click(..))";
private static final String EXECUTION_CHANGE_VALUE = "execution(* org.openqa.selenium.WebElement."
+ "sendKeys(..)) || "
+ "execution(* org.openqa.selenium.WebElement.clear(..)) || "
+ "execution(* io.appium.java_client.android.AndroidElement.replaceValue(..)) || "
+ "execution(* io.appium.java_client.MobileElement.setValue(..))";
private static final String EXECUTION_SCRIPT = "execution(* org.openqa.selenium.JavascriptExecutor."
+ "executeScript(..)) || "
+ "execution(* org.openqa.selenium.JavascriptExecutor.executeAsyncScript(..))";
private static final String EXECUTION_ALERT_ACCEPT = "execution(* org.openqa.selenium.Alert."
+ "accept(..))";
private static final String EXECUTION_ALERT_DISMISS = "execution(* org.openqa.selenium.Alert."
+ "dismiss(..))";
private static final String EXECUTION_ALERT_SEND_KEYS = "execution(* org.openqa.selenium.Alert."
+ "sendKeys(..))";
private static final String EXECUTION_WINDOW_SET_SIZE = "execution(* org.openqa.selenium."
+ "WebDriver.Window.setSize(..))";
private static final String EXECUTION_WINDOW_SET_POSITION = "execution(* org.openqa.selenium.WebDriver."
+ "Window.setPosition(..))";
private static final String EXECUTION_WINDOW_MAXIMIZE = "execution(* org.openqa.selenium.WebDriver."
+ "Window.maximize(..))";
private static final String EXECUTION_ROTATE = "execution(* org.openqa.selenium.Rotatable"
+ ".rotate(..))";
private static final String EXECUTION_CONTEXT = "execution(* org.openqa.selenium.ContextAware."
+ "context(..))";
private static final String EXECUTION_SWITCH_TO_WINDOW = "execution(* org.openqa.selenium.WebDriver.TargetLocator"
+ ".window(..))";
private static final String EXECUTION_TAKE_SCREENSHOT_AS = "execution(* org.openqa.selenium.TakesScreenshot"
+ ".getScreenshotAs(..))";
private static final String AROUND = "execution(* org.openqa.selenium.WebDriver.*(..)) || "
+ "execution(* org.openqa.selenium.WebElement.*(..)) || "
+ "execution(* org.openqa.selenium.WebDriver.Navigation.*(..)) || "
+ "execution(* org.openqa.selenium.WebDriver.Options.*(..)) || "
+ "execution(* org.openqa.selenium.WebDriver.TargetLocator.*(..)) || "
+ "execution(* org.openqa.selenium.WebDriver.TargetLocator.*(..)) || "
+ "execution(* org.openqa.selenium.JavascriptExecutor.*(..)) || "
+ "execution(* org.openqa.selenium.ContextAware.*(..)) || "
+ "execution(* io.appium.java_client.FindsByAccessibilityId.*(..)) || "
+ "execution(* io.appium.java_client.FindsByAndroidUIAutomator.*(..)) || "
+ "execution(* io.appium.java_client.FindsByIosUIAutomation.*(..)) || "
+ "execution(* io.appium.java_client.FindsByWindowsAutomation.*(..)) || "
+ "execution(* io.appium.java_client.FindsByIosNSPredicate.*(..)) || "
+ "execution(* org.openqa.selenium.internal.FindsByClassName.*(..)) || "
+ "execution(* org.openqa.selenium.internal.FindsByCssSelector.*(..)) || "
+ "execution(* org.openqa.selenium.internal.FindsById.*(..)) || "
+ "execution(* org.openqa.selenium.internal.FindsByLinkText.*(..)) || "
+ "execution(* org.openqa.selenium.internal.FindsByName.*(..)) || "
+ "execution(* org.openqa.selenium.internal.FindsByTagName.*(..)) || "
+ "execution(* org.openqa.selenium.internal.FindsByXPath.*(..)) || "
+ "execution(* org.openqa.selenium.WebDriver.Window.*(..)) || "
+ "execution(* io.appium.java_client.android.AndroidElement.*(..)) || "
+ "execution(* io.appium.java_client.ios.IOSElement.*(..)) || "
+ "execution(* io.appium.java_client.android.AndroidDriver.*(..)) || "
+ "execution(* io.appium.java_client.ios.IOSDriver.*(..)) || "
+ "execution(* io.appium.java_client.AppiumDriver.*(..)) || "
+ "execution(* io.appium.java_client.MobileElement.*(..)) || "
+ "execution(* org.openqa.selenium.remote.RemoteWebDriver.*(..)) || "
+ "execution(* org.openqa.selenium.remote.RemoteWebElement.*(..)) || "
+ "execution(* org.openqa.selenium.Alert.*(..)) || "
+ "execution(* org.openqa.selenium.TakesScreenshot.*(..))";
private final AbstractApplicationContext context;
private final WebDriver driver;
private final DefaultListener listener = new DefaultListener();
private static Throwable getRootCause(Throwable thrown) {
Class extends Throwable> throwableClass = thrown.getClass();
if (!InvocationTargetException.class.equals(throwableClass) && !RuntimeException.class.equals(throwableClass)) {
return thrown;
}
if (thrown.getCause() != null) {
return getRootCause(thrown.getCause());
}
return thrown;
}
private static Class> getClassForProxy(Class> classOfObject) {
Class> returnStatement = null;
for (Class> c : listenable) {
if (!c.isAssignableFrom(classOfObject)) {
continue;
}
returnStatement = c;
}
return returnStatement;
}
DefaultAspect(AbstractApplicationContext context, WebDriver driver) {
this.context = context;
this.driver = driver;
}
private Object transformToListenable(Object toBeTransformed) {
if (toBeTransformed == null) {
return null;
}
Object result = toBeTransformed;
if (getClassForProxy(toBeTransformed.getClass()) != null) {
result = context.getBean(COMPONENT_BEAN, toBeTransformed);
}
return result;
}
private List