How to use getEnhancedProxy method of io.appium.java_client.pagefactory.utils.ProxyFactory class

Best io.appium code snippet using io.appium.java_client.pagefactory.utils.ProxyFactory.getEnhancedProxy

Run io.appium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

LazyLocatorFactory.java

Source: LazyLocatorFactory.java Github

copy
1package com.malski.core.mobile.factory;
2
3import com.malski.core.mobile.control.LazyMobileContext;
4import io.appium.java_client.MobileElement;
5import io.appium.java_client.TouchableElement;
6import io.appium.java_client.android.AndroidDriver;
7import io.appium.java_client.android.AndroidElement;
8import io.appium.java_client.ios.IOSDriver;
9import io.appium.java_client.ios.IOSElement;
10import io.appium.java_client.pagefactory.AppiumFieldDecorator;
11import io.appium.java_client.pagefactory.TimeOutDuration;
12import io.appium.java_client.pagefactory.Widget;
13import io.appium.java_client.pagefactory.locator.CacheableLocator;
14import io.appium.java_client.pagefactory.utils.ProxyFactory;
15import io.appium.java_client.pagefactory.utils.WebDriverUnpackUtility;
16import org.openqa.selenium.SearchContext;
17import org.openqa.selenium.WebDriver;
18import org.openqa.selenium.WebElement;
19import org.openqa.selenium.remote.RemoteWebElement;
20import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;
21import org.openqa.selenium.support.pagefactory.ElementLocator;
22
23import java.lang.reflect.Constructor;
24import java.lang.reflect.Field;
25import java.lang.reflect.ParameterizedType;
26import java.lang.reflect.Type;
27import java.util.*;
28import java.util.concurrent.TimeUnit;
29
30public class LazyLocatorFactory extends AppiumFieldDecorator {
31    private final LazyMobileContext searchContext;
32    public static TimeUnit DEFAULT_TIMEUNIT;
33
34    public LazyLocatorFactory(LazyMobileContext searchContext) {
35        super(searchContext);
36        this.searchContext = searchContext;
37    }
38
39    private static final List<Class<? extends WebElement>> availableElementClasses = new ArrayList() {
40        private static final long serialVersionUID = 1L;
41
42        {
43            this.add(WebElement.class);
44            this.add(RemoteWebElement.class);
45            this.add(MobileElement.class);
46            this.add(TouchableElement.class);
47            this.add(AndroidElement.class);
48            this.add(IOSElement.class);
49        }
50    };
51    private static final Map<Class<? extends SearchContext>, Class<? extends WebElement>> elementRuleMap = new HashMap() {
52        private static final long serialVersionUID = 1L;
53
54        {
55            this.put(AndroidDriver.class, AndroidElement.class);
56            this.put(IOSDriver.class, IOSElement.class);
57        }
58    };
59
60    static {
61        DEFAULT_TIMEUNIT = TimeUnit.SECONDS;
62    }
63
64    public Object decorate(ClassLoader ignored, Field field) {
65        Object result = this.defaultElementFieldDecoracor.decorate(ignored, field);
66        return result != null ? result : this.decorateWidget(field);
67    }
68
69    private Class<?> getTypeForProxy() {
70        Class driverClass = this.originalDriver.getClass();
71        Set rules = elementRuleMap.entrySet();
72        Iterator var4 = rules.iterator();
73
74        while (var4.hasNext()) {
75            Map.Entry e = (Map.Entry) var4.next();
76            if (((Class) e.getKey()).isAssignableFrom(driverClass)) {
77                return (Class) e.getValue();
78            }
79        }
80
81        return RemoteWebElement.class;
82    }
83
84    private WebElement proxyForAnElement(ElementLocator locator) {
85        ElementInterceptor elementInterceptor = new ElementInterceptor(locator, this.originalDriver);
86        return (WebElement) ProxyFactory.getEnhancedProxy(this.getTypeForProxy(), elementInterceptor);
87    }
88}
Full Screen

SapiumFieldDecorator.java

Source: SapiumFieldDecorator.java Github

