...15 */16package com.mengge.events;17import com.google.common.collect.ImmutableList;18import com.mengge.events.api.Listener;19import org.aspectj.lang.JoinPoint;20import org.aspectj.lang.ProceedingJoinPoint;21import org.aspectj.lang.annotation.After;22import org.aspectj.lang.annotation.Around;23import org.aspectj.lang.annotation.Aspect;24import org.aspectj.lang.annotation.Before;25import org.openqa.selenium.Alert;26import org.openqa.selenium.By;27import org.openqa.selenium.ContextAware;28import org.openqa.selenium.Dimension;29import org.openqa.selenium.Point;30import org.openqa.selenium.ScreenOrientation;31import org.openqa.selenium.WebDriver;32import org.openqa.selenium.WebElement;33import org.openqa.selenium.security.Credentials;34import org.springframework.context.support.AbstractApplicationContext;35import java.lang.reflect.InvocationTargetException;36import java.util.ArrayList;37import java.util.Collection;38import java.util.List;39@Aspect40class DefaultAspect {41 private static final List<Class<?>> listenable = ImmutableList.of(WebDriver.class,42 WebElement.class, WebDriver.Navigation.class, WebDriver.TargetLocator.class,43 ContextAware.class, Alert.class, WebDriver.Options.class, WebDriver.Window.class);44 private static final String EXECUTION_NAVIGATION_TO = "execution(* org.openqa.selenium.WebDriver."45 + "Navigation.get(..)) || "46 + "execution(* org.openqa.selenium.WebDriver.Navigation.to(..)) || "47 + "execution(* org.openqa.selenium.WebDriver.get(..))";48 private static final String EXECUTION_NAVIGATION_BACK = "execution(* org.openqa.selenium.WebDriver."49 + "Navigation.back(..))";50 private static final String EXECUTION_NAVIGATION_FORWARD = "execution(* org.openqa.selenium.WebDriver."51 + "Navigation.forward(..))";52 private static final String EXECUTION_NAVIGATION_REFRESH = "execution(* org.openqa.selenium.WebDriver."53 + "Navigation.refresh(..))";54 private static final String EXECUTION_SEARCH = "execution(* org.openqa.selenium.SearchContext."55 + "findElement(..)) || "56 + "execution(* org.openqa.selenium.SearchContext.findElements(..))";57 private static final String EXECUTION_CLICK = "execution(* org.openqa.selenium.WebElement.click(..))";58 private static final String EXECUTION_CHANGE_VALUE = "execution(* org.openqa.selenium.WebElement."59 + "sendKeys(..)) || "60 + "execution(* org.openqa.selenium.WebElement.clear(..)) || "61 + "execution(* AndroidElement.replaceValue(..)) || "62 + "execution(* MobileElement.setValue(..))";63 private static final String EXECUTION_SCRIPT = "execution(* org.openqa.selenium.JavascriptExecutor."64 + "executeScript(..)) || "65 + "execution(* org.openqa.selenium.JavascriptExecutor.executeAsyncScript(..))";66 private static final String EXECUTION_ALERT_ACCEPT = "execution(* org.openqa.selenium.Alert."67 + "accept(..))";68 private static final String EXECUTION_ALERT_DISMISS = "execution(* org.openqa.selenium.Alert."69 + "dismiss(..))";70 private static final String EXECUTION_ALERT_SEND_KEYS = "execution(* org.openqa.selenium.Alert."71 + "sendKeys(..))";72 private static final String EXECUTION_ALERT_AUTHENTICATION = "execution(* org.openqa.selenium."73 + "Alert.setCredentials(..)) || "74 + "execution(* org.openqa.selenium.Alert.authenticateUsing(..))";75 private static final String EXECUTION_WINDOW_SET_SIZE = "execution(* org.openqa.selenium."76 + "WebDriver.Window.setSize(..))";77 private static final String EXECUTION_WINDOW_SET_POSITION = "execution(* org.openqa.selenium.WebDriver."78 + "Window.setPosition(..))";79 private static final String EXECUTION_WINDOW_MAXIMIZE = "execution(* org.openqa.selenium.WebDriver."80 + "Window.maximize(..))";81 private static final String EXECUTION_ROTATE = "execution(* org.openqa.selenium.Rotatable"82 + ".rotate(..))";83 private static final String EXECUTION_CONTEXT = "execution(* org.openqa.selenium.ContextAware."84 + "context(..))";85 private static final String AROUND = "execution(* org.openqa.selenium.WebDriver.*(..)) || "86 + "execution(* org.openqa.selenium.WebElement.*(..)) || "87 + "execution(* org.openqa.selenium.WebDriver.Navigation.*(..)) || "88 + "execution(* org.openqa.selenium.WebDriver.Options.*(..)) || "89 + "execution(* org.openqa.selenium.WebDriver.TargetLocator.*(..)) || "90 + "execution(* org.openqa.selenium.WebDriver.TargetLocator.*(..)) || "91 + "execution(* org.openqa.selenium.JavascriptExecutor.*(..)) || "92 + "execution(* org.openqa.selenium.ContextAware.*(..)) || "93 + "execution(* FindsByAccessibilityId.*(..)) || "94 + "execution(* FindsByAndroidUIAutomator.*(..)) || "95 + "execution(* FindsByIosUIAutomation.*(..)) || "96 + "execution(* org.openqa.selenium.internal.FindsByClassName.*(..)) || "97 + "execution(* org.openqa.selenium.internal.FindsByCssSelector.*(..)) || "98 + "execution(* org.openqa.selenium.internal.FindsById.*(..)) || "99 + "execution(* org.openqa.selenium.internal.FindsByLinkText.*(..)) || "100 + "execution(* org.openqa.selenium.internal.FindsByName.*(..)) || "101 + "execution(* org.openqa.selenium.internal.FindsByTagName.*(..)) || "102 + "execution(* org.openqa.selenium.internal.FindsByXPath.*(..)) || "103 + "execution(* org.openqa.selenium.WebDriver.Window.*(..)) || "104 + "execution(* AndroidElement.*(..)) || "105 + "execution(* IOSElement.*(..)) || "106 + "execution(* AndroidDriver.*(..)) || "107 + "execution(* IOSDriver.*(..)) || "108 + "execution(* AppiumDriver.*(..)) || "109 + "execution(* MobileElement.*(..)) || "110 + "execution(* org.openqa.selenium.remote.RemoteWebDriver.*(..)) || "111 + "execution(* org.openqa.selenium.remote.RemoteWebElement.*(..)) || "112 + "execution(* org.openqa.selenium.Alert.*(..))";113 private final AbstractApplicationContext context;114 private final WebDriver driver;115 private final DefaultListener listener = new DefaultListener();116 private static Throwable getRootCause(Throwable thrown) {117 Class<? extends Throwable> throwableClass = thrown.getClass();118 if (!InvocationTargetException.class.equals(throwableClass) && !RuntimeException.class.equals(throwableClass)) {119 return thrown;120 }121 if (thrown.getCause() != null) {122 return getRootCause(thrown.getCause());123 }124 return thrown;125 }126 private static Class<?> getClassForProxy(Class<?> classOfObject) {127 Class<?> returnStatement = null;128 for (Class<?> c : listenable) {129 if (!c.isAssignableFrom(classOfObject)) {130 continue;131 }132 returnStatement = c;133 }134 return returnStatement;135 }136 DefaultAspect(AbstractApplicationContext context, WebDriver driver) {137 this.context = context;138 this.driver = driver;139 }140 private Object transformToListenable(Object toBeTransformed) {141 if (toBeTransformed == null) {142 return null;143 }144 Object result = null;145 if (getClassForProxy(toBeTransformed.getClass()) != null) {146 result = context.getBean(DefaultBeanConfiguration.COMPONENT_BEAN, toBeTransformed);147 }148 return result;149 }150 private List<Object> returnProxyList(List<Object> originalList) throws Exception {151 try {152 List<Object> proxyList = new ArrayList<>();153 for (Object o : originalList) {154 if (getClassForProxy(o.getClass()) == null) {155 proxyList.add(o);156 } else {157 proxyList.add(context.getBean(DefaultBeanConfiguration.COMPONENT_BEAN, o));158 }159 }160 return proxyList;161 } catch (Exception e) {162 throw e;163 }164 }165 public void add(Collection<Listener> listeners) {166 listener.add(listeners);167 }168 @Before(EXECUTION_NAVIGATION_TO)169 public void beforeNavigateTo(JoinPoint joinPoint) throws Throwable {170 try {171 listener.beforeNavigateTo(String.valueOf(joinPoint.getArgs()[0]), driver);172 } catch (Throwable t) {173 throw getRootCause(t);174 }175 }176 @After(EXECUTION_NAVIGATION_TO)177 public void afterNavigateTo(JoinPoint joinPoint) throws Throwable {178 try {179 listener.afterNavigateTo(String.valueOf(joinPoint.getArgs()[0]), driver);180 } catch (Throwable t) {181 throw getRootCause(t);182 }183 }184 @Before(EXECUTION_NAVIGATION_BACK)185 public void beforeNavigateBack(JoinPoint joinPoint) throws Throwable {186 try {187 listener.beforeNavigateBack(driver);188 } catch (Throwable t) {189 throw getRootCause(t);190 }191 }192 @After(EXECUTION_NAVIGATION_BACK)193 public void afterNavigateBack(JoinPoint joinPoint) throws Throwable {194 try {195 listener.afterNavigateBack(driver);196 } catch (Throwable t) {197 throw getRootCause(t);198 }199 }200 @Before(EXECUTION_NAVIGATION_FORWARD)201 public void beforeNavigateForward(JoinPoint joinPoint) throws Throwable {202 try {203 listener.beforeNavigateForward(driver);204 } catch (Throwable t) {205 throw getRootCause(t);206 }207 }208 @After(EXECUTION_NAVIGATION_FORWARD)209 public void afterNavigateForward(JoinPoint joinPoint) throws Throwable {210 try {211 listener.afterNavigateForward(driver);212 } catch (Throwable t) {213 throw getRootCause(t);214 }215 }216 @Before(EXECUTION_NAVIGATION_REFRESH)217 public void beforeNavigateRefresh(JoinPoint joinPoint) throws Throwable {218 try {219 listener.beforeNavigateRefresh(driver);220 } catch (Throwable t) {221 throw getRootCause(t);222 }223 }224 @After(EXECUTION_NAVIGATION_REFRESH)225 public void afterNavigateRefresh(JoinPoint joinPoint) throws Throwable {226 try {227 listener.afterNavigateRefresh(driver);228 } catch (Throwable t) {229 throw getRootCause(t);230 }231 }232 @SuppressWarnings("unchecked")233 private <T extends Object> T castArgument(JoinPoint joinPoint, int argIndex) {234 return (T) joinPoint.getArgs()[argIndex];235 }236 @SuppressWarnings("unchecked")237 private <T extends Object> T castTarget(JoinPoint joinPoint) {238 return (T) joinPoint.getTarget();239 }240 @Before(EXECUTION_SEARCH)241 public void beforeFindBy(JoinPoint joinPoint) throws Throwable {242 try {243 Object target = joinPoint.getTarget();244 if (!WebElement.class.isAssignableFrom(target.getClass())) {245 listener.beforeFindBy((By) castArgument(joinPoint, 0), null, driver);246 } else {247 listener.beforeFindBy((By) castArgument(joinPoint, 0),248 (WebElement) castTarget(joinPoint), driver);249 }250 } catch (Throwable t) {251 throw getRootCause(t);252 }253 }254 @After(EXECUTION_SEARCH)255 public void afterFindBy(JoinPoint joinPoint) throws Throwable {256 try {257 Object target = joinPoint.getTarget();258 if (!WebElement.class.isAssignableFrom(target.getClass())) {259 listener.afterFindBy((By) castArgument(joinPoint, 0), null, driver);260 } else {261 listener.afterFindBy((By) castArgument(joinPoint, 0),262 (WebElement) castTarget(joinPoint), driver);263 }264 } catch (Throwable t) {265 throw getRootCause(t);266 }267 }268 @Before(EXECUTION_CLICK)269 public void beforeClickOn(JoinPoint joinPoint) throws Throwable {270 try {271 listener.beforeClickOn((WebElement) castTarget(joinPoint), driver);272 } catch (Throwable t) {273 throw getRootCause(t);274 }275 }276 @After(EXECUTION_CLICK)277 public void afterClickOn(JoinPoint joinPoint) throws Throwable {278 try {279 listener.afterClickOn((WebElement) castTarget(joinPoint), driver);280 } catch (Throwable t) {281 throw getRootCause(t);282 }283 }284 @Before(EXECUTION_CHANGE_VALUE)285 public void beforeChangeValueOf(JoinPoint joinPoint) throws Throwable {286 try {287 listener.beforeChangeValueOf((WebElement) castTarget(joinPoint), driver);288 } catch (Throwable t) {289 throw getRootCause(t);290 }291 }292 @After(EXECUTION_CHANGE_VALUE)293 public void afterChangeValueOf(JoinPoint joinPoint) throws Throwable {294 try {295 listener.afterChangeValueOf((WebElement) castTarget(joinPoint), driver);296 } catch (Throwable t) {297 throw getRootCause(t);298 }299 }300 @Before(EXECUTION_SCRIPT)301 public void beforeScript(JoinPoint joinPoint) throws Throwable {302 try {303 listener.beforeScript(String.valueOf(joinPoint.getArgs()[0]), driver);304 } catch (Throwable t) {305 throw getRootCause(t);306 }307 }308 @After(EXECUTION_SCRIPT)309 public void afterScript(JoinPoint joinPoint) throws Throwable {310 try {311 listener.afterScript(String.valueOf(joinPoint.getArgs()[0]), driver);312 } catch (Throwable t) {313 throw getRootCause(t);314 }315 }316 @Before(EXECUTION_ALERT_ACCEPT)317 public void beforeAlertAccept(JoinPoint joinPoint) throws Throwable {318 try {319 listener.beforeAlertAccept(driver, (Alert) castTarget(joinPoint));320 } catch (Throwable t) {321 throw getRootCause(t);322 }323 }324 @After(EXECUTION_ALERT_ACCEPT)325 public void afterAlertAccept(JoinPoint joinPoint) throws Throwable {326 try {327 listener.afterAlertAccept(driver, (Alert) castTarget(joinPoint));328 } catch (Throwable t) {329 throw getRootCause(t);330 }331 }332 @Before(EXECUTION_ALERT_DISMISS)333 public void beforeAlertDismiss(JoinPoint joinPoint) throws Throwable {334 try {335 listener.beforeAlertDismiss(driver, (Alert) castTarget(joinPoint));336 } catch (Throwable t) {337 throw getRootCause(t);338 }339 }340 @After(EXECUTION_ALERT_DISMISS)341 public void afterAlertDismiss(JoinPoint joinPoint) throws Throwable {342 try {343 listener.afterAlertDismiss(driver, (Alert) castTarget(joinPoint));344 } catch (Throwable t) {345 throw getRootCause(t);346 }347 }348 @Before(EXECUTION_ALERT_SEND_KEYS)349 public void beforeAlertSendKeys(JoinPoint joinPoint) throws Throwable {350 try {351 listener.beforeAlertSendKeys(driver, (Alert) castTarget(joinPoint),352 String.valueOf(joinPoint.getArgs()[0]));353 } catch (Throwable t) {354 throw getRootCause(t);355 }356 }357 @After(EXECUTION_ALERT_SEND_KEYS)358 public void afterAlertSendKeys(JoinPoint joinPoint) throws Throwable {359 try {360 listener.afterAlertSendKeys(driver, (Alert) castTarget(joinPoint),361 String.valueOf(joinPoint.getArgs()[0]));362 } catch (Throwable t) {363 throw getRootCause(t);364 }365 }366 @Before(EXECUTION_ALERT_AUTHENTICATION)367 public void beforeAlertAuthentication(JoinPoint joinPoint) throws Throwable {368 try {369 listener.beforeAuthentication(driver,370 (Alert) castTarget(joinPoint), (Credentials) castArgument(joinPoint, 0));371 } catch (Throwable t) {372 throw getRootCause(t);373 }374 }375 @After(EXECUTION_ALERT_AUTHENTICATION)376 public void afterAlertAuthentication(JoinPoint joinPoint) throws Throwable {377 try {378 listener.afterAuthentication(driver, (Alert) castTarget(joinPoint),379 (Credentials) castArgument(joinPoint, 0));380 } catch (Throwable t) {381 throw getRootCause(t);382 }383 }384 @Before(EXECUTION_WINDOW_SET_SIZE)385 public void beforeWindowIsResized(JoinPoint joinPoint) throws Throwable {386 try {387 listener.beforeWindowChangeSize(driver,388 (WebDriver.Window) castTarget(joinPoint), (Dimension) castArgument(joinPoint, 0));389 } catch (Throwable t) {390 throw getRootCause(t);391 }392 }393 @After(EXECUTION_WINDOW_SET_SIZE)394 public void afterWindowIsResized(JoinPoint joinPoint) throws Throwable {395 try {396 listener.afterWindowChangeSize(driver, (WebDriver.Window) castTarget(joinPoint),397 (Dimension) castArgument(joinPoint, 0));398 } catch (Throwable t) {399 throw getRootCause(t);400 }401 }402 @Before(EXECUTION_WINDOW_SET_POSITION)403 public void beforeWindowIsMoved(JoinPoint joinPoint) throws Throwable {404 try {405 listener.beforeWindowIsMoved(driver, (WebDriver.Window) castTarget(joinPoint),406 (Point) castArgument(joinPoint, 0));407 } catch (Throwable t) {408 throw getRootCause(t);409 }410 }411 @After(EXECUTION_WINDOW_SET_POSITION)412 public void afterWindowIsMoved(JoinPoint joinPoint) throws Throwable {413 try {414 listener.afterWindowIsMoved(driver, (WebDriver.Window) castTarget(joinPoint),415 (Point) castArgument(joinPoint, 0));416 } catch (Throwable t) {417 throw getRootCause(t);418 }419 }420 @Before(EXECUTION_WINDOW_MAXIMIZE)421 public void beforeMaximization(JoinPoint joinPoint) throws Throwable {422 try {423 listener.beforeWindowIsMaximized(driver, (WebDriver.Window) castTarget(joinPoint));424 } catch (Throwable t) {425 throw getRootCause(t);426 }427 }428 @After(EXECUTION_WINDOW_MAXIMIZE)429 public void afterMaximization(JoinPoint joinPoint) throws Throwable {430 try {431 listener.afterWindowIsMaximized(driver, (WebDriver.Window) castTarget(joinPoint));432 } catch (Throwable t) {433 throw getRootCause(t);434 }435 }436 @Before(EXECUTION_ROTATE)437 public void beforeRotation(JoinPoint joinPoint) throws Throwable {438 try {439 listener.beforeRotation(driver, (ScreenOrientation) castArgument(joinPoint, 0));440 } catch (Throwable t) {441 throw getRootCause(t);442 }443 }444 @After(EXECUTION_ROTATE)445 public void afterRotation(JoinPoint joinPoint) throws Throwable {446 try {447 listener.afterRotation(driver, (ScreenOrientation) castArgument(joinPoint, 0));448 } catch (Throwable t) {449 throw getRootCause(t);450 }451 }452 @Before(EXECUTION_CONTEXT)453 public void beforeSwitchingToContext(JoinPoint joinPoint) throws Throwable {454 try {455 listener.beforeSwitchingToContext(driver, String.valueOf(joinPoint.getArgs()[0]));456 } catch (Throwable t) {457 throw getRootCause(t);458 }459 }460 @After(EXECUTION_CONTEXT)461 public void afterSwitchingToContextn(JoinPoint joinPoint) throws Throwable {462 try {463 listener.afterSwitchingToContext(driver, String.valueOf(joinPoint.getArgs()[0]));464 } catch (Throwable t) {465 throw getRootCause(t);466 }467 }468 @Around(AROUND)469 public Object doAround(ProceedingJoinPoint point) throws Throwable {470 Throwable t = null;471 Object result = null;472 try {473 result = point.proceed();474 } catch (Throwable e) {475 t = e;476 }477 if (t != null) {478 Throwable rootCause = getRootCause(t);479 listener.onException(rootCause, driver);480 throw rootCause;481 }482 if (result == null) { // maybe it was "void"483 return result;...