How to use ByAll class of io.appium.java_client.pagefactory.bys.builder package

Best io.appium code snippet using io.appium.java_client.pagefactory.bys.builder.ByAll

Run io.appium automation tests on LambdaTest cloud grid

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

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 io.appium.java_client.pagefactory;
18
19import static java.lang.Integer.signum;
20import static java.util.Arrays.asList;
21import static java.util.Optional.ofNullable;
22
23import io.appium.java_client.pagefactory.bys.ContentMappedBy;
24import io.appium.java_client.pagefactory.bys.ContentType;
25import io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder;
26import io.appium.java_client.pagefactory.bys.builder.ByAll;
27import io.appium.java_client.pagefactory.bys.builder.ByChained;
28import io.appium.java_client.pagefactory.bys.builder.HowToUseSelectors;
29import org.openqa.selenium.By;
30import org.openqa.selenium.support.ByIdOrName;
31import org.openqa.selenium.support.CacheLookup;
32import org.openqa.selenium.support.FindAll;
33import org.openqa.selenium.support.FindBy;
34import org.openqa.selenium.support.FindBys;
35
36import java.lang.annotation.Annotation;
37import java.lang.reflect.AnnotatedElement;
38import java.lang.reflect.Field;
39import java.lang.reflect.InvocationTargetException;
40import java.lang.reflect.Method;
41import java.util.ArrayList;
42import java.util.Arrays;
43import java.util.Comparator;
44import java.util.HashMap;
45import java.util.List;
46import java.util.Map;
47import java.util.Optional;
48
49public class DefaultElementByBuilder extends AppiumByBuilder {
50
51    private static final String PRIORITY = "priority";
52    private static final String VALUE = "value";
53    private static final Class[] ANNOTATION_ARGUMENTS = new Class[]{};
54    private static final Object[] ANNOTATION_PARAMETERS = new Object[]{};
55
56    public DefaultElementByBuilder(String platform, String automation) {
57        super(platform, automation);
58    }
59
60    private static void checkDisallowedAnnotationPairs(Annotation a1, Annotation a2)
61            throws IllegalArgumentException {
62        if (a1 != null && a2 != null) {
63            throw new IllegalArgumentException(
64                    "If you use a '@" + a1.getClass().getSimpleName() + "' annotation, "
65                            + "you must not also use a '@" + a2.getClass().getSimpleName()
66                            + "' annotation");
67        }
68    }
69
70    private static By buildMobileBy(LocatorGroupStrategy locatorGroupStrategy, By[] bys) {
71        if (bys.length == 0) {
72            return null;
73        }
74        LocatorGroupStrategy strategy = ofNullable(locatorGroupStrategy)
75                .orElse(LocatorGroupStrategy.CHAIN);
76        if (strategy.equals(LocatorGroupStrategy.ALL_POSSIBLE)) {
77            return new ByAll(bys);
78        }
79        return new ByChained(bys);
80    }
81
82    @Override
83    protected void assertValidAnnotations() {
84        AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
85        FindBy findBy = annotatedElement.getAnnotation(FindBy.class);
86        FindBys findBys = annotatedElement.getAnnotation(FindBys.class);
87        checkDisallowedAnnotationPairs(findBy, findBys);
88        FindAll findAll = annotatedElement.getAnnotation(FindAll.class);
89        checkDisallowedAnnotationPairs(findBy, findAll);
90        checkDisallowedAnnotationPairs(findBys, findAll);
91    }
92
93    @Override
94    protected By buildDefaultBy() {
95        AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
96        By defaultBy = null;
97        FindBy findBy = annotatedElement.getAnnotation(FindBy.class);
98        if (findBy != null) {
99            defaultBy = new FindBy.FindByBuilder().buildIt(findBy, (Field) annotatedElement);
100        }
101
102        if (defaultBy == null) {
103            FindBys findBys = annotatedElement.getAnnotation(FindBys.class);
104            if (findBys != null) {
105                defaultBy = new FindBys.FindByBuilder().buildIt(findBys, (Field) annotatedElement);
106            }
107        }
108
109        if (defaultBy == null) {
110            FindAll findAll = annotatedElement.getAnnotation(FindAll.class);
111            if (findAll != null) {
112                defaultBy = new FindAll.FindByBuilder().buildIt(findAll, (Field) annotatedElement);
113            }
114        }
115        return defaultBy;
116    }
117
118    private By[] getBys(Class<? extends Annotation> singleLocator, Class<? extends Annotation> chainedLocator,
119                        Class<? extends Annotation> allLocator) {
120        AnnotationComparator comparator = new AnnotationComparator();
121        AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
122
123        List<Annotation> annotations = new ArrayList<>(asList(annotatedElement.getAnnotationsByType(singleLocator)));
124        annotations.addAll(asList(annotatedElement.getAnnotationsByType(chainedLocator)));
125        annotations.addAll(asList(annotatedElement.getAnnotationsByType(allLocator)));
126
127        annotations.sort(comparator);
128        List<By> result = new ArrayList<>();
129
130        for (Annotation a : annotations) {
131            Class<?> annotationClass = a.annotationType();
132            if (singleLocator.equals(annotationClass)) {
133                result.add(createBy(new Annotation[]{a}, HowToUseSelectors.USE_ONE));
134                continue;
135            }
136
137            Method value;
138            Annotation[] subLocators;
139            try {
140                value = annotationClass.getMethod(VALUE, ANNOTATION_ARGUMENTS);
141                subLocators = (Annotation[]) value.invoke(a, ANNOTATION_PARAMETERS);
142            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
143                throw new ClassCastException(String.format("The annotation '%s' has no convenient '%s' method which "
144                        + "returns array of annotations", annotationClass.getName(), VALUE));
145            }
146
147            Arrays.sort(subLocators, comparator);
148            if (chainedLocator.equals(annotationClass)) {
149                result.add(createBy(subLocators, HowToUseSelectors.BUILD_CHAINED));
150                continue;
151            }
152
153            if (allLocator.equals(annotationClass)) {
154                result.add(createBy(subLocators, HowToUseSelectors.USE_ANY));
155            }
156        }
157
158        return result.toArray(new By[result.size()]);
159    }
160
161    @Override
162    protected By buildMobileNativeBy() {
163        AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
164        HowToUseLocators howToUseLocators = annotatedElement.getAnnotation(HowToUseLocators.class);
165
166        Optional<HowToUseLocators> howToUseLocatorsOptional = ofNullable(howToUseLocators);
167
168        if (isAndroid()) {
169            return buildMobileBy(howToUseLocatorsOptional.map(HowToUseLocators::androidAutomation).orElse(null),
170                    getBys(AndroidFindBy.class, AndroidFindBys.class, AndroidFindAll.class));
171        }
172
173        if (isIOSXcuit() || isIOS()) {
174            return buildMobileBy(howToUseLocatorsOptional.map(HowToUseLocators::iOSXCUITAutomation).orElse(null),
175                    getBys(iOSXCUITFindBy.class, iOSXCUITFindBys.class, iOSXCUITFindAll.class));
176        }
177
178        if (isWindows()) {
179            return buildMobileBy(howToUseLocatorsOptional.map(HowToUseLocators::windowsAutomation).orElse(null),
180                    getBys(WindowsFindBy.class, WindowsFindBys.class, WindowsFindAll.class));
181        }
182
183        return null;
184    }
185
186    @Override
187    public boolean isLookupCached() {
188        AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
189        return (annotatedElement.getAnnotation(CacheLookup.class) != null);
190    }
191
192    private By returnMappedBy(By byDefault, By nativeAppBy) {
193        Map<ContentType, By> contentMap = new HashMap<>();
194        contentMap.put(ContentType.HTML_OR_DEFAULT, byDefault);
195        contentMap.put(ContentType.NATIVE_MOBILE_SPECIFIC, nativeAppBy);
196        return new ContentMappedBy(contentMap);
197    }
198
199    @Override
200    public By buildBy() {
201        assertValidAnnotations();
202
203        By defaultBy = buildDefaultBy();
204        By mobileNativeBy = buildMobileNativeBy();
205
206        String idOrName = ((Field) annotatedElementContainer.getAnnotated()).getName();
207
208        if (defaultBy == null && mobileNativeBy == null) {
209            defaultBy =
210                    new ByIdOrName(((Field) annotatedElementContainer.getAnnotated()).getName());
211            mobileNativeBy = new By.ById(idOrName);
212            return returnMappedBy(defaultBy, mobileNativeBy);
213        }
214
215        if (defaultBy == null) {
216            defaultBy =
217                    new ByIdOrName(((Field) annotatedElementContainer.getAnnotated()).getName());
218            return returnMappedBy(defaultBy, mobileNativeBy);
219        }
220
221        if (mobileNativeBy == null) {
222            mobileNativeBy = defaultBy;
223            return returnMappedBy(defaultBy, mobileNativeBy);
224        }
225
226        return returnMappedBy(defaultBy, mobileNativeBy);
227    }
228
229    private static class AnnotationComparator implements Comparator<Annotation> {
230
231        private static Method getPriorityMethod(Class<? extends Annotation> clazz) {
232            try {
233                return clazz.getMethod(PRIORITY, ANNOTATION_ARGUMENTS);
234            } catch (NoSuchMethodException e) {
235                throw new ClassCastException(String.format("Class %s has no '%s' method", clazz.getName(), PRIORITY));
236            }
237        }
238
239        private static int getPriorityValue(Method priorityMethod, Annotation annotation,
240                                            Class<? extends Annotation> clazz) {
241            try {
242                return (int) priorityMethod.invoke(annotation, ANNOTATION_PARAMETERS);
243            } catch (IllegalAccessException | InvocationTargetException e) {
244                throw new IllegalArgumentException(String
245                        .format("It is impossible to get priority. Annotation class: %s", clazz.toString()), e);
246            }
247        }
248
249        @Override
250        public int compare(Annotation o1, Annotation o2) {
251            Class<? extends Annotation> c1 = o1.annotationType();
252            Class<? extends Annotation> c2 = o2.annotationType();
253
254            Method priority1 = getPriorityMethod(c1);
255            Method priority2 = getPriorityMethod(c2);
256
257            int p1 = getPriorityValue(priority1, o1, c1);
258            int p2 = getPriorityValue(priority2, o2, c2);
259
260            return signum(p1 - p2);
261        }
262    }
263}
Full Screen

