This repository was archived by the owner on May 20, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMobileBy.java
More file actions
633 lines (522 loc) · 24.8 KB
/
Copy pathMobileBy.java
File metadata and controls
633 lines (522 loc) · 24.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.appium.java_client;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import java.io.Serializable;
import java.util.List;
@SuppressWarnings("serial")
public abstract class MobileBy extends By {
private static final String ERROR_TEXT = "The class %s of the given context "
+ "doesn't implement %s nor %s. Sorry. It is impossible to find something.";
private final String locatorString;
private final MobileSelector selector;
private static IllegalArgumentException formIllegalArgumentException(Class<?> givenClass,
Class<?> class1, Class<?> class2) {
return new IllegalArgumentException(String.format(ERROR_TEXT, givenClass.getCanonicalName(),
class1.getCanonicalName(), class2.getCanonicalName()));
}
protected MobileBy(MobileSelector selector, String locatorString) {
if (StringUtils.isBlank(locatorString)) {
throw new IllegalArgumentException("Must supply a not empty locator value.");
}
this.locatorString = locatorString;
this.selector = selector;
}
protected String getLocatorString() {
return locatorString;
}
@SuppressWarnings("unchecked")
@Override public List<WebElement> findElements(SearchContext context) {
return (List<WebElement>) ((FindsByFluentSelector<?>) context)
.findElements(selector.toString(), getLocatorString());
}
@Override public WebElement findElement(SearchContext context) {
return ((FindsByFluentSelector<?>) context)
.findElement(selector.toString(), getLocatorString());
}
/**
* Read http://developer.android.com/intl/ru/tools/testing-support-library/
* index.html#uia-apis
* @param uiautomatorText is Android UIAutomator string
* @return an instance of {@link io.appium.java_client.MobileBy.ByAndroidUIAutomator}
*/
public static By AndroidUIAutomator(final String uiautomatorText) {
return new ByAndroidUIAutomator(uiautomatorText);
}
/**
* About Android accessibility
* https://developer.android.com/intl/ru/training/accessibility/accessible-app.html
* About iOS accessibility
* https://developer.apple.com/library/ios/documentation/UIKit/Reference/
* UIAccessibilityIdentification_Protocol/index.html
* @param accessibilityId id is a convenient UI automation accessibility Id.
* @return an instance of {@link io.appium.java_client.MobileBy.ByAndroidUIAutomator}
*/
public static By AccessibilityId(final String accessibilityId) {
return new ByAccessibilityId(accessibilityId);
}
/**
* This locator strategy is available in XCUITest Driver mode.
* @param iOSClassChainString is a valid class chain locator string.
* See <a href="https://github.com/facebook/WebDriverAgent/wiki/Queries">
* the documentation</a> for more details
* @return an instance of {@link io.appium.java_client.MobileBy.ByIosClassChain}
*/
public static By iOSClassChain(final String iOSClassChainString) {
return new ByIosClassChain(iOSClassChainString);
}
/**
* This locator strategy is available in XCUITest Driver mode.
* @param iOSNsPredicateString is an an iOS NsPredicate String
* @return an instance of {@link io.appium.java_client.MobileBy.ByIosNsPredicate}
*/
public static By iOSNsPredicateString(final String iOSNsPredicateString) {
return new ByIosNsPredicate(iOSNsPredicateString);
}
public static By windowsAutomation(final String windowsAutomation) {
return new ByWindowsAutomation(windowsAutomation);
}
/**
* This locator strategy is available in Espresso Driver mode.
* @since Appium 1.8.2 beta
* @param tag is an view tag string
* @return an instance of {@link ByAndroidViewTag}
*/
public static By AndroidViewTag(final String tag) {
return new ByAndroidViewTag(tag);
}
/**
* This locator strategy is available only if OpenCV libraries and
* NodeJS bindings are installed on the server machine.
*
* @see <a href="https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/image-comparison.md">
* The documentation on Image Comparison Features</a>
* @see <a href="https://github.com/appium/appium-base-driver/blob/master/lib/basedriver/device-settings.js">
* The settings available for lookup fine-tuning</a>
* @since Appium 1.8.2
* @param b64Template base64-encoded template image string. Supported image formats are the same
* as for OpenCV library.
* @return an instance of {@link ByImage}
*/
public static By image(final String b64Template) {
return new ByImage(b64Template);
}
/**
* This type of locator requires the use of the 'customFindModules' capability and a
* separately-installed element finding plugin.
*
* @param selector selector to pass to the custom element finding plugin
* @return an instance of {@link ByCustom}
* @since Appium 1.9.2
*/
public static By custom(final String selector) {
return new ByCustom(selector);
}
public static class ByAndroidUIAutomator extends MobileBy implements Serializable {
public ByAndroidUIAutomator(String uiautomatorText) {
super(MobileSelector.ANDROID_UI_AUTOMATOR, uiautomatorText);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@SuppressWarnings("unchecked")
@Override
public List<WebElement> findElements(SearchContext context) throws WebDriverException,
IllegalArgumentException {
Class<?> contextClass = context.getClass();
if (FindsByAndroidUIAutomator.class.isAssignableFrom(contextClass)) {
return FindsByAndroidUIAutomator.class.cast(context)
.findElementsByAndroidUIAutomator(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElements(context);
}
throw formIllegalArgumentException(contextClass, FindsByAndroidUIAutomator.class,
FindsByFluentSelector.class);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@Override public WebElement findElement(SearchContext context) throws WebDriverException,
IllegalArgumentException {
Class<?> contextClass = context.getClass();
if (FindsByAndroidUIAutomator.class.isAssignableFrom(contextClass)) {
return FindsByAndroidUIAutomator.class.cast(context)
.findElementByAndroidUIAutomator(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElement(context);
}
throw formIllegalArgumentException(contextClass, FindsByAndroidUIAutomator.class,
FindsByFluentSelector.class);
}
@Override public String toString() {
return "By.AndroidUIAutomator: " + getLocatorString();
}
}
public static class ByAccessibilityId extends MobileBy implements Serializable {
public ByAccessibilityId(String accessibilityId) {
super(MobileSelector.ACCESSIBILITY, accessibilityId);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@SuppressWarnings("unchecked")
@Override
public List<WebElement> findElements(SearchContext context) throws WebDriverException,
IllegalArgumentException {
Class<?> contextClass = context.getClass();
if (FindsByAccessibilityId.class.isAssignableFrom(contextClass)) {
return FindsByAccessibilityId.class.cast(context)
.findElementsByAccessibilityId(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElements(context);
}
throw formIllegalArgumentException(contextClass, FindsByAccessibilityId.class,
FindsByFluentSelector.class);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@Override public WebElement findElement(SearchContext context) throws WebDriverException,
IllegalArgumentException {
Class<?> contextClass = context.getClass();
if (FindsByAccessibilityId.class.isAssignableFrom(contextClass)) {
return FindsByAccessibilityId.class.cast(context)
.findElementByAccessibilityId(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElement(context);
}
throw formIllegalArgumentException(contextClass, FindsByAccessibilityId.class,
FindsByFluentSelector.class);
}
@Override public String toString() {
return "By.AccessibilityId: " + getLocatorString();
}
}
public static class ByIosClassChain extends MobileBy implements Serializable {
protected ByIosClassChain(String locatorString) {
super(MobileSelector.IOS_CLASS_CHAIN, locatorString);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@SuppressWarnings("unchecked")
@Override public List<WebElement> findElements(SearchContext context) {
Class<?> contextClass = context.getClass();
if (FindsByIosClassChain.class.isAssignableFrom(contextClass)) {
return FindsByIosClassChain.class.cast(context)
.findElementsByIosClassChain(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElements(context);
}
throw formIllegalArgumentException(contextClass, FindsByIosClassChain.class,
FindsByFluentSelector.class);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@Override public WebElement findElement(SearchContext context) {
Class<?> contextClass = context.getClass();
if (FindsByIosClassChain.class.isAssignableFrom(contextClass)) {
return FindsByIosClassChain.class.cast(context)
.findElementByIosClassChain(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElement(context);
}
throw formIllegalArgumentException(contextClass, FindsByIosClassChain.class,
FindsByFluentSelector.class);
}
@Override public String toString() {
return "By.IosClassChain: " + getLocatorString();
}
}
public static class ByIosNsPredicate extends MobileBy implements Serializable {
protected ByIosNsPredicate(String locatorString) {
super(MobileSelector.IOS_PREDICATE_STRING, locatorString);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@SuppressWarnings("unchecked")
@Override public List<WebElement> findElements(SearchContext context) {
Class<?> contextClass = context.getClass();
if (FindsByIosNSPredicate.class.isAssignableFrom(contextClass)) {
return FindsByIosNSPredicate.class.cast(context)
.findElementsByIosNsPredicate(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElements(context);
}
throw formIllegalArgumentException(contextClass, FindsByIosNSPredicate.class,
FindsByFluentSelector.class);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@Override public WebElement findElement(SearchContext context) {
Class<?> contextClass = context.getClass();
if (FindsByIosNSPredicate.class.isAssignableFrom(contextClass)) {
return FindsByIosNSPredicate.class.cast(context)
.findElementByIosNsPredicate(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElement(context);
}
throw formIllegalArgumentException(contextClass, FindsByIosNSPredicate.class,
FindsByFluentSelector.class);
}
@Override public String toString() {
return "By.IosNsPredicate: " + getLocatorString();
}
}
public static class ByWindowsAutomation extends MobileBy implements Serializable {
protected ByWindowsAutomation(String locatorString) {
super(MobileSelector.WINDOWS_UI_AUTOMATION, locatorString);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@SuppressWarnings("unchecked")
@Override public List<WebElement> findElements(SearchContext context) {
Class<?> contextClass = context.getClass();
if (FindsByWindowsAutomation.class.isAssignableFrom(contextClass)) {
return FindsByWindowsAutomation.class.cast(context)
.findElementsByWindowsUIAutomation(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElements(context);
}
throw formIllegalArgumentException(contextClass, FindsByWindowsAutomation.class,
FindsByFluentSelector.class);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@Override public WebElement findElement(SearchContext context) {
Class<?> contextClass = context.getClass();
if (FindsByWindowsAutomation.class.isAssignableFrom(contextClass)) {
return FindsByWindowsAutomation.class.cast(context)
.findElementByWindowsUIAutomation(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElement(context);
}
throw formIllegalArgumentException(contextClass, FindsByIosNSPredicate.class,
FindsByWindowsAutomation.class);
}
}
public static class ByImage extends MobileBy implements Serializable {
protected ByImage(String b64Template) {
super(MobileSelector.IMAGE, b64Template);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@SuppressWarnings("unchecked")
@Override public List<WebElement> findElements(SearchContext context) {
Class<?> contextClass = context.getClass();
if (FindsByImage.class.isAssignableFrom(contextClass)) {
return FindsByImage.class.cast(context).findElementsByImage(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElements(context);
}
throw formIllegalArgumentException(contextClass, FindsByImage.class, FindsByFluentSelector.class);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@Override public WebElement findElement(SearchContext context) {
Class<?> contextClass = context.getClass();
if (FindsByImage.class.isAssignableFrom(contextClass)) {
return FindsByImage.class.cast(context).findElementByImage(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElement(context);
}
throw formIllegalArgumentException(contextClass, FindsByImage.class, FindsByFluentSelector.class);
}
@Override public String toString() {
return "By.Image: " + getLocatorString();
}
}
public static class ByCustom extends MobileBy implements Serializable {
protected ByCustom(String selector) {
super(MobileSelector.CUSTOM, selector);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@SuppressWarnings("unchecked")
@Override public List<WebElement> findElements(SearchContext context) {
Class<?> contextClass = context.getClass();
if (FindsByCustom.class.isAssignableFrom(contextClass)) {
return FindsByCustom.class.cast(context).findElementsByCustom(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElements(context);
}
throw formIllegalArgumentException(contextClass, FindsByCustom.class, FindsByFluentSelector.class);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@Override public WebElement findElement(SearchContext context) {
Class<?> contextClass = context.getClass();
if (FindsByCustom.class.isAssignableFrom(contextClass)) {
return FindsByCustom.class.cast(context).findElementByCustom(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElement(context);
}
throw formIllegalArgumentException(contextClass, FindsByCustom.class, FindsByFluentSelector.class);
}
@Override public String toString() {
return "By.Custom: " + getLocatorString();
}
}
public static class ByAndroidViewTag extends MobileBy implements Serializable {
public ByAndroidViewTag(String tag) {
super(MobileSelector.ANDROID_VIEWTAG, tag);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@SuppressWarnings("unchecked")
@Override
public List<WebElement> findElements(SearchContext context) throws WebDriverException,
IllegalArgumentException {
Class<?> contextClass = context.getClass();
if (FindsByAndroidViewTag.class.isAssignableFrom(contextClass)) {
return FindsByAndroidViewTag.class.cast(context)
.findElementsByAndroidViewTag(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElements(context);
}
throw formIllegalArgumentException(contextClass, FindsByAndroidViewTag.class,
FindsByFluentSelector.class);
}
/**
* {@inheritDoc}
*
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@Override public WebElement findElement(SearchContext context) throws WebDriverException,
IllegalArgumentException {
Class<?> contextClass = context.getClass();
if (FindsByAndroidViewTag.class.isAssignableFrom(contextClass)) {
return FindsByAndroidViewTag.class.cast(context)
.findElementByAndroidViewTag(getLocatorString());
}
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
return super.findElement(context);
}
throw formIllegalArgumentException(contextClass, FindsByAndroidViewTag.class,
FindsByFluentSelector.class);
}
@Override public String toString() {
return "By.AndroidViewTag: " + getLocatorString();
}
}
}