Search in sources :

Example 1 with CacheableLocator

use of io.appium.java_client.pagefactory.locator.CacheableLocator in project java-client by appium.

the class AppiumElementLocatorFactory method createLocator.

@Override
@Nullable
public CacheableLocator createLocator(AnnotatedElement annotatedElement) {
    TimeOutDuration customDuration;
    if (annotatedElement.isAnnotationPresent(WithTimeout.class)) {
        WithTimeout withTimeout = annotatedElement.getAnnotation(WithTimeout.class);
        customDuration = new TimeOutDuration(withTimeout.time(), withTimeout.unit());
    } else {
        customDuration = duration;
    }
    builder.setAnnotated(annotatedElement);
    By byResult = builder.buildBy();
    return ofNullable(byResult).map(by -> new AppiumElementLocator(searchContext, by, builder.isLookupCached(), customDuration)).orElse(null);
}
Also used : CacheableLocator(io.appium.java_client.pagefactory.locator.CacheableLocator) SearchContext(org.openqa.selenium.SearchContext) CacheableElementLocatorFactory(io.appium.java_client.pagefactory.locator.CacheableElementLocatorFactory) Optional.ofNullable(java.util.Optional.ofNullable) By(org.openqa.selenium.By) Field(java.lang.reflect.Field) Nullable(javax.annotation.Nullable) AppiumByBuilder(io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder) AnnotatedElement(java.lang.reflect.AnnotatedElement) By(org.openqa.selenium.By) Optional.ofNullable(java.util.Optional.ofNullable) Nullable(javax.annotation.Nullable)

Example 2 with CacheableLocator

use of io.appium.java_client.pagefactory.locator.CacheableLocator in project java-client by appium.

the class AppiumFieldDecorator method decorateWidget.

@SuppressWarnings("unchecked")
private Object decorateWidget(Field field) {
    Class<?> type = field.getType();
    if (!Widget.class.isAssignableFrom(type) && !List.class.isAssignableFrom(type)) {
        return null;
    }
    Class<? extends Widget> widgetType;
    boolean isAlist = false;
    if (List.class.isAssignableFrom(type)) {
        isAlist = true;
        Type genericType = field.getGenericType();
        if (!(genericType instanceof ParameterizedType)) {
            return null;
        }
        Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0];
        if (ParameterizedType.class.isAssignableFrom(listType.getClass())) {
            listType = ((ParameterizedType) listType).getRawType();
        }
        if (listType instanceof Class) {
            if (!Widget.class.isAssignableFrom((Class) listType)) {
                return null;
            }
            widgetType = Class.class.cast(listType);
        } else {
            return null;
        }
    } else {
        widgetType = (Class<? extends Widget>) field.getType();
    }
    CacheableLocator locator = widgetLocatorFactory.createLocator(field);
    Map<ContentType, Constructor<? extends Widget>> map = OverrideWidgetReader.read(widgetType, field, platform, automation);
    if (isAlist) {
        return getEnhancedProxy(ArrayList.class, new WidgetListInterceptor(locator, webDriver, map, widgetType, duration));
    }
    Constructor<? extends Widget> constructor = WidgetConstructorUtil.findConvenientConstructor(widgetType);
    return getEnhancedProxy(widgetType, new Class[] { constructor.getParameterTypes()[0] }, new Object[] { proxyForAnElement(locator) }, new WidgetInterceptor(locator, webDriver, null, map, duration));
}
Also used : ContentType(io.appium.java_client.pagefactory.bys.ContentType) Constructor(java.lang.reflect.Constructor) ParameterizedType(java.lang.reflect.ParameterizedType) ContentType(io.appium.java_client.pagefactory.bys.ContentType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ElementMap.getElementClass(io.appium.java_client.internal.ElementMap.getElementClass) CacheableLocator(io.appium.java_client.pagefactory.locator.CacheableLocator)

Example 3 with CacheableLocator

use of io.appium.java_client.pagefactory.locator.CacheableLocator in project java-client by appium.

the class WidgetListInterceptor method getObject.

@Override
protected Object getObject(List<WebElement> elements, Method method, Object[] args) throws Throwable {
    if (cachedElements == null || (locator != null && !((CacheableLocator) locator).isLookUpCached())) {
        cachedElements = elements;
        cachedWidgets.clear();
        ContentType type = null;
        for (WebElement element : cachedElements) {
            type = ofNullable(type).orElseGet(() -> getCurrentContentType(element));
            Class<?>[] params = new Class<?>[] { instantiationMap.get(type).getParameterTypes()[0] };
            cachedWidgets.add(ProxyFactory.getEnhancedProxy(declaredType, params, new Object[] { element }, new WidgetInterceptor(null, driver, element, instantiationMap, duration)));
        }
    }
    try {
        return method.invoke(cachedWidgets, args);
    } catch (Throwable t) {
        throw extractReadableException(t);
    }
}
Also used : WebDriverUnpackUtility.getCurrentContentType(io.appium.java_client.pagefactory.utils.WebDriverUnpackUtility.getCurrentContentType) ContentType(io.appium.java_client.pagefactory.bys.ContentType) WebElement(org.openqa.selenium.WebElement) CacheableLocator(io.appium.java_client.pagefactory.locator.CacheableLocator)

Aggregations

CacheableLocator (io.appium.java_client.pagefactory.locator.CacheableLocator)3 ContentType (io.appium.java_client.pagefactory.bys.ContentType)2 ElementMap.getElementClass (io.appium.java_client.internal.ElementMap.getElementClass)1 AppiumByBuilder (io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder)1 CacheableElementLocatorFactory (io.appium.java_client.pagefactory.locator.CacheableElementLocatorFactory)1 WebDriverUnpackUtility.getCurrentContentType (io.appium.java_client.pagefactory.utils.WebDriverUnpackUtility.getCurrentContentType)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Nullable (javax.annotation.Nullable)1 By (org.openqa.selenium.By)1 SearchContext (org.openqa.selenium.SearchContext)1 WebElement (org.openqa.selenium.WebElement)1