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

EventFiring objects. The addition to event firing WebDriver #559

Merged
merged 4 commits into from
Jan 29, 2017
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
13 changes: 7 additions & 6 deletions src/main/java/io/appium/java_client/events/DefaultAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.appium.java_client.events;

import static io.appium.java_client.events.DefaultBeanConfiguration.COMPONENT_BEAN;

import com.google.common.collect.ImmutableList;

Expand Down Expand Up @@ -96,15 +97,15 @@ class DefaultAspect {
+ "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(* io.appium.java_client.FindsByFluentSelector.*(..)) || "
+ "execution(* io.appium.java_client.FindsByWindowsAutomation.*(..)) || "
+ "execution(* org.openqa.selenium.WebDriver.Window.*(..)) || "
+ "execution(* io.appium.java_client.android.AndroidElement.*(..)) || "
+ "execution(* io.appium.java_client.ios.IOSElement.*(..)) || "
Expand Down Expand Up @@ -155,7 +156,7 @@ private Object transformToListenable(Object toBeTransformed) {

Object result = toBeTransformed;
if (getClassForProxy(toBeTransformed.getClass()) != null) {
result = context.getBean(DefaultBeanConfiguration.COMPONENT_BEAN, toBeTransformed);
result = context.getBean(COMPONENT_BEAN, toBeTransformed);
}
return result;
}
Expand All @@ -167,7 +168,7 @@ private List<Object> returnProxyList(List<Object> originalList) throws Exception
if (getClassForProxy(o.getClass()) == null) {
proxyList.add(o);
} else {
proxyList.add(context.getBean(DefaultBeanConfiguration.COMPONENT_BEAN, o));
proxyList.add(context.getBean(COMPONENT_BEAN, o));
}
}
return proxyList;
Expand Down Expand Up @@ -254,12 +255,12 @@ public void afterNavigateRefresh(JoinPoint joinPoint) throws Throwable {
}

