Skip to content

Commit 667f81a

Browse files
committed
refactor: unify locator factories naming and toString implementations
1 parent 8d99d4c commit 667f81a

23 files changed

+506
-224
lines changed
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
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+
17+
package io.appium.java_client;
18+
19+
import lombok.Getter;
20+
import org.apache.commons.lang3.Validate;
21+
import org.openqa.selenium.By;
22+
import org.openqa.selenium.By.Remotable;
23+
import org.openqa.selenium.SearchContext;
24+
import org.openqa.selenium.WebElement;
25+
26+
import java.io.Serializable;
27+
import java.util.List;
28+
29+
@SuppressWarnings("serial")
30+
public abstract class AppiumBy extends By implements Remotable {
31+
32+
@Getter private final Parameters remoteParameters;
33+
private final String locatorName;
34+
35+
protected AppiumBy(String selector, String locatorString, String locatorName) {
36+
Validate.notBlank(locatorString, "Must supply a not empty locator value.");
37+
this.remoteParameters = new Parameters(selector, locatorString);
38+
this.locatorName = locatorName;
39+
}
40+
41+
@SuppressWarnings("unchecked")
42+
@Override public List<WebElement> findElements(SearchContext context) {
43+
return context.findElements(this);
44+
}
45+
46+
@Override public WebElement findElement(SearchContext context) {
47+
return context.findElement(this);
48+
}
49+
50+
@Override public String toString() {
51+
return String.format("By.%s: %s", locatorName, remoteParameters.value());
52+
}
53+
54+
/**
55+
* About Android accessibility
56+
* https://developer.android.com/intl/ru/training/accessibility/accessible-app.html
57+
* About iOS accessibility
58+
* https://developer.apple.com/library/ios/documentation/UIKit/Reference/
59+
* UIAccessibilityIdentification_Protocol/index.html
60+
* @param accessibilityId id is a convenient UI automation accessibility Id.
61+
* @return an instance of {@link AppiumBy.ByAndroidUIAutomator}
62+
*/
63+
public static By accessibilityId(final String accessibilityId) {
64+
return new ByAccessibilityId(accessibilityId);
65+
}
66+
67+
/**
68+
* This locator strategy is only available in Espresso Driver mode.
69+
* @param dataMatcherString is a valid json string detailing hamcrest matcher for Espresso onData().
70+
* See <a href="http://appium.io/docs/en/writing-running-appium/android/espresso-datamatcher-selector/">
71+
* the documentation</a> for more details
72+
* @return an instance of {@link AppiumBy.ByAndroidDataMatcher}
73+
*/
74+
public static By androidDataMatcher(final String dataMatcherString) {
75+
return new ByAndroidDataMatcher(dataMatcherString);
76+
}
77+
78+
/**
79+
* Refer to https://developer.android.com/training/testing/ui-automator
80+
* @param uiautomatorText is Android UIAutomator string
81+
* @return an instance of {@link AppiumBy.ByAndroidUIAutomator}
82+
*/
83+
public static By androidUIAutomator(final String uiautomatorText) {
84+
return new ByAndroidUIAutomator(uiautomatorText);
85+
}
86+
87+
/**
88+
* This locator strategy is only available in Espresso Driver mode.
89+
* @param viewMatcherString is a valid json string detailing hamcrest matcher for Espresso onView().
90+
* See <a href="http://appium.io/docs/en/writing-running-appium/android/espresso-datamatcher-selector/">
91+
* the documentation</a> for more details
92+
* @return an instance of {@link AppiumBy.ByAndroidViewMatcher}
93+
*/
94+
public static By androidViewMatcher(final String viewMatcherString) {
95+
return new ByAndroidViewMatcher(viewMatcherString);
96+
}
97+
98+
/**
99+
* This locator strategy is available in Espresso Driver mode.
100+
* @since Appium 1.8.2 beta
101+
* @param tag is a view tag string
102+
* @return an instance of {@link ByAndroidViewTag}
103+
*/
104+
public static By androidViewTag(final String tag) {
105+
return new ByAndroidViewTag(tag);
106+
}
107+
108+
/**
109+
* For IOS it is the full name of the XCUI element and begins with XCUIElementType.
110+
* For Android it is the full name of the UIAutomator2 class (e.g.: android.widget.TextView)
111+
* @param selector the class name of the element
112+
* @return an instance of {@link ByClassName}
113+
*/
114+
public static By className(final String selector) {
115+
return new ByClassName(selector);
116+
}
117+
118+
/**
119+
* This type of locator requires the use of the 'customFindModules' capability and a
120+
* separately-installed element finding plugin.
121+
*
122+
* @param selector selector to pass to the custom element finding plugin
123+
* @return an instance of {@link ByCustom}
124+
* @since Appium 1.9.2
125+
*/
126+
public static By custom(final String selector) {
127+
return new ByCustom(selector);
128+
}
129+
130+
/**
131+
* This locator strategy is available only if OpenCV libraries and
132+
* NodeJS bindings are installed on the server machine.
133+
*
134+
* @see <a href="https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/image-comparison.md">
135+
* The documentation on Image Comparison Features</a>
136+
* @see <a href="https://github.com/appium/appium-base-driver/blob/master/lib/basedriver/device-settings.js">
137+
* The settings available for lookup fine-tuning</a>
138+
* @since Appium 1.8.2
139+
* @param b64Template base64-encoded template image string. Supported image formats are the same
140+
* as for OpenCV library.
141+
* @return an instance of {@link ByImage}
142+
*/
143+
public static By image(final String b64Template) {
144+
return new ByImage(b64Template);
145+
}
146+
147+
/**
148+
* This locator strategy is available in XCUITest Driver mode.
149+
* @param iOSClassChainString is a valid class chain locator string.
150+
* See <a href="https://github.com/facebookarchive/WebDriverAgent/wiki/Class-Chain-Queries-Construction-Rules">
151+
* the documentation</a> for more details
152+
* @return an instance of {@link AppiumBy.ByIosClassChain}
153+
*/
154+
public static By iOSClassChain(final String iOSClassChainString) {
155+
return new ByIosClassChain(iOSClassChainString);
156+
}
157+
158+
/**
159+
* This locator strategy is available in XCUITest Driver mode.
160+
* @param iOSNsPredicateString is an iOS NsPredicate String
161+
* @return an instance of {@link AppiumBy.ByIosNsPredicate}
162+
*/
163+
public static By iOSNsPredicateString(final String iOSNsPredicateString) {
164+
return new ByIosNsPredicate(iOSNsPredicateString);
165+
}
166+
167+
/**
168+
* The Windows UIAutomation selector.
169+
* @param windowsAutomation The element name in the Windows UIAutomation selector
170+
* @return an instance of {@link AppiumBy.ByWindowsAutomation}
171+
*/
172+
public static By windowsAutomation(final String windowsAutomation) {
173+
return new ByWindowsAutomation(windowsAutomation);
174+
}
175+
176+
public static class ByAccessibilityId extends AppiumBy implements Serializable {
177+
178+
public ByAccessibilityId(String accessibilityId) {
179+
super("accessibility id", accessibilityId, "accessibilityId");
180+
}
181+
}
182+
183+
public static class ByAndroidDataMatcher extends AppiumBy implements Serializable {
184+
185+
protected ByAndroidDataMatcher(String locatorString) {
186+
super("-android datamatcher", locatorString, "androidDataMatcher");
187+
}
188+
}
189+
190+
public static class ByAndroidUIAutomator extends AppiumBy implements Serializable {
191+
192+
public ByAndroidUIAutomator(String uiautomatorText) {
193+
super("-android uiautomator", uiautomatorText, "androidUIAutomator");
194+
}
195+
}
196+
197+
public static class ByAndroidViewMatcher extends AppiumBy implements Serializable {
198+
199+
protected ByAndroidViewMatcher(String locatorString) {
200+
super("-android viewmatcher", locatorString, "androidViewMatcher");
201+
}
202+
}
203+
204+
public static class ByAndroidViewTag extends AppiumBy implements Serializable {
205+
206+
public ByAndroidViewTag(String tag) {
207+
super("-android viewtag", tag, "androidViewTag");
208+
}
209+
}
210+
211+
public static class ByClassName extends AppiumBy implements Serializable {
212+
213+
protected ByClassName(String selector) {
214+
super("class name", selector, "className");
215+
}
216+
}
217+
218+
public static class ByCustom extends AppiumBy implements Serializable {
219+
220+
protected ByCustom(String selector) {
221+
super("-custom", selector, "custom");
222+
}
223+
}
224+
225+
public static class ByImage extends AppiumBy implements Serializable {
226+
227+
protected ByImage(String b64Template) {
228+
super("-image", b64Template, "image");
229+
}
230+
}
231+
232+
public static class ByIosClassChain extends AppiumBy implements Serializable {
233+
234+
protected ByIosClassChain(String locatorString) {
235+
super("-ios class chain", locatorString, "iOSClassChain");
236+
}
237+
}
238+
239+
public static class ByIosNsPredicate extends AppiumBy implements Serializable {
240+
241+
protected ByIosNsPredicate(String locatorString) {
242+
super("-ios predicate string", locatorString, "iOSNsPredicate");
243+
}
244+
}
245+
246+
public static class ByWindowsAutomation extends AppiumBy implements Serializable {
247+
248+
protected ByWindowsAutomation(String locatorString) {
249+
super("-windows uiautomation", locatorString, "windowsAutomation");
250+
}
251+
}
252+
}
253+
254+

0 commit comments

Comments
 (0)