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