@SuppressWarnings("unchecked")
private <T extends Object> T castArgument(JoinPoint joinPoint, int argIndex) {
private <T> T castArgument(JoinPoint joinPoint, int argIndex) {
return (T) joinPoint.getArgs()[argIndex];
}

@SuppressWarnings("unchecked")
private <T extends Object> T castTarget(JoinPoint joinPoint) {
private <T> T castTarget(JoinPoint joinPoint) {
return (T) joinPoint.getTarget();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,34 @@
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
class DefaultBeanConfiguration {

public static final String LISTENABLE_OBJECT = "object";
public static final String COMPONENT_BEAN = "component";
public static final String WEB_DRIVER_BEAN = "webdriver";

private final List<Listener> listeners = new ArrayList<>();
private WebDriver driver;
private AbstractApplicationContext context;
protected final List<Listener> listeners = new ArrayList<>();
protected WebDriver driver;
protected AbstractApplicationContext context;

@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Bean(name = WEB_DRIVER_BEAN)
public <T extends WebDriver> T getListenableWebdriver(T driver, List<Listener> listeners,
@Bean(name = LISTENABLE_OBJECT)
public <T> T init(T t, WebDriver driver, List<Listener> listeners,
AbstractApplicationContext context) {
this.driver = driver;
this.listeners.addAll(listeners);
this.context = context;
return driver;
return t;
}

@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Bean(name = COMPONENT_BEAN)
public <T> T getComponent(T t) {
return t;
}

@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Bean(name = "webdriverAspect")
@Bean(name = "defaultAspect")
public DefaultAspect getAspect() {
DefaultAspect aspect = new DefaultAspect(context, driver);
aspect.add(listeners);
return aspect;
}

@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Bean(name = COMPONENT_BEAN)
public Object getComponent(Object component) {
return component;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.appium.java_client.events;


import io.appium.java_client.events.api.Listener;
import org.openqa.selenium.WebDriver;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.ServiceLoader;

public class EventFiringObjectFactory {

/**
* This method makes an event firing object
*
* @param t an original {@link Object} that is
* supposed to be listenable
* @param driver an instance of {@link org.openqa.selenium.WebDriver}
* @param listeners is a collection of {@link io.appium.java_client.events.api.Listener} that
* is supposed to be used for the event firing
* @param <T> T
* @return an {@link Object} that fires events
*/
@SuppressWarnings("unchecked")
public static <T> T getEventFiringObject(T t, WebDriver driver, Collection<Listener> listeners) {
final List<Listener> listenerList = new ArrayList<>();

for (Listener listener : ServiceLoader.load(
Listener.class)) {
listenerList.add(listener);
}

listeners.stream().filter(listener -> !listenerList.contains(listener)).forEach(listenerList::add);

AbstractApplicationContext context = new AnnotationConfigApplicationContext(
DefaultBeanConfiguration.class);
return (T) context.getBean(
DefaultBeanConfiguration.LISTENABLE_OBJECT, t, driver, listenerList, context);
}

/**
* This method makes an event firing object
*
* @param t an original {@link Object} that is
* supposed to be listenable
* @param driver an instance of {@link org.openqa.selenium.WebDriver}
* @param <T> T
* @return an {@link Object} that fires events
*/
public static <T> T getEventFiringObject(T t, WebDriver driver) {
return getEventFiringObject(t, driver, Collections.<Listener>emptyList());
}

/**
* This method makes an event firing object
*
* @param t an original {@link Object} that is
* supposed to be listenable
* @param driver an instance of {@link org.openqa.selenium.WebDriver}
* @param listeners is an array of {@link io.appium.java_client.events.api.Listener} that
* is supposed to be used for the event firing
*
* @param <T> T
* @return an instance of {@link org.openqa.selenium.WebDriver} that fires events
*/
public static <T> T getEventFiringObject(T t, WebDriver driver, Listener ... listeners) {
return getEventFiringObject(t, driver, Arrays.asList(listeners));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,12 @@

package io.appium.java_client.events;

import static io.appium.java_client.events.EventFiringObjectFactory.getEventFiringObject;

import io.appium.java_client.events.api.Listener;
import org.openqa.selenium.WebDriver;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;

public class EventFiringWebDriverFactory {

Expand All @@ -41,7 +34,7 @@ public class EventFiringWebDriverFactory {
* @return an instance of {@link org.openqa.selenium.WebDriver} that fires events
*/
public static <T extends WebDriver> T getEventFiringWebDriver(T driver) {
return getEventFiringWebDriver(driver, Collections.<Listener>emptyList());
return getEventFiringObject(driver, driver);
}

/**
Expand All @@ -55,7 +48,7 @@ public static <T extends WebDriver> T getEventFiringWebDriver(T driver) {
* @return an instance of {@link org.openqa.selenium.WebDriver} that fires events
*/
public static <T extends WebDriver> T getEventFiringWebDriver(T driver, Listener ... listeners) {
return getEventFiringWebDriver(driver, Arrays.asList(listeners));
return getEventFiringObject(driver, driver, listeners);
}

/**
Expand All @@ -68,21 +61,7 @@ public static <T extends WebDriver> T getEventFiringWebDriver(T driver, Listener
* @param <T> T
* @return an instance of {@link org.openqa.selenium.WebDriver} that fires events
*/
@SuppressWarnings("unchecked")
public static <T extends WebDriver> T getEventFiringWebDriver(T driver, Collection<Listener> listeners) {
List<Listener> listenerList = new ArrayList<>();
Iterator<Listener> providers = ServiceLoader.load(
Listener.class).iterator();

while (providers.hasNext()) {
listenerList.add(providers.next());
}

listenerList.addAll(listeners);

AbstractApplicationContext context = new AnnotationConfigApplicationContext(
DefaultBeanConfiguration.class);
return (T) context.getBean(
DefaultBeanConfiguration.WEB_DRIVER_BEAN, driver, listenerList, context);
return getEventFiringObject(driver, driver, listeners);
}
}
Loading