copy
1package com.sapium.pagefactory;
2
3import static io.appium.java_client.pagefactory.utils.ProxyFactory.getEnhancedProxy;
4import io.appium.java_client.MobileElement;
5import io.appium.java_client.TouchableElement;
6import io.appium.java_client.android.AndroidDriver;
7import io.appium.java_client.android.AndroidElement;
8import io.appium.java_client.ios.IOSDriver;
9import io.appium.java_client.ios.IOSElement;
10import io.appium.java_client.pagefactory.utils.WebDriverUnpackUtility;
11
12import java.lang.reflect.Field;
13import java.lang.reflect.ParameterizedType;
14import java.lang.reflect.Type;
15import java.util.ArrayList;
16import java.util.HashMap;
17import java.util.List;
18import java.util.Map;
19
20import org.openqa.selenium.SearchContext;
21import org.openqa.selenium.WebDriver;
22import org.openqa.selenium.WebElement;
23import org.openqa.selenium.remote.RemoteWebElement;
24import org.openqa.selenium.support.pagefactory.ElementLocator;
25import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;
26import org.openqa.selenium.support.pagefactory.FieldDecorator;
27
28public class SapiumFieldDecorator implements FieldDecorator {
29	private static final List<Class<? extends WebElement>> availableElementClasses =
30			new ArrayList<Class<? extends WebElement>>() {
31				private static final long serialVersionUID = 1L;
32				{
33					add(WebElement.class);
34					add(RemoteWebElement.class);
35					add(MobileElement.class);
36					add(TouchableElement.class);
37					add(AndroidElement.class);
38					add(IOSElement.class);
39				}
40			};
41			
42	private final static Map<Class<? extends SearchContext>, Class<? extends WebElement>> elementRuleMap =
43			new HashMap<Class<? extends SearchContext>, Class<? extends WebElement>>() {
44				private static final long serialVersionUID = 1L;
45				{
46					put(AndroidDriver.class, AndroidElement.class);
47					put(IOSDriver.class, IOSElement.class);
48				}
49			};
50			
51	private ElementLocatorFactory factory;
52	private final WebDriver originalDriver;
53
54	public SapiumFieldDecorator(SearchContext context, ElementLocatorFactory factory) {
55		this.factory = factory;
56		this.originalDriver = WebDriverUnpackUtility.unpackWebDriverFromSearchContext(context);
57	}
58	
59	@Override
60	public Object decorate(ClassLoader loader, Field field) {
61		if (!(WebElement.class.isAssignableFrom(field.getType())
62				|| isDecoratableList(field))) {
63			return null;
64		}
65			
66		ElementLocator locator = factory.createLocator(field);
67		
68		if (locator == null) {
69			return null;
70		}
71		
72		if (WebElement.class.isAssignableFrom(field.getType())) {
73			return proxyForLocator(loader, locator);
74		} else if (List.class.isAssignableFrom(field.getType())) {
75			return proxyForListLocator(loader, locator);
76		} else {
77			return null;
78		}
79	}
80
81	private boolean isDecoratableList(Field field) {
82		if (!List.class.isAssignableFrom(field.getType())) {
83			return false;
84		}
85
86		// Type erasure in Java isn't complete. Attempt to discover the generic
87		// type of the list.
88		Type genericType = field.getGenericType();
89		
90		if (!(genericType instanceof ParameterizedType)) {
91			return false;
92		}
93
94		Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0];
95
96		boolean result = false;
97		
98		for (Class<? extends WebElement> webElementClass : availableElementClasses) {
99			if (!webElementClass.equals(listType)) {
100				continue;
101			}
102			
103			result = true;
104			break;
105		}
106		
107		return result;
108	}
109
110	private Object proxyForLocator(ClassLoader loader, ElementLocator locator) {
111		ElementInterceptor elementInterceptor = new ElementInterceptor(locator, originalDriver);
112        return (WebElement) getEnhancedProxy(getTypeForProxy(), elementInterceptor);
113	}
114	
115	private Object proxyForListLocator(ClassLoader loader,
116			ElementLocator locator) {
117		ElementListInterceptor elementInterceptor = new ElementListInterceptor(locator);
118		return  getEnhancedProxy(ArrayList.class,
119				elementInterceptor);
120	}
121	
122	private Class<?> getTypeForProxy() {
123		Class<? extends SearchContext> driverClass = originalDriver.getClass();
124		Iterable<Map.Entry<Class<? extends SearchContext>, Class<? extends WebElement>>> rules = elementRuleMap.entrySet();
125		//it will return MobileElement subclass when here is something
126		for (Map.Entry<Class<? extends SearchContext>, Class<? extends WebElement>> e : rules) {
127			//that extends AppiumDriver or MobileElement
128			if (e.getKey().isAssignableFrom(driverClass)) {
129				return e.getValue();
130			}
131		} //it is compatible with desktop browser. So at this case it returns RemoteWebElement.class
132		return RemoteWebElement.class;
133    }
134}
135
Full Screen

AIFieldDecorator.java

Source: AIFieldDecorator.java Github

copy
1package com.globant.aimate.annotation;
2
3import static io.appium.java_client.internal.ElementMap.getElementClass;
4import static io.appium.java_client.pagefactory.utils.ProxyFactory.getEnhancedProxy;
5
6import java.lang.reflect.Field;
7import java.lang.reflect.ParameterizedType;
8import java.lang.reflect.Type;
9import java.lang.reflect.TypeVariable;
10import java.time.Duration;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.List;
14
15import org.openqa.selenium.Capabilities;
16import org.openqa.selenium.HasCapabilities;
17import org.openqa.selenium.SearchContext;
18import org.openqa.selenium.WebDriver;
19import org.openqa.selenium.WebElement;
20import org.openqa.selenium.remote.RemoteWebElement;
21import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;
22import org.openqa.selenium.support.pagefactory.ElementLocator;
23
24import com.google.common.collect.ImmutableList;
25
26import io.appium.java_client.MobileElement;
27import io.appium.java_client.android.AndroidElement;
28import io.appium.java_client.internal.CapabilityHelpers;
29import io.appium.java_client.ios.IOSElement;
30import io.appium.java_client.pagefactory.AppiumFieldDecorator;
31import io.appium.java_client.windows.WindowsElement;
32
33public class AIFieldDecorator extends AppiumFieldDecorator {
34	
35    private static final List<Class<? extends WebElement>> availableElementClasses = ImmutableList.of(WebElement.class,
36            RemoteWebElement.class, MobileElement.class, AndroidElement.class,
37            IOSElement.class, WindowsElement.class);
38	private final DefaultFieldDecorator fieldDecorator;
39	private WebDriver webDriver;
40    private final String platform;
41    private final String automation;
42    
43    public AIFieldDecorator(SearchContext context) {
44        this(context, DEFAULT_WAITING_TIMEOUT);
45    }
46
47    public AIFieldDecorator(SearchContext context, Duration duration) {
48    	super(context, duration);
49    	
50    	this.webDriver = (WebDriver) context;
51    	
52        if (this.webDriver instanceof HasCapabilities) {
53            Capabilities caps = ((HasCapabilities) this.webDriver).getCapabilities();
54            this.platform = CapabilityHelpers.getCapability(caps, "platformName", String.class);
55            this.automation = CapabilityHelpers.getCapability(caps, "automationName", String.class);
56        } else {
57            this.platform = null;
58            this.automation = null;
59        }
60    	
61    	// The magic inside the brackets is overriding proxyForLocator and proxyForListLocator methods of DefaultFieldDecorator class
62		fieldDecorator = new DefaultFieldDecorator(new AIElementLocatorFactory(context))  {
63            @Override
64            protected WebElement proxyForLocator(ClassLoader ignored, ElementLocator locator) {
65                AIElementInterceptor elementInterceptor = new AIElementInterceptor(locator, webDriver);
66                return getEnhancedProxy(getElementClass(platform, automation), elementInterceptor);
67            }
68
69            @Override
70            @SuppressWarnings("unchecked")
71            protected List<WebElement> proxyForListLocator(ClassLoader ignored,
72                                                           ElementLocator locator) {
73                AIElementListInterceptor elementInterceptor = new AIElementListInterceptor(locator);
74                return getEnhancedProxy(ArrayList.class, elementInterceptor);
75            }
76
77            @Override
78            protected boolean isDecoratableList(Field field) {
79                if (!List.class.isAssignableFrom(field.getType())) {
80                    return false;
81                }
82
83                Type genericType = field.getGenericType();
84                if (!(genericType instanceof ParameterizedType)) {
85                    return false;
86                }
87
88                Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0];
89
90                for (Class<? extends WebElement> webElementClass : availableElementClasses) {
91                    if (webElementClass.equals(listType)) {
92                        return true;
93                    }
94                }
95
96                return ((listType instanceof TypeVariable)
97                        && Arrays.asList(((TypeVariable<?>) listType).getBounds())
98                        .stream().anyMatch(item -> availableElementClasses.contains(item)));
99            }
100        };
101    }
102    
103    @Override
104    public Object decorate(ClassLoader ignored, Field field) {
105		if (field.isAnnotationPresent(AIFindBy.class)) {
106			return fieldDecorator.decorate(ignored, field);
107		}
108    	return super.decorate(ignored, field);
109    }
110    
111}
112
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in ProxyFactory

Trigger getEnhancedProxy code on LambdaTest Cloud Grid

Execute automation tests with getEnhancedProxy on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)