How to use isLookupCached method of io.appium.java_client.pagefactory.DefaultElementByBuilder class

Best io.appium code snippet using io.appium.java_client.pagefactory.DefaultElementByBuilder.isLookupCached

Run io.appium automation tests on LambdaTest cloud grid

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

InjectionAnnotations.java

Source: InjectionAnnotations.java Github

copy
1package org.fluentlenium.core.inject;
2
3import static io.appium.java_client.remote.AutomationName.IOS_XCUI_TEST;
4import static io.appium.java_client.remote.MobilePlatform.ANDROID;
5import static io.appium.java_client.remote.MobilePlatform.IOS;
6import static io.appium.java_client.remote.MobilePlatform.WINDOWS;
7import static java.util.Optional.ofNullable;
8import static org.fluentlenium.utils.CollectionUtils.isList;
9
10import io.appium.java_client.pagefactory.DefaultElementByBuilder;
11import org.fluentlenium.configuration.ConfigurationException;
12import org.fluentlenium.core.label.FluentLabelProvider;
13import org.fluentlenium.core.page.ClassAnnotations;
14import org.fluentlenium.utils.ReflectionUtils;
15import org.openqa.selenium.By;
16import org.openqa.selenium.Capabilities;
17import org.openqa.selenium.support.ByIdOrName;
18import org.openqa.selenium.support.pagefactory.AbstractAnnotations;
19import org.openqa.selenium.support.pagefactory.Annotations;
20
21import java.lang.annotation.Annotation;
22import java.lang.reflect.Field;
23import java.util.Arrays;
24
25/**
26 * Inspired by {@link org.openqa.selenium.support.pagefactory.Annotations}, but also supports annotations defined on
27 * return type class.
28 */
29public class InjectionAnnotations extends AbstractAnnotations implements FluentLabelProvider {
30    private final ClassAnnotations classAnnotations;
31    private final Annotations fieldAnnotations;
32    private final LabelAnnotations labelFieldAnnotations;
33    private final DefaultElementByBuilder defaultElementByBuilder;
34    private final boolean mobileElement;
35
36    /**
37     * Creates a new injection annotations object
38     *
39     * @param field        field to analyze
40     * @param capabilities Selenium capabilities
41     */
42    public InjectionAnnotations(Field field, Capabilities capabilities) {
43        classAnnotations = new ClassAnnotations(getEffectiveClass(field));
44        fieldAnnotations = new Annotations(field);
45        labelFieldAnnotations = new LabelAnnotations(field);
46        String platform = getPlatform(capabilities);
47        String automation = getAutomation(capabilities);
48        defaultElementByBuilder = new DefaultElementByBuilder(platform, automation);
49        if (isAnnotatedWithSupportedMobileBy(field)) {
50            checkCapabilities(platform, automation);
51            defaultElementByBuilder.setAnnotated(field);
52            mobileElement = true;
53        } else {
54            mobileElement = false;
55        }
56    }
57
58    private void checkCapabilities(String platform, String automation) {
59        boolean correctConfiguration = isAndroid(platform) || isIos(platform, automation) || isWindows(platform);
60        if (!correctConfiguration) {
61            throw new ConfigurationException("You have annotated elements with Appium @FindBys"
62                    + " but capabilities are incomplete. Please use one of these configurations:\n"
63                    + "platformName:Windows\n"
64                    + "plaformName:Android\n"
65                    + "plaformName:iOS, automationName:XCUITest");
66        }
67    }
68
69    private boolean isWindows(String platform) {
70        return WINDOWS.equalsIgnoreCase(platform);
71    }
72
73    private boolean isIos(String platform, String automation) {
74        return IOS.equalsIgnoreCase(platform) && IOS_XCUI_TEST.equalsIgnoreCase(automation);
75    }
76
77    private boolean isAndroid(String platform) {
78        return ANDROID.equalsIgnoreCase(platform);
79    }
80
81    private String getAutomation(Capabilities capabilities) {
82        return ofNullable(capabilities)
83                .map(capability -> capability.getCapability("automationName"))
84                .map(String::valueOf)
85                .orElse(null);
86    }
87
88    private String getPlatform(Capabilities capabilities) {
89        if (capabilities == null) {
90            return null;
91        }
92
93        Object platformName = ofNullable(capabilities.getCapability("platformName"))
94                .orElseGet(() -> capabilities.getCapability("platform"));
95        return ofNullable(platformName).map(String::valueOf).orElse(null);
96    }
97
98    private boolean isAnnotatedWithSupportedMobileBy(Field field) {
99        Annotation[] annotations = field.getAnnotations();
100        return Arrays.stream(annotations)
101                .anyMatch(SupportedAppiumAnnotations::isSupported);
102    }
103
104    @Override
105    public By buildBy() {
106        if (mobileElement) {
107            return defaultElementByBuilder.buildBy();
108        }
109        By fieldBy = fieldAnnotations.buildBy();
110        By classBy = classAnnotations.buildBy();
111
112        if (classBy != null && fieldBy instanceof ByIdOrName) {
113            return classBy;
114        }
115        return fieldBy;
116    }
117
118    @Override
119    public boolean isLookupCached() {
120        return classAnnotations.isLookupCached() || fieldAnnotations.isLookupCached();
121    }
122
123    @Override
124    public String getLabel() {
125        return labelFieldAnnotations.getLabel();
126    }
127
128    @Override
129    public String[] getLabelHints() {
130        return labelFieldAnnotations.getLabelHints();
131    }
132
133    private static Class<?> getEffectiveClass(Field field) {
134        if (isList(field)) {
135            Class<?> effectiveClass = ReflectionUtils.getFirstGenericType(field);
136            if (effectiveClass != null) {
137                return effectiveClass;
138            }
139        }
140        return field.getType();
141    }
142}
143
Full Screen