SearchAll.java

Source: SearchAll.java Github

copy
1package com.github.hemanthsridhar.support; /**
2 * Created by hemanthsridhar on 12/12/21.
3 */
4
5import com.github.hemanthsridhar.builder.CustomPageFactoryFinder;
6import com.github.hemanthsridhar.pagefactory.AbstractCustomFindByBuilder;
7import io.appium.java_client.pagefactory.bys.builder.ByAll;
8import org.openqa.selenium.By;
9
10import java.lang.annotation.ElementType;
11import java.lang.annotation.Retention;
12import java.lang.annotation.RetentionPolicy;
13import java.lang.annotation.Target;
14import java.lang.reflect.Field;
15
16@Retention(RetentionPolicy.RUNTIME)
17@Target({ElementType.FIELD, ElementType.TYPE})
18@CustomPageFactoryFinder(value = SearchAll.FindByAllBuilder.class)
19public @interface SearchAll {
20
21    SearchBy[] value();
22
23    class FindByAllBuilder extends AbstractCustomFindByBuilder {
24        @Override
25        public By buildIt(Object annotation, Field field, String filePath) {
26            SearchAll findBys = (SearchAll) annotation;
27            SearchBy[] findByArray = findBys.value();
28            By[] byArray = new By[findByArray.length];
29            for (int i = 0; i < findByArray.length; i++) {
30                byArray[i] = buildByFromFindBy(findByArray[i], field, filePath);
31            }
32            return new ByAll(byArray);
33        }
34    }
35}
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 methods in ByAll

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)