How to use createLocator method of io.appium.java_client.pagefactory.AppiumElementLocatorFactory class

Best io.appium code snippet using io.appium.java_client.pagefactory.AppiumElementLocatorFactory.createLocator

Run io.appium automation tests on LambdaTest cloud grid

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

AppiumElementLocatorFactory.java

Source: AppiumElementLocatorFactory.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 io.appium.java_client.pagefactory.WithTimeout.DurationBuilder.build;
20import static java.util.Optional.ofNullable;
21
22import io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder;
23import io.appium.java_client.pagefactory.locator.CacheableElementLocatorFactory;
24import io.appium.java_client.pagefactory.locator.CacheableLocator;
25import org.openqa.selenium.By;
26import org.openqa.selenium.SearchContext;
27
28import java.lang.reflect.AnnotatedElement;
29import java.lang.reflect.Field;
30import java.time.Duration;
31import javax.annotation.Nullable;
32
33public class AppiumElementLocatorFactory implements CacheableElementLocatorFactory {
34    private final SearchContext searchContext;
35    private final Duration duration;
36    private final AppiumByBuilder builder;
37
38    /**
39     * Creates a new mobile element locator factory.
40     *
41     * @param searchContext     The context to use when finding the element
42     * @param duration   timeout parameters for the elements to be found
43     * @param builder    is handler of Appium-specific page object annotations
44     */
45    public AppiumElementLocatorFactory(SearchContext searchContext, Duration duration,
46                                       AppiumByBuilder builder) {
47        this.searchContext = searchContext;
48        this.duration = duration;
49        this.builder = builder;
50    }
51
52    public @Nullable CacheableLocator createLocator(Field field) {
53        return this.createLocator((AnnotatedElement) field);
54    }
55
56    @Override public @Nullable CacheableLocator createLocator(AnnotatedElement annotatedElement) {
57        Duration customDuration;
58        if (annotatedElement.isAnnotationPresent(WithTimeout.class)) {
59            WithTimeout withTimeout = annotatedElement.getAnnotation(WithTimeout.class);
60            customDuration = build(withTimeout);
61        } else {
62            customDuration = duration;
63        }
64
65        builder.setAnnotated(annotatedElement);
66        By byResult = builder.buildBy();
67
68        return ofNullable(byResult)
69                .map(by -> new AppiumElementLocator(searchContext, by, builder.isLookupCached(), customDuration))
70                .orElse(null);
71    }
72
73
74}
75
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 AppiumElementLocatorFactory

Trigger createLocator code on LambdaTest Cloud Grid

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