DefaultElementByBuilder.java

Source: DefaultElementByBuilder.java Github

copy
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * See the NOTICE file distributed with this work for additional
5 * information regarding copyright ownership.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.dsc.test.app.pagefactory.appium;
18
19import io.appium.java_client.pagefactory.AndroidFindAll;
20import io.appium.java_client.pagefactory.AndroidFindBy;
21import io.appium.java_client.pagefactory.AndroidFindBys;
22import io.appium.java_client.pagefactory.HowToUseLocators;
23import io.appium.java_client.pagefactory.LocatorGroupStrategy;
24import io.appium.java_client.pagefactory.SelendroidFindAll;
25import io.appium.java_client.pagefactory.SelendroidFindBy;
26import io.appium.java_client.pagefactory.SelendroidFindBys;
27import io.appium.java_client.pagefactory.WindowsFindBy;
28import io.appium.java_client.pagefactory.iOSFindAll;
29import io.appium.java_client.pagefactory.iOSFindBy;
30import io.appium.java_client.pagefactory.iOSFindBys;
31import io.appium.java_client.pagefactory.iOSXCUITFindBy;
32import io.appium.java_client.pagefactory.bys.ContentMappedBy;
33import io.appium.java_client.pagefactory.bys.ContentType;
34import io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder;
35import io.appium.java_client.pagefactory.bys.builder.HowToUseSelectors;
36import org.openqa.selenium.By;
37import org.openqa.selenium.support.ByIdOrName;
38import org.openqa.selenium.support.CacheLookup;
39import org.openqa.selenium.support.FindAll;
40import org.openqa.selenium.support.FindBy;
41import org.openqa.selenium.support.FindBys;
42
43import java.lang.annotation.Annotation;
44import java.lang.reflect.AnnotatedElement;
45import java.lang.reflect.Field;
46import java.util.HashMap;
47import java.util.Map;
48import java.util.Optional;
49
50class DefaultElementByBuilder extends AppiumByBuilder {
51
52    protected DefaultElementByBuilder(String platform, String automation) {
53        super(platform, automation);
54    }
55
56    private static void checkDisallowedAnnotationPairs(Annotation a1, Annotation a2)
57        throws IllegalArgumentException {
58        if (a1 != null && a2 != null) {
59            throw new IllegalArgumentException(
60                "If you use a '@" + a1.getClass().getSimpleName() + "' annotation, "
61                    + "you must not also use a '@" + a2.getClass().getSimpleName()
62                    + "' annotation");
63        }
64    }
65
66    private static By buildMobileBy(LocatorGroupStrategy locatorGroupStrategy, Annotation[] annotations) {
67        if (annotations.length == 1) {
68            return createBy(new Annotation[] {annotations[0]}, HowToUseSelectors.USE_ONE);
69        } else {
70            LocatorGroupStrategy strategy = Optional.ofNullable(locatorGroupStrategy)
71                    .orElse(LocatorGroupStrategy.CHAIN);
72            if (strategy.equals(LocatorGroupStrategy.ALL_POSSIBLE)) {
73                return createBy(annotations, HowToUseSelectors.USE_ANY);
74            }
75            return createBy(annotations, HowToUseSelectors.BUILD_CHAINED);
76        }
77    }
78
79    @Override protected void assertValidAnnotations() {
80        AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
81        AndroidFindBy androidBy = annotatedElement.getAnnotation(AndroidFindBy.class);
82        AndroidFindBys androidBys = annotatedElement.getAnnotation(AndroidFindBys.class);
83        checkDisallowedAnnotationPairs(androidBy, androidBys);
84        AndroidFindAll androidFindAll = annotatedElement.getAnnotation(AndroidFindAll.class);
85        checkDisallowedAnnotationPairs(androidBy, androidFindAll);
86        checkDisallowedAnnotationPairs(androidBys, androidFindAll);
87
88        SelendroidFindBy selendroidBy = annotatedElement.getAnnotation(SelendroidFindBy.class);
89        SelendroidFindBys selendroidBys = annotatedElement.getAnnotation(SelendroidFindBys.class);
90        checkDisallowedAnnotationPairs(selendroidBy, selendroidBys);
91        SelendroidFindAll selendroidFindAll =
92            annotatedElement.getAnnotation(SelendroidFindAll.class);
93        checkDisallowedAnnotationPairs(selendroidBy, selendroidFindAll);
94        checkDisallowedAnnotationPairs(selendroidBys, selendroidFindAll);
95
96        iOSFindBy iOSBy = annotatedElement.getAnnotation(iOSFindBy.class);
97        iOSFindBys iOSBys = annotatedElement.getAnnotation(iOSFindBys.class);
98        checkDisallowedAnnotationPairs(iOSBy, iOSBys);
99        iOSFindAll iOSFindAll = annotatedElement.getAnnotation(iOSFindAll.class);
100        checkDisallowedAnnotationPairs(iOSBy, iOSFindAll);
101        checkDisallowedAnnotationPairs(iOSBys, iOSFindAll);
102
103        FindBy findBy = annotatedElement.getAnnotation(FindBy.class);
104        FindBys findBys = annotatedElement.getAnnotation(FindBys.class);
105        checkDisallowedAnnotationPairs(findBy, findBys);
106        FindAll findAll = annotatedElement.getAnnotation(FindAll.class);
107        checkDisallowedAnnotationPairs(findBy, findAll);
108        checkDisallowedAnnotationPairs(findBys, findAll);
109    }
110
111    @Override protected By buildDefaultBy() {
112        AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
113        By defaultBy = null;
114        FindBy findBy = annotatedElement.getAnnotation(FindBy.class);
115        if (findBy != null) {
116            defaultBy = super.buildByFromFindBy(findBy);
117        }
118
119        if (defaultBy == null) {
120            FindBys findBys = annotatedElement.getAnnotation(FindBys.class);
121            if (findBys != null) {
122                defaultBy = super.buildByFromFindBys(findBys);
123            }
124        }
125
126        if (defaultBy == null) {
127            FindAll findAll = annotatedElement.getAnnotation(FindAll.class);
128            if (findAll != null) {
129                defaultBy = super.buildBysFromFindByOneOf(findAll);
130            }
131        }
132        return defaultBy;
133    }
134
135    @Override protected By buildMobileNativeBy() {
136        AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
137        HowToUseLocators howToUseLocators = annotatedElement.getAnnotation(HowToUseLocators.class);
138
139        if (isSelendroidAutomation()) {
140            SelendroidFindBy[] selendroidFindByArray =
141                annotatedElement.getAnnotationsByType(SelendroidFindBy.class);
142            //should be kept for some time
143            SelendroidFindBys selendroidFindBys =
144                annotatedElement.getAnnotation(SelendroidFindBys.class);
145            SelendroidFindAll selendroidFindByAll =
146                annotatedElement.getAnnotation(SelendroidFindAll.class);
147
148            if (selendroidFindByArray != null && selendroidFindByArray.length == 1) {
149                return createBy(new Annotation[] {selendroidFindByArray[0]}, HowToUseSelectors.USE_ONE);
150            }
151
152            if (selendroidFindBys != null) {
153                return createBy(selendroidFindBys.value(), HowToUseSelectors.BUILD_CHAINED);
154            }
155
156            if (selendroidFindByAll != null) {
157                return createBy(selendroidFindByAll.value(), HowToUseSelectors.USE_ANY);
158            }
159            ///////////////////////////////////////
160            //code that supposed to be supported
161            if (selendroidFindByArray != null && selendroidFindByArray.length > 0) {
162                return buildMobileBy(howToUseLocators != null ? howToUseLocators.selendroidAutomation() : null,
163                        selendroidFindByArray);
164            }
165        }
166
167        if (isAndroid()) {
168            AndroidFindBy[] androidFindByArray = annotatedElement.getAnnotationsByType(AndroidFindBy.class);
169            //should be kept for some time
170            AndroidFindBys androidFindBys = annotatedElement.getAnnotation(AndroidFindBys.class);
171            AndroidFindAll androidFindAll = annotatedElement.getAnnotation(AndroidFindAll.class);
172
173            if (androidFindByArray != null && androidFindByArray.length == 1) {
174                return createBy(new Annotation[] {androidFindByArray[0]}, HowToUseSelectors.USE_ONE);
175            }
176
177            if (androidFindBys != null) {
178                return createBy(androidFindBys.value(), HowToUseSelectors.BUILD_CHAINED);
179            }
180
181            if (androidFindAll != null) {
182                return createBy(androidFindAll.value(), HowToUseSelectors.USE_ANY);
183            }
184            ///////////////////////////////////////
185            //code that supposed to be supported
186            if (androidFindByArray != null && androidFindByArray.length > 0) {
187                return buildMobileBy(howToUseLocators != null ? howToUseLocators.androidAutomation() : null,
188                        androidFindByArray);
189            }
190        }
191
192        if (isIOSXcuit()) {
193            iOSXCUITFindBy[] xCuitFindByArray = annotatedElement.getAnnotationsByType(iOSXCUITFindBy.class);
194            if (xCuitFindByArray != null && xCuitFindByArray.length > 0) {
195                return buildMobileBy(howToUseLocators != null ? howToUseLocators.iOSXCUITAutomation() : null,
196                        xCuitFindByArray);
197            }
198        }
199
200        if (isIOS()) {
201            iOSFindBy[] iOSFindByArray = annotatedElement.getAnnotationsByType(iOSFindBy.class);
202            //should be kept for some time
203            iOSFindBys iOSFindBys = annotatedElement.getAnnotation(iOSFindBys.class);
204            iOSFindAll iOSFindAll = annotatedElement.getAnnotation(iOSFindAll.class);
205
206            if (iOSFindByArray != null && iOSFindByArray.length == 1) {
207                return createBy(new Annotation[] {iOSFindByArray[0]}, HowToUseSelectors.USE_ONE);
208            }
209
210            if (iOSFindBys != null) {
211                return createBy(iOSFindBys.value(), HowToUseSelectors.BUILD_CHAINED);
212            }
213
214            if (iOSFindAll != null) {
215                return createBy(iOSFindAll.value(), HowToUseSelectors.USE_ANY);
216            }
217            ///////////////////////////////////////
218            //code that supposed to be supported
219            if (iOSFindByArray != null && iOSFindByArray.length > 0) {
220                return buildMobileBy(howToUseLocators != null ? howToUseLocators.iOSAutomation() : null,
221                        iOSFindByArray);
222            }
223        }
224
225        if (isWindows()) {
226            WindowsFindBy[] windowsFindByArray = annotatedElement.getAnnotationsByType(WindowsFindBy.class);
227            if (windowsFindByArray != null && windowsFindByArray.length > 0) {
228                return buildMobileBy(howToUseLocators != null ? howToUseLocators.windowsAutomation() : null,
229                        windowsFindByArray);
230            }
231        }
232
233        return null;
234    }
235
236    @Override public boolean isLookupCached() {
237        AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
238        return (annotatedElement.getAnnotation(CacheLookup.class) != null);
239    }
240
241    private By returnMappedBy(By byDefault, By nativeAppBy) {
242        Map<ContentType, By> contentMap = new HashMap<>();
243        contentMap.put(ContentType.HTML_OR_DEFAULT, byDefault);
244        contentMap.put(ContentType.NATIVE_MOBILE_SPECIFIC, nativeAppBy);
245        return new ContentMappedBy(contentMap);
246    }
247
248    @Override public By buildBy() {
249        assertValidAnnotations();
250
251        By defaultBy = buildDefaultBy();
252        By mobileNativeBy = buildMobileNativeBy();
253
254        String idOrName = ((Field) annotatedElementContainer.getAnnotated()).getName();
255
256        if (defaultBy == null && mobileNativeBy == null) {
257            defaultBy =
258                new ByIdOrName(((Field) annotatedElementContainer.getAnnotated()).getName());
259            mobileNativeBy = new By.ById(idOrName);
260            return returnMappedBy(defaultBy, mobileNativeBy);
261        }
262
263        if (defaultBy == null) {
264            defaultBy =
265                new ByIdOrName(((Field) annotatedElementContainer.getAnnotated()).getName());
266            return returnMappedBy(defaultBy, mobileNativeBy);
267        }
268
269        if (mobileNativeBy == null) {
270            mobileNativeBy = defaultBy;
271            return returnMappedBy(defaultBy, mobileNativeBy);
272        }
273
274        return returnMappedBy(defaultBy, mobileNativeBy);
275    }
276}
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

Trigger isLookupCached code on LambdaTest Cloud Grid

Execute automation tests with isLookupCached 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)