How to use toString method of io.appium.java_client.MobileBy.ByIosClassChain class

Best io.appium code snippet using io.appium.java_client.MobileBy.ByIosClassChain.toString

Run io.appium automation tests on LambdaTest cloud grid

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

MobileBy.java

Source: MobileBy.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;
18
19import org.apache.commons.lang3.StringUtils;
20import org.openqa.selenium.By;
21import org.openqa.selenium.SearchContext;
22import org.openqa.selenium.WebDriverException;
23import org.openqa.selenium.WebElement;
24
25import java.io.Serializable;
26import java.util.List;
27
28@SuppressWarnings("serial")
29public abstract class MobileBy extends By {
30
31    private static final String ERROR_TEXT = "The class %s of the given context "
32        + "doesn't implement %s nor %s. Sorry. It is impossible to find something.";
33
34    private final String locatorString;
35    private final MobileSelector selector;
36
37    private static IllegalArgumentException formIllegalArgumentException(Class<?> givenClass,
38        Class<?> class1, Class<?> class2) {
39        return new IllegalArgumentException(String.format(ERROR_TEXT, givenClass.getCanonicalName(),
40            class1.getCanonicalName(), class2.getCanonicalName()));
41    }
42
43    protected MobileBy(MobileSelector selector, String locatorString) {
44        if (StringUtils.isBlank(locatorString)) {
45            throw new IllegalArgumentException("Must supply a not empty locator value.");
46        }
47        this.locatorString = locatorString;
48        this.selector = selector;
49    }
50
51    protected String getLocatorString() {
52        return locatorString;
53    }
54
55    @SuppressWarnings("unchecked")
56    @Override public List<WebElement> findElements(SearchContext context) {
57        return (List<WebElement>) ((FindsByFluentSelector<?>) context)
58            .findElements(selector.toString(), getLocatorString());
59    }
60
61    @Override public WebElement findElement(SearchContext context) {
62        return ((FindsByFluentSelector<?>) context)
63            .findElement(selector.toString(), getLocatorString());
64    }
65
66    /**
67     * Read https://developer.apple.com/library/tvos/documentation/DeveloperTools/
68     * Conceptual/InstrumentsUserGuide/UIAutomation.html
69     *
70     * @param iOSAutomationText is iOS UIAutomation string
71     * @return an instance of {@link io.appium.java_client.MobileBy.ByIosUIAutomation}
72     */
73    public static By IosUIAutomation(final String iOSAutomationText) {
74        return new ByIosUIAutomation(iOSAutomationText);
75    }
76
77    /**
78     * Read http://developer.android.com/intl/ru/tools/testing-support-library/
79     * index.html#uia-apis
80     * @param uiautomatorText is Android UIAutomator string
81     * @return an instance of {@link io.appium.java_client.MobileBy.ByAndroidUIAutomator}
82     */
83    public static By AndroidUIAutomator(final String uiautomatorText) {
84        return new ByAndroidUIAutomator(uiautomatorText);
85    }
86
87    /**
88     * About Android accessibility
89     * https://developer.android.com/intl/ru/training/accessibility/accessible-app.html
90     * About iOS accessibility
91     * https://developer.apple.com/library/ios/documentation/UIKit/Reference/
92     * UIAccessibilityIdentification_Protocol/index.html
93     * @param accessibilityId id is a convenient UI automation accessibility Id.
94     * @return an instance of {@link io.appium.java_client.MobileBy.ByAndroidUIAutomator}
95     */
96    public static By AccessibilityId(final String accessibilityId) {
97        return new ByAccessibilityId(accessibilityId);
98    }
99
100    /**
101     * This locator strategy is available in XCUITest Driver mode
102     * @param iOSClassChainString is a valid class chain locator string.
103     *                            See <a href="https://github.com/facebook/WebDriverAgent/wiki/Queries">
104     *                            the documentation</a> for more details
105     * @return an instance of {@link io.appium.java_client.MobileBy.ByIosClassChain}
106     */
107    public static By iOSClassChain(final String iOSClassChainString) {
108        return new ByIosClassChain(iOSClassChainString);
109    }
110
111    /**
112    * This locator strategy is available in XCUITest Driver mode
113    * @param iOSNsPredicateString is an an iOS NsPredicate String
114    * @return an instance of {@link io.appium.java_client.MobileBy.ByIosNsPredicate}
115    */
116    public static By iOSNsPredicateString(final String iOSNsPredicateString) {
117        return new ByIosNsPredicate(iOSNsPredicateString);
118    }
119
120    public static By windowsAutomation(final String windowsAutomation) {
121        return new ByWindowsAutomation(windowsAutomation);
122    }
123    
124    public static class ByIosUIAutomation extends MobileBy implements Serializable {
125
126        public ByIosUIAutomation(String iOSAutomationText) {
127            super(MobileSelector.IOS_UI_AUTOMATION, iOSAutomationText);
128        }
129
130        /**
131         * @throws WebDriverException when current session doesn't support the given selector or when
132         *      value of the selector is not consistent.
133         * @throws IllegalArgumentException when it is impossible to find something on the given
134         * {@link SearchContext} instance
135         */
136        @SuppressWarnings("unchecked")
137        @Override
138        public List<WebElement> findElements(SearchContext context) throws WebDriverException,
139            IllegalArgumentException {
140            Class<?> contextClass = context.getClass();
141
142            if (FindsByIosUIAutomation.class.isAssignableFrom(contextClass)) {
143                return FindsByIosUIAutomation.class.cast(context)
144                    .findElementsByIosUIAutomation(getLocatorString());
145            }
146
147            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
148                return super.findElements(context);
149            }
150
151            throw formIllegalArgumentException(contextClass, FindsByIosUIAutomation.class,
152                FindsByFluentSelector.class);
153        }
154
155        /**
156         * @throws WebDriverException when current session doesn't support the given selector or when
157         *      value of the selector is not consistent.
158         * @throws IllegalArgumentException when it is impossible to find something on the given
159         * {@link SearchContext} instance
160         */
161        @Override public WebElement findElement(SearchContext context) throws WebDriverException,
162            IllegalArgumentException {
163            Class<?> contextClass = context.getClass();
164
165            if (FindsByIosUIAutomation.class.isAssignableFrom(contextClass)) {
166                return ((FindsByIosUIAutomation<?>) context)
167                    .findElementByIosUIAutomation(getLocatorString());
168            }
169
170            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
171                return super.findElement(context);
172            }
173
174            throw formIllegalArgumentException(contextClass, FindsByIosUIAutomation.class,
175                FindsByFluentSelector.class);
176        }
177
178        @Override public String toString() {
179            return "By.IosUIAutomation: " + getLocatorString();
180        }
181    }
182
183
184    public static class ByAndroidUIAutomator extends MobileBy implements Serializable {
185
186
187        public ByAndroidUIAutomator(String uiautomatorText) {
188            super(MobileSelector.ANDROID_UI_AUTOMATOR, uiautomatorText);
189        }
190
191        /**
192         * @throws WebDriverException when current session doesn't support the given selector or when
193         *      value of the selector is not consistent.
194         * @throws IllegalArgumentException when it is impossible to find something on the given
195         * {@link SearchContext} instance
196         */
197        @SuppressWarnings("unchecked")
198        @Override
199        public List<WebElement> findElements(SearchContext context) throws WebDriverException,
200            IllegalArgumentException {
201            Class<?> contextClass = context.getClass();
202
203            if (FindsByAndroidUIAutomator.class.isAssignableFrom(contextClass)) {
204                return FindsByAndroidUIAutomator.class.cast(context)
205                    .findElementsByAndroidUIAutomator(getLocatorString());
206            }
207
208            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
209                return super.findElements(context);
210            }
211
212            throw formIllegalArgumentException(contextClass, FindsByAndroidUIAutomator.class,
213                FindsByFluentSelector.class);
214        }
215
216        /**
217         * @throws WebDriverException when current session doesn't support the given selector or when
218         *      value of the selector is not consistent.
219         * @throws IllegalArgumentException when it is impossible to find something on the given
220         * {@link SearchContext} instance
221         */
222        @Override public WebElement findElement(SearchContext context) throws WebDriverException,
223            IllegalArgumentException {
224            Class<?> contextClass = context.getClass();
225
226            if (FindsByAndroidUIAutomator.class.isAssignableFrom(contextClass)) {
227                return FindsByAndroidUIAutomator.class.cast(context)
228                    .findElementByAndroidUIAutomator(getLocatorString());
229            }
230
231            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
232                return super.findElement(context);
233            }
234
235            throw formIllegalArgumentException(contextClass, FindsByAndroidUIAutomator.class,
236                FindsByFluentSelector.class);
237        }
238
239        @Override public String toString() {
240            return "By.AndroidUIAutomator: " + getLocatorString();
241        }
242    }
243
244
245    public static class ByAccessibilityId extends MobileBy implements Serializable {
246
247        public ByAccessibilityId(String accessibilityId) {
248            super(MobileSelector.ACCESSIBILITY, accessibilityId);
249        }
250
251        /**
252         * @throws WebDriverException when current session doesn't support the given selector or when
253         *      value of the selector is not consistent.
254         * @throws IllegalArgumentException when it is impossible to find something on the given
255         * {@link SearchContext} instance
256         */
257        @SuppressWarnings("unchecked")
258        @Override
259        public List<WebElement> findElements(SearchContext context) throws WebDriverException,
260            IllegalArgumentException {
261            Class<?> contextClass = context.getClass();
262
263            if (FindsByAccessibilityId.class.isAssignableFrom(contextClass)) {
264                return FindsByAccessibilityId.class.cast(context)
265                    .findElementsByAccessibilityId(getLocatorString());
266            }
267
268            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
269                return super.findElements(context);
270            }
271
272            throw formIllegalArgumentException(contextClass, FindsByAccessibilityId.class,
273                FindsByFluentSelector.class);
274        }
275
276        /**
277         * @throws WebDriverException when current session doesn't support the given selector or when
278         *      value of the selector is not consistent.
279         * @throws IllegalArgumentException when it is impossible to find something on the given
280         * {@link SearchContext} instance
281         */
282        @Override public WebElement findElement(SearchContext context) throws WebDriverException,
283            IllegalArgumentException {
284            Class<?> contextClass = context.getClass();
285
286            if (FindsByAccessibilityId.class.isAssignableFrom(contextClass)) {
287                return FindsByAccessibilityId.class.cast(context)
288                    .findElementByAccessibilityId(getLocatorString());
289            }
290
291            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
292                return super.findElement(context);
293            }
294
295            throw formIllegalArgumentException(contextClass, FindsByAccessibilityId.class,
296                FindsByFluentSelector.class);
297        }
298
299        @Override public String toString() {
300            return "By.AccessibilityId: " + getLocatorString();
301        }
302    }
303
304    public static class ByIosClassChain extends MobileBy implements Serializable {
305
306        protected ByIosClassChain(String locatorString) {
307            super(MobileSelector.IOS_CLASS_CHAIN, locatorString);
308        }
309
310        /**
311         * @throws WebDriverException when current session doesn't support the given selector or when
312         *      value of the selector is not consistent.
313         * @throws IllegalArgumentException when it is impossible to find something on the given
314         * {@link SearchContext} instance
315         */
316        @SuppressWarnings("unchecked")
317        @Override public List<WebElement> findElements(SearchContext context) {
318            Class<?> contextClass = context.getClass();
319
320            if (FindsByIosClassChain.class.isAssignableFrom(contextClass)) {
321                return FindsByIosClassChain.class.cast(context)
322                        .findElementsByIosClassChain(getLocatorString());
323            }
324
325            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
326                return super.findElements(context);
327            }
328
329            throw formIllegalArgumentException(contextClass, FindsByIosClassChain.class,
330                    FindsByFluentSelector.class);
331        }
332
333        /**
334         * @throws WebDriverException when current session doesn't support the given selector or when
335         *      value of the selector is not consistent.
336         * @throws IllegalArgumentException when it is impossible to find something on the given
337         * {@link SearchContext} instance
338         */
339        @Override public WebElement findElement(SearchContext context) {
340            Class<?> contextClass = context.getClass();
341
342            if (FindsByIosClassChain.class.isAssignableFrom(contextClass)) {
343                return FindsByIosClassChain.class.cast(context)
344                        .findElementByIosClassChain(getLocatorString());
345            }
346
347            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
348                return super.findElement(context);
349            }
350
351            throw formIllegalArgumentException(contextClass, FindsByIosClassChain.class,
352                    FindsByFluentSelector.class);
353        }
354
355        @Override public String toString() {
356            return "By.IosClassChain: " + getLocatorString();
357        }
358    }
359
360    public static class ByIosNsPredicate extends MobileBy implements Serializable {
361
362        protected ByIosNsPredicate(String locatorString) {
363            super(MobileSelector.IOS_PREDICATE_STRING, locatorString);
364        }
365
366        /**
367         * @throws WebDriverException when current session doesn't support the given selector or when
368         *      value of the selector is not consistent.
369         * @throws IllegalArgumentException when it is impossible to find something on the given
370         * {@link SearchContext} instance
371         */
372        @SuppressWarnings("unchecked")
373        @Override public List<WebElement> findElements(SearchContext context) {
374            Class<?> contextClass = context.getClass();
375
376            if (FindsByIosNSPredicate.class.isAssignableFrom(contextClass)) {
377                return FindsByIosNSPredicate.class.cast(context)
378                        .findElementsByIosNsPredicate(getLocatorString());
379            }
380
381            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
382                return super.findElements(context);
383            }
384
385            throw formIllegalArgumentException(contextClass, FindsByIosNSPredicate.class,
386                    FindsByFluentSelector.class);
387        }
388
389        /**
390         * @throws WebDriverException when current session doesn't support the given selector or when
391         *      value of the selector is not consistent.
392         * @throws IllegalArgumentException when it is impossible to find something on the given
393         * {@link SearchContext} instance
394         */
395        @Override public WebElement findElement(SearchContext context) {
396            Class<?> contextClass = context.getClass();
397
398            if (FindsByIosNSPredicate.class.isAssignableFrom(contextClass)) {
399                return FindsByIosNSPredicate.class.cast(context)
400                        .findElementByIosNsPredicate(getLocatorString());
401            }
402
403            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
404                return super.findElement(context);
405            }
406
407            throw formIllegalArgumentException(contextClass, FindsByIosNSPredicate.class,
408                    FindsByFluentSelector.class);
409        }
410
411        @Override public String toString() {
412            return "By.IosNsPredicate: " + getLocatorString();
413        }
414    }
415
416    public static class ByWindowsAutomation extends MobileBy implements Serializable {
417
418        protected ByWindowsAutomation(String locatorString) {
419            super(MobileSelector.WINDOWS_UI_AUTOMATION, locatorString);
420        }
421
422        /**
423         * @throws WebDriverException when current session doesn't support the given selector or when
424         *      value of the selector is not consistent.
425         * @throws IllegalArgumentException when it is impossible to find something on the given
426         * {@link SearchContext} instance
427         */
428        @SuppressWarnings("unchecked")
429        @Override public List<WebElement> findElements(SearchContext context) {
430            Class<?> contextClass = context.getClass();
431
432            if (FindsByWindowsAutomation.class.isAssignableFrom(contextClass)) {
433                return FindsByWindowsAutomation.class.cast(context)
434                    .findElementsByWindowsUIAutomation(getLocatorString());
435            }
436
437            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
438                return super.findElements(context);
439            }
440
441            throw formIllegalArgumentException(contextClass, FindsByWindowsAutomation.class,
442                FindsByFluentSelector.class);
443        }
444
445        /**
446         * @throws WebDriverException when current session doesn't support the given selector or when
447         *      value of the selector is not consistent.
448         * @throws IllegalArgumentException when it is impossible to find something on the given
449         * {@link SearchContext} instance
450         */
451        @Override public WebElement findElement(SearchContext context) {
452            Class<?> contextClass = context.getClass();
453
454            if (FindsByWindowsAutomation.class.isAssignableFrom(contextClass)) {
455                return FindsByWindowsAutomation.class.cast(context)
456                    .findElementByWindowsUIAutomation(getLocatorString());
457            }
458
459            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
460                return super.findElement(context);
461            }
462
463            throw formIllegalArgumentException(contextClass, FindsByIosNSPredicate.class,
464                FindsByWindowsAutomation.class);
465        }
466    }
467}
468
469
470
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 MobileBy.ByIosClassChain

Trigger toString code on LambdaTest Cloud Grid

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