Best io.appium code snippet using io.appium.java_client.MobileBy.ByCustom
MobileBy.java
Source:MobileBy.java
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 additional5 * information regarding copyright ownership.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */16package io.appium.java_client;17import lombok.AccessLevel;18import lombok.Getter;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;24import java.io.Serializable;25import java.util.List;26@SuppressWarnings("serial")27public abstract class MobileBy extends By {28 private static final String ERROR_TEXT = "The class %s of the given context "29 + "doesn't implement %s nor %s. Sorry. It is impossible to find something.";30 @Getter(AccessLevel.PROTECTED) private final String locatorString;31 private final MobileSelector selector;32 private static IllegalArgumentException formIllegalArgumentException(Class<?> givenClass,33 Class<?> class1, Class<?> class2) {34 return new IllegalArgumentException(String.format(ERROR_TEXT, givenClass.getCanonicalName(),35 class1.getCanonicalName(), class2.getCanonicalName()));36 }37 protected MobileBy(MobileSelector selector, String locatorString) {38 if (StringUtils.isBlank(locatorString)) {39 throw new IllegalArgumentException("Must supply a not empty locator value.");40 }41 this.locatorString = locatorString;42 this.selector = selector;43 }44 @SuppressWarnings("unchecked")45 @Override public List<WebElement> findElements(SearchContext context) {46 return (List<WebElement>) ((FindsByFluentSelector<?>) context)47 .findElements(selector.toString(), getLocatorString());48 }49 @Override public WebElement findElement(SearchContext context) {50 return ((FindsByFluentSelector<?>) context)51 .findElement(selector.toString(), getLocatorString());52 }53 /**54 * Refer to https://developer.android.com/training/testing/ui-automator55 * @param uiautomatorText is Android UIAutomator string56 * @return an instance of {@link io.appium.java_client.MobileBy.ByAndroidUIAutomator}57 */58 public static By AndroidUIAutomator(final String uiautomatorText) {59 return new ByAndroidUIAutomator(uiautomatorText);60 }61 /**62 * About Android accessibility63 * https://developer.android.com/intl/ru/training/accessibility/accessible-app.html64 * About iOS accessibility65 * https://developer.apple.com/library/ios/documentation/UIKit/Reference/66 * UIAccessibilityIdentification_Protocol/index.html67 * @param accessibilityId id is a convenient UI automation accessibility Id.68 * @return an instance of {@link io.appium.java_client.MobileBy.ByAndroidUIAutomator}69 */70 public static By AccessibilityId(final String accessibilityId) {71 return new ByAccessibilityId(accessibilityId);72 }73 /**74 * This locator strategy is available in XCUITest Driver mode.75 * @param iOSClassChainString is a valid class chain locator string.76 * See <a href="https://github.com/facebookarchive/WebDriverAgent/wiki/Class-Chain-Queries-Construction-Rules">77 * the documentation</a> for more details78 * @return an instance of {@link io.appium.java_client.MobileBy.ByIosClassChain}79 */80 public static By iOSClassChain(final String iOSClassChainString) {81 return new ByIosClassChain(iOSClassChainString);82 }83 /**84 * This locator strategy is only available in Espresso Driver mode.85 * @param dataMatcherString is a valid json string detailing hamcrest matcher for Espresso onData().86 * See <a href="http://appium.io/docs/en/writing-running-appium/android/espresso-datamatcher-selector/">87 * the documentation</a> for more details88 * @return an instance of {@link io.appium.java_client.MobileBy.ByAndroidDataMatcher}89 */90 public static By androidDataMatcher(final String dataMatcherString) {91 return new ByAndroidDataMatcher(dataMatcherString);92 }93 /**94 * This locator strategy is only available in Espresso Driver mode.95 * @param viewMatcherString is a valid json string detailing hamcrest matcher for Espresso onView().96 * See <a href="http://appium.io/docs/en/writing-running-appium/android/espresso-datamatcher-selector/">97 * the documentation</a> for more details98 * @return an instance of {@link io.appium.java_client.MobileBy.ByAndroidViewMatcher}99 */100 public static By androidViewMatcher(final String viewMatcherString) {101 return new ByAndroidViewMatcher(viewMatcherString);102 }103 /**104 * This locator strategy is available in XCUITest Driver mode.105 * @param iOSNsPredicateString is an an iOS NsPredicate String106 * @return an instance of {@link io.appium.java_client.MobileBy.ByIosNsPredicate}107 */108 public static By iOSNsPredicateString(final String iOSNsPredicateString) {109 return new ByIosNsPredicate(iOSNsPredicateString);110 }111 public static By windowsAutomation(final String windowsAutomation) {112 return new ByWindowsAutomation(windowsAutomation);113 }114 /**115 * This locator strategy is available in Espresso Driver mode.116 * @since Appium 1.8.2 beta117 * @param tag is an view tag string118 * @return an instance of {@link ByAndroidViewTag}119 */120 public static By AndroidViewTag(final String tag) {121 return new ByAndroidViewTag(tag);122 }123 /**124 * This locator strategy is available only if OpenCV libraries and125 * NodeJS bindings are installed on the server machine.126 *127 * @see <a href="https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/image-comparison.md">128 * The documentation on Image Comparison Features</a>129 * @see <a href="https://github.com/appium/appium-base-driver/blob/master/lib/basedriver/device-settings.js">130 * The settings available for lookup fine-tuning</a>131 * @since Appium 1.8.2132 * @param b64Template base64-encoded template image string. Supported image formats are the same133 * as for OpenCV library.134 * @return an instance of {@link ByImage}135 */136 public static By image(final String b64Template) {137 return new ByImage(b64Template);138 }139 /**140 * This type of locator requires the use of the 'customFindModules' capability and a141 * separately-installed element finding plugin.142 *143 * @param selector selector to pass to the custom element finding plugin144 * @return an instance of {@link ByCustom}145 * @since Appium 1.9.2146 */147 public static By custom(final String selector) {148 return new ByCustom(selector);149 }150 public static class ByAndroidUIAutomator extends MobileBy implements Serializable {151 public ByAndroidUIAutomator(String uiautomatorText) {152 super(MobileSelector.ANDROID_UI_AUTOMATOR, uiautomatorText);153 }154 /**155 * {@inheritDoc}156 *157 * @throws WebDriverException when current session doesn't support the given selector or when158 * value of the selector is not consistent.159 * @throws IllegalArgumentException when it is impossible to find something on the given160 * {@link SearchContext} instance161 */162 @SuppressWarnings("unchecked")163 @Override164 public List<WebElement> findElements(SearchContext context) throws WebDriverException,165 IllegalArgumentException {166 Class<?> contextClass = context.getClass();167 if (FindsByAndroidUIAutomator.class.isAssignableFrom(contextClass)) {168 return FindsByAndroidUIAutomator.class.cast(context)169 .findElementsByAndroidUIAutomator(getLocatorString());170 }171 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {172 return super.findElements(context);173 }174 throw formIllegalArgumentException(contextClass, FindsByAndroidUIAutomator.class,175 FindsByFluentSelector.class);176 }177 /**178 * {@inheritDoc}179 *180 * @throws WebDriverException when current session doesn't support the given selector or when181 * value of the selector is not consistent.182 * @throws IllegalArgumentException when it is impossible to find something on the given183 * {@link SearchContext} instance184 */185 @Override public WebElement findElement(SearchContext context) throws WebDriverException,186 IllegalArgumentException {187 Class<?> contextClass = context.getClass();188 if (FindsByAndroidUIAutomator.class.isAssignableFrom(contextClass)) {189 return FindsByAndroidUIAutomator.class.cast(context)190 .findElementByAndroidUIAutomator(getLocatorString());191 }192 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {193 return super.findElement(context);194 }195 throw formIllegalArgumentException(contextClass, FindsByAndroidUIAutomator.class,196 FindsByFluentSelector.class);197 }198 @Override public String toString() {199 return "By.AndroidUIAutomator: " + getLocatorString();200 }201 }202 public static class ByAccessibilityId extends MobileBy implements Serializable {203 public ByAccessibilityId(String accessibilityId) {204 super(MobileSelector.ACCESSIBILITY, accessibilityId);205 }206 /**207 * {@inheritDoc}208 *209 * @throws WebDriverException when current session doesn't support the given selector or when210 * value of the selector is not consistent.211 * @throws IllegalArgumentException when it is impossible to find something on the given212 * {@link SearchContext} instance213 */214 @SuppressWarnings("unchecked")215 @Override216 public List<WebElement> findElements(SearchContext context) throws WebDriverException,217 IllegalArgumentException {218 Class<?> contextClass = context.getClass();219 if (FindsByAccessibilityId.class.isAssignableFrom(contextClass)) {220 return FindsByAccessibilityId.class.cast(context)221 .findElementsByAccessibilityId(getLocatorString());222 }223 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {224 return super.findElements(context);225 }226 throw formIllegalArgumentException(contextClass, FindsByAccessibilityId.class,227 FindsByFluentSelector.class);228 }229 /**230 * {@inheritDoc}231 *232 * @throws WebDriverException when current session doesn't support the given selector or when233 * value of the selector is not consistent.234 * @throws IllegalArgumentException when it is impossible to find something on the given235 * {@link SearchContext} instance236 */237 @Override public WebElement findElement(SearchContext context) throws WebDriverException,238 IllegalArgumentException {239 Class<?> contextClass = context.getClass();240 if (FindsByAccessibilityId.class.isAssignableFrom(contextClass)) {241 return FindsByAccessibilityId.class.cast(context)242 .findElementByAccessibilityId(getLocatorString());243 }244 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {245 return super.findElement(context);246 }247 throw formIllegalArgumentException(contextClass, FindsByAccessibilityId.class,248 FindsByFluentSelector.class);249 }250 @Override public String toString() {251 return "By.AccessibilityId: " + getLocatorString();252 }253 }254 public static class ByIosClassChain extends MobileBy implements Serializable {255 protected ByIosClassChain(String locatorString) {256 super(MobileSelector.IOS_CLASS_CHAIN, locatorString);257 }258 /**259 * {@inheritDoc}260 *261 * @throws WebDriverException when current session doesn't support the given selector or when262 * value of the selector is not consistent.263 * @throws IllegalArgumentException when it is impossible to find something on the given264 * {@link SearchContext} instance265 */266 @SuppressWarnings("unchecked")267 @Override public List<WebElement> findElements(SearchContext context) {268 Class<?> contextClass = context.getClass();269 if (FindsByIosClassChain.class.isAssignableFrom(contextClass)) {270 return FindsByIosClassChain.class.cast(context)271 .findElementsByIosClassChain(getLocatorString());272 }273 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {274 return super.findElements(context);275 }276 throw formIllegalArgumentException(contextClass, FindsByIosClassChain.class,277 FindsByFluentSelector.class);278 }279 /**280 * {@inheritDoc}281 *282 * @throws WebDriverException when current session doesn't support the given selector or when283 * value of the selector is not consistent.284 * @throws IllegalArgumentException when it is impossible to find something on the given285 * {@link SearchContext} instance286 */287 @Override public WebElement findElement(SearchContext context) {288 Class<?> contextClass = context.getClass();289 if (FindsByIosClassChain.class.isAssignableFrom(contextClass)) {290 return FindsByIosClassChain.class.cast(context)291 .findElementByIosClassChain(getLocatorString());292 }293 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {294 return super.findElement(context);295 }296 throw formIllegalArgumentException(contextClass, FindsByIosClassChain.class,297 FindsByFluentSelector.class);298 }299 @Override public String toString() {300 return "By.IosClassChain: " + getLocatorString();301 }302 }303 public static class ByAndroidDataMatcher extends MobileBy implements Serializable {304 protected ByAndroidDataMatcher(String locatorString) {305 super(MobileSelector.ANDROID_DATA_MATCHER, locatorString);306 }307 /**308 * {@inheritDoc}309 *310 * @throws WebDriverException when current session doesn't support the given selector or when311 * value of the selector is not consistent.312 * @throws IllegalArgumentException when it is impossible to find something on the given313 * {@link SearchContext} instance314 */315 @SuppressWarnings("unchecked")316 @Override public List<WebElement> findElements(SearchContext context) {317 Class<?> contextClass = context.getClass();318 if (FindsByAndroidDataMatcher.class.isAssignableFrom(contextClass)) {319 return FindsByAndroidDataMatcher.class.cast(context)320 .findElementsByAndroidDataMatcher(getLocatorString());321 }322 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {323 return super.findElements(context);324 }325 throw formIllegalArgumentException(contextClass, FindsByAndroidDataMatcher.class,326 FindsByFluentSelector.class);327 }328 /**329 * {@inheritDoc}330 *331 * @throws WebDriverException when current session doesn't support the given selector or when332 * value of the selector is not consistent.333 * @throws IllegalArgumentException when it is impossible to find something on the given334 * {@link SearchContext} instance335 */336 @Override public WebElement findElement(SearchContext context) {337 Class<?> contextClass = context.getClass();338 if (FindsByAndroidDataMatcher.class.isAssignableFrom(contextClass)) {339 return FindsByAndroidDataMatcher.class.cast(context)340 .findElementByAndroidDataMatcher(getLocatorString());341 }342 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {343 return super.findElement(context);344 }345 throw formIllegalArgumentException(contextClass, FindsByAndroidDataMatcher.class,346 FindsByFluentSelector.class);347 }348 @Override public String toString() {349 return "By.FindsByAndroidDataMatcher: " + getLocatorString();350 }351 }352 public static class ByAndroidViewMatcher extends MobileBy implements Serializable {353 protected ByAndroidViewMatcher(String locatorString) {354 super(MobileSelector.ANDROID_VIEW_MATCHER, locatorString);355 }356 /**357 * {@inheritDoc}358 *359 * @throws WebDriverException when current session doesn't support the given selector or when360 * value of the selector is not consistent.361 * @throws IllegalArgumentException when it is impossible to find something on the given362 * {@link SearchContext} instance363 */364 @SuppressWarnings("unchecked")365 @Override public List<WebElement> findElements(SearchContext context) {366 Class<?> contextClass = context.getClass();367 if (FindsByAndroidViewMatcher.class.isAssignableFrom(contextClass)) {368 return FindsByAndroidViewMatcher.class.cast(context)369 .findElementsByAndroidViewMatcher(getLocatorString());370 }371 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {372 return super.findElements(context);373 }374 throw formIllegalArgumentException(contextClass, FindsByAndroidViewMatcher.class,375 FindsByFluentSelector.class);376 }377 /**378 * {@inheritDoc}379 *380 * @throws WebDriverException when current session doesn't support the given selector or when381 * value of the selector is not consistent.382 * @throws IllegalArgumentException when it is impossible to find something on the given383 * {@link SearchContext} instance384 */385 @Override public WebElement findElement(SearchContext context) {386 Class<?> contextClass = context.getClass();387 if (FindsByAndroidViewMatcher.class.isAssignableFrom(contextClass)) {388 return FindsByAndroidViewMatcher.class.cast(context)389 .findElementByAndroidViewMatcher(getLocatorString());390 }391 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {392 return super.findElement(context);393 }394 throw formIllegalArgumentException(contextClass, FindsByAndroidViewMatcher.class,395 FindsByFluentSelector.class);396 }397 @Override public String toString() {398 return "By.FindsByAndroidViewMatcher: " + getLocatorString();399 }400 }401 public static class ByIosNsPredicate extends MobileBy implements Serializable {402 protected ByIosNsPredicate(String locatorString) {403 super(MobileSelector.IOS_PREDICATE_STRING, locatorString);404 }405 /**406 * {@inheritDoc}407 *408 * @throws WebDriverException when current session doesn't support the given selector or when409 * value of the selector is not consistent.410 * @throws IllegalArgumentException when it is impossible to find something on the given411 * {@link SearchContext} instance412 */413 @SuppressWarnings("unchecked")414 @Override public List<WebElement> findElements(SearchContext context) {415 Class<?> contextClass = context.getClass();416 if (FindsByIosNSPredicate.class.isAssignableFrom(contextClass)) {417 return FindsByIosNSPredicate.class.cast(context)418 .findElementsByIosNsPredicate(getLocatorString());419 }420 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {421 return super.findElements(context);422 }423 throw formIllegalArgumentException(contextClass, FindsByIosNSPredicate.class,424 FindsByFluentSelector.class);425 }426 /**427 * {@inheritDoc}428 *429 * @throws WebDriverException when current session doesn't support the given selector or when430 * value of the selector is not consistent.431 * @throws IllegalArgumentException when it is impossible to find something on the given432 * {@link SearchContext} instance433 */434 @Override public WebElement findElement(SearchContext context) {435 Class<?> contextClass = context.getClass();436 if (FindsByIosNSPredicate.class.isAssignableFrom(contextClass)) {437 return FindsByIosNSPredicate.class.cast(context)438 .findElementByIosNsPredicate(getLocatorString());439 }440 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {441 return super.findElement(context);442 }443 throw formIllegalArgumentException(contextClass, FindsByIosNSPredicate.class,444 FindsByFluentSelector.class);445 }446 @Override public String toString() {447 return "By.IosNsPredicate: " + getLocatorString();448 }449 }450 public static class ByWindowsAutomation extends MobileBy implements Serializable {451 protected ByWindowsAutomation(String locatorString) {452 super(MobileSelector.WINDOWS_UI_AUTOMATION, locatorString);453 }454 /**455 * {@inheritDoc}456 *457 * @throws WebDriverException when current session doesn't support the given selector or when458 * value of the selector is not consistent.459 * @throws IllegalArgumentException when it is impossible to find something on the given460 * {@link SearchContext} instance461 */462 @SuppressWarnings("unchecked")463 @Override public List<WebElement> findElements(SearchContext context) {464 Class<?> contextClass = context.getClass();465 if (FindsByWindowsAutomation.class.isAssignableFrom(contextClass)) {466 return FindsByWindowsAutomation.class.cast(context)467 .findElementsByWindowsUIAutomation(getLocatorString());468 }469 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {470 return super.findElements(context);471 }472 throw formIllegalArgumentException(contextClass, FindsByWindowsAutomation.class,473 FindsByFluentSelector.class);474 }475 /**476 * {@inheritDoc}477 *478 * @throws WebDriverException when current session doesn't support the given selector or when479 * value of the selector is not consistent.480 * @throws IllegalArgumentException when it is impossible to find something on the given481 * {@link SearchContext} instance482 */483 @Override public WebElement findElement(SearchContext context) {484 Class<?> contextClass = context.getClass();485 if (FindsByWindowsAutomation.class.isAssignableFrom(contextClass)) {486 return FindsByWindowsAutomation.class.cast(context)487 .findElementByWindowsUIAutomation(getLocatorString());488 }489 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {490 return super.findElement(context);491 }492 throw formIllegalArgumentException(contextClass, FindsByIosNSPredicate.class,493 FindsByWindowsAutomation.class);494 }495 }496 public static class ByImage extends MobileBy implements Serializable {497 protected ByImage(String b64Template) {498 super(MobileSelector.IMAGE, b64Template);499 }500 /**501 * {@inheritDoc}502 *503 * @throws WebDriverException when current session doesn't support the given selector or when504 * value of the selector is not consistent.505 * @throws IllegalArgumentException when it is impossible to find something on the given506 * {@link SearchContext} instance507 */508 @SuppressWarnings("unchecked")509 @Override public List<WebElement> findElements(SearchContext context) {510 Class<?> contextClass = context.getClass();511 if (FindsByImage.class.isAssignableFrom(contextClass)) {512 return FindsByImage.class.cast(context).findElementsByImage(getLocatorString());513 }514 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {515 return super.findElements(context);516 }517 throw formIllegalArgumentException(contextClass, FindsByImage.class, FindsByFluentSelector.class);518 }519 /**520 * {@inheritDoc}521 *522 * @throws WebDriverException when current session doesn't support the given selector or when523 * value of the selector is not consistent.524 * @throws IllegalArgumentException when it is impossible to find something on the given525 * {@link SearchContext} instance526 */527 @Override public WebElement findElement(SearchContext context) {528 Class<?> contextClass = context.getClass();529 if (FindsByImage.class.isAssignableFrom(contextClass)) {530 return FindsByImage.class.cast(context).findElementByImage(getLocatorString());531 }532 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {533 return super.findElement(context);534 }535 throw formIllegalArgumentException(contextClass, FindsByImage.class, FindsByFluentSelector.class);536 }537 @Override public String toString() {538 return "By.Image: " + getLocatorString();539 }540 }541 public static class ByCustom extends MobileBy implements Serializable {542 protected ByCustom(String selector) {543 super(MobileSelector.CUSTOM, selector);544 }545 /**546 * {@inheritDoc}547 *548 * @throws WebDriverException when current session doesn't support the given selector or when549 * value of the selector is not consistent.550 * @throws IllegalArgumentException when it is impossible to find something on the given551 * {@link SearchContext} instance552 */553 @SuppressWarnings("unchecked")554 @Override public List<WebElement> findElements(SearchContext context) {555 Class<?> contextClass = context.getClass();556 if (FindsByCustom.class.isAssignableFrom(contextClass)) {557 return FindsByCustom.class.cast(context).findElementsByCustom(getLocatorString());558 }559 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {560 return super.findElements(context);561 }562 throw formIllegalArgumentException(contextClass, FindsByCustom.class, FindsByFluentSelector.class);563 }564 /**565 * {@inheritDoc}566 *567 * @throws WebDriverException when current session doesn't support the given selector or when568 * value of the selector is not consistent.569 * @throws IllegalArgumentException when it is impossible to find something on the given570 * {@link SearchContext} instance571 */572 @Override public WebElement findElement(SearchContext context) {573 Class<?> contextClass = context.getClass();574 if (FindsByCustom.class.isAssignableFrom(contextClass)) {575 return FindsByCustom.class.cast(context).findElementByCustom(getLocatorString());576 }577 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {578 return super.findElement(context);579 }580 throw formIllegalArgumentException(contextClass, FindsByCustom.class, FindsByFluentSelector.class);581 }582 @Override public String toString() {583 return "By.Custom: " + getLocatorString();584 }585 }586 public static class ByAndroidViewTag extends MobileBy implements Serializable {587 public ByAndroidViewTag(String tag) {588 super(MobileSelector.ANDROID_VIEWTAG, tag);589 }590 /**591 * {@inheritDoc}592 *593 * @throws WebDriverException when current session doesn't support the given selector or when594 * value of the selector is not consistent.595 * @throws IllegalArgumentException when it is impossible to find something on the given596 * {@link SearchContext} instance597 */598 @SuppressWarnings("unchecked")599 @Override600 public List<WebElement> findElements(SearchContext context) throws WebDriverException,601 IllegalArgumentException {602 Class<?> contextClass = context.getClass();603 if (FindsByAndroidViewTag.class.isAssignableFrom(contextClass)) {604 return FindsByAndroidViewTag.class.cast(context)605 .findElementsByAndroidViewTag(getLocatorString());606 }607 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {608 return super.findElements(context);609 }610 throw formIllegalArgumentException(contextClass, FindsByAndroidViewTag.class,611 FindsByFluentSelector.class);612 }613 /**614 * {@inheritDoc}615 *616 * @throws WebDriverException when current session doesn't support the given selector or when617 * value of the selector is not consistent.618 * @throws IllegalArgumentException when it is impossible to find something on the given619 * {@link SearchContext} instance620 */621 @Override public WebElement findElement(SearchContext context) throws WebDriverException,622 IllegalArgumentException {623 Class<?> contextClass = context.getClass();624 if (FindsByAndroidViewTag.class.isAssignableFrom(contextClass)) {625 return FindsByAndroidViewTag.class.cast(context)626 .findElementByAndroidViewTag(getLocatorString());627 }628 if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {629 return super.findElement(context);630 }631 throw formIllegalArgumentException(contextClass, FindsByAndroidViewTag.class,632 FindsByFluentSelector.class);633 }634 @Override public String toString() {635 return "By.AndroidViewTag: " + getLocatorString();636 }637 }638}...
By.java
Source:By.java
1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements. See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership. The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License. You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package com.testpros.fast;18import io.appium.java_client.MobileSelector;19/**20 * Mechanism used to locate elements within a document. In order to create your own locating21 * mechanisms, it is possible to subclass this class and override the protected methods as required,22 * though it is expected that all subclasses rely on the basic finding mechanisms provided23 * through static methods of this class:24 *25 * <code>26 * public WebElement findElement(WebDriver driver) {27 * WebElement element = driver.findElement(By.id(getSelector()));28 * if (element == null)29 * element = driver.findElement(By.name(getSelector());30 * return element;31 * }32 * </code>33 */34//public class By extends io.appium.java_client.MobileBy {35//public abstract class By extends org.openqa.selenium.By {36public class By {37 org.openqa.selenium.By by;38 public By(org.openqa.selenium.By by) {39 this.by = by;40 }41 public org.openqa.selenium.By getBy() {42 return by;43 }44 45 /**46 * @param id The value of the "id" attribute to search for.47 * @return A By which locates elements by the value of the "id" attribute.48 */49 public static By id(String id) {50 return new By(org.openqa.selenium.By.id(id));51 }52 /**53 * @param linkText The exact text to match against.54 * @return A By which locates A elements by the exact text it displays.55 */56 public static By linkText(String linkText) {57 return new By(org.openqa.selenium.By.linkText(linkText));58 }59 /**60 * @param partialLinkText The partial text to match against61 * @return a By which locates elements that contain the given link text.62 */63 public static By partialLinkText(String partialLinkText) {64 return new By(org.openqa.selenium.By.partialLinkText(partialLinkText));65 }66 /**67 * @param name The value of the "name" attribute to search for.68 * @return A By which locates elements by the value of the "name" attribute.69 */70 public static By name(String name) {71 return new By(org.openqa.selenium.By.name(name));72 }73 /**74 * @param tagName The element's tag name.75 * @return A By which locates elements by their tag name.76 */77 public static By tagName(String tagName) {78 return new By(org.openqa.selenium.By.tagName(tagName));79 }80 /**81 * @param xpathExpression The XPath to use.82 * @return A By which locates elements via XPath.83 */84 public static By xpath(String xpathExpression) {85 return new By(org.openqa.selenium.By.xpath(xpathExpression));86 }87 /**88 * Find elements based on the value of the "class" attribute. If an element has multiple classes, then89 * this will match against each of them. For example, if the value is "one two onone", then the90 * class names "one" and "two" will match.91 *92 * @param className The value of the "class" attribute to search for.93 * @return A By which locates elements by the value of the "class" attribute.94 */95 public static By className(String className) {96 return new By(org.openqa.selenium.By.className(className));97 }98 /**99 * Find elements via the driver's underlying W3C Selector engine. If the browser does not100 * implement the Selector API, a best effort is made to emulate the API. In this case, we strive101 * for at least CSS2 support, but offer no guarantees.102 *103 * @param cssSelector CSS expression.104 * @return A By which locates elements by CSS.105 */106 public static By cssSelector(String cssSelector) {107 return new By(org.openqa.selenium.By.cssSelector(cssSelector));108 }109 /**110 * Read http://developer.android.com/intl/ru/tools/testing-support-library/111 * index.html#uia-apis112 *113 * @param uiautomatorText is Android UIAutomator string114 * @return an instance of {@link By.ByAndroidUIAutomator}115 */116 public static By AndroidUIAutomator(final String uiautomatorText) {117 return new By(io.appium.java_client.MobileBy.AndroidUIAutomator(uiautomatorText));118 }119 /**120 * About Android accessibility121 * https://developer.android.com/intl/ru/training/accessibility/accessible-app.html122 * About iOS accessibility123 * https://developer.apple.com/library/ios/documentation/UIKit/Reference/124 * UIAccessibilityIdentification_Protocol/index.html125 *126 * @param accessibilityId id is a convenient UI automation accessibility Id.127 * @return an instance of {@link By.ByAndroidUIAutomator}128 */129 public static By AccessibilityId(final String accessibilityId) {130 return new By(io.appium.java_client.MobileBy.AccessibilityId(accessibilityId));131 }132 /**133 * This locator strategy is available in XCUITest Driver mode.134 *135 * @param iOSClassChainString is a valid class chain locator string.136 * See <a href="https://github.com/facebook/WebDriverAgent/wiki/Queries">137 * the documentation</a> for more details138 * @return an instance of {@link By.ByIosClassChain}139 */140 public static By iOSClassChain(final String iOSClassChainString) {141 return new By(io.appium.java_client.MobileBy.iOSClassChain(iOSClassChainString));142 }143 /**144 * This locator strategy is only available in Espresso Driver mode.145 *146 * @param dataMatcherString is a valid class chain locator string.147 * See <a href="https://github.com/appium/appium-espresso-driver/pull/386">148 * the documentation</a> for more details149 * @return an instance of {@link By.ByAndroidDataMatcher}150 */151 public static By androidDataMatcher(final String dataMatcherString) {152 return new By(io.appium.java_client.MobileBy.androidDataMatcher(dataMatcherString));153 }154 /**155 * This locator strategy is available in XCUITest Driver mode.156 *157 * @param iOSNsPredicateString is an an iOS NsPredicate String158 * @return an instance of {@link By.ByIosNsPredicate}159 */160 public static By iOSNsPredicateString(final String iOSNsPredicateString) {161 return new By(io.appium.java_client.MobileBy.iOSNsPredicateString(iOSNsPredicateString));162 }163 public static By windowsAutomation(final String windowsAutomation) {164 return new By(io.appium.java_client.MobileBy.windowsAutomation(windowsAutomation));165 }166 /**167 * This locator strategy is available in Espresso Driver mode.168 *169 * @param tag is an view tag string170 * @return an instance of {@link ByAndroidViewTag}171 * @since Appium 1.8.2 beta172 */173 public static By AndroidViewTag(final String tag) {174 return new By(io.appium.java_client.MobileBy.AndroidViewTag(tag));175 }176 /**177 * This locator strategy is available only if OpenCV libraries and178 * NodeJS bindings are installed on the server machine.179 *180 * @param b64Template base64-encoded template image string. Supported image formats are the same181 * as for OpenCV library.182 * @return an instance of {@link ByImage}183 * @see <a href="https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/image-comparison.md">184 * The documentation on Image Comparison Features</a>185 * @see <a href="https://github.com/appium/appium-base-driver/blob/master/lib/basedriver/device-settings.js">186 * The settings available for lookup fine-tuning</a>187 * @since Appium 1.8.2188 */189 public static By image(final String b64Template) {190 return new By(io.appium.java_client.MobileBy.image(b64Template));191 }192 /**193 * This type of locator requires the use of the 'customFindModules' capability and a194 * separately-installed element finding plugin.195 *196 * @param selector selector to pass to the custom element finding plugin197 * @return an instance of {@link ByCustom}198 * @since Appium 1.9.2199 */200 public static By custom(final String selector) {201 return new By(io.appium.java_client.MobileBy.custom(selector));202 }203}...
MobileBy.ByCustom
Using AI Code Generation
1MobileBy.ByCustom byCustom = new MobileBy.ByCustom();2byCustom.using("content-desc").value("SomeValue");3driver.findElement(byCustom);4by_custom = MobileBy.ByCustom()5driver.find_element(by_custom)6let byCustom = new MobileBy.ByCustom();7byCustom.using = "content-desc";8byCustom.value = "SomeValue";9driver.findElement(byCustom);10driver.find_element(by_custom)11driver.findElement(byCustom)12$byCustom = new MobileBy.ByCustom();13$byCustom->using("content-desc")->value("SomeValue");14$driver->findElement($byCustom);15ByCustom byCustom = new ByCustom();16byCustom.using("content-desc").value("SomeValue");17driver.findElement(byCustom);18let byCustom = MobileBy.ByCustom()19driver.findElement(byCustom)20byCustom := MobileBy.ByCustom{}21driver.FindElement(byCustom)
MobileBy.ByCustom
Using AI Code Generation
1MobileBy.ByCustom byCustom = new MobileBy.ByCustom();2byCustom.using("new UiSelector().text(\""+ text +"\")");3byCustom.withAncestor(byCustom.using("new UiSelector().className(\"android.widget.FrameLayout\")"));4WebElement element = driver.findElement(byCustom);5System.out.println("The element is: " + element.getText());6var byCustom = new MobileBy.ByCustom();7byCustom.using("new UiSelector().text(\""+ text +"\")");8byCustom.withAncestor(byCustom.using("new UiSelector().className(\"android.widget.FrameLayout\")"));9var element = driver.findElement(byCustom);10console.log("The element is: " + element.getText());11byCustom = MobileBy.ByCustom()12byCustom.using("new UiSelector().text(\""+ text +"\")")13byCustom.withAncestor(byCustom.using("new UiSelector().className(\"android.widget.FrameLayout\")"))14element = driver.find_element(byCustom)15print("The element is: " + element.text)16byCustom.using("new UiSelector().text(\""+ text +"\")")17byCustom.withAncestor(byCustom.using("new UiSelector().className(\"android.widget.FrameLayout\")"))18element = driver.find_element(byCustom)19$byCustom = new MobileBy.ByCustom();20$byCustom->using("new UiSelector().text(\""+ $text +"\")");21$byCustom->withAncestor($byCustom->using("new UiSelector().className(\"android.widget.FrameLayout\")"));22$element = $driver->findElement($byCustom);23echo "The element is: " + $element->getText();24byCustom = new MobileBy.ByCustom()25byCustom.using("new UiSelector().text(\""+
MobileBy.ByCustom
Using AI Code Generation
1MobileBy.ByCustom byCustom = new MobileBy.ByCustom();2byCustom.setCustomId("myCustomId");3driver.findElement(byCustom);4driver.find_element(by_custom)5by_custom = MobileBy.ByCustom()6driver.find_element(by_custom)7const byCustom = new MobileBy.ByCustom();8byCustom.customId = 'myCustomId';9driver.findElement(byCustom);10MobileBy.ByCustom byCustom = new MobileBy.ByCustom();11byCustom.CustomId = "myCustomId";12driver.FindElement(byCustom);13byCustom := MobileBy.ByCustom()14driver.FindElement(byCustom)15$byCustom = new MobileBy::ByCustom();16$byCustom->customId = 'myCustomId';17$driver->findElement($byCustom);18let byCustom = MobileBy.ByCustom()19driver.findElement(byCustom)20driver.find_element(by_custom)21by_custom = MobileBy.ByCustom()22driver.find_element(by_custom)
MobileBy.ByCustom
Using AI Code Generation
1MobileBy.ByCustom byCustom = new MobileBy.ByCustom();2byCustom.using("custom").value("custom");3Appium::Core::Android::By.new.custom("custom")4from appium.webdriver.common.mobileby import By5By.CUSTOM("custom")6const MobileBy = require('appium').MobileBy;7MobileBy.ByCustom('custom');8import { MobileBy } from 'appium';9MobileBy.ByCustom('custom');10import { MobileBy } from 'appium';11MobileBy.ByCustom('custom');12$byCustom = By::custom('custom');13By byCustom = By.Custom("custom");14let byCustom = By.custom("custom")15val byCustom = By.custom("custom")16Appium::Core::Android::By.new.custom("custom")17from appium.webdriver.common.mobileby import By18By.CUSTOM("custom")19const { By } = require('appium');20By.custom('custom');21import { By } from 'appium';22By.custom('custom');23import {
MobileBy.ByCustom
Using AI Code Generation
1MobileBy.ByCustom byCustom = new MobileBy.ByCustom();2byCustom.setCustomId("com.example.android.contactmanager:id/contactNameEditText");3MobileElement element = (MobileElement) driver.findElement(byCustom);4by_custom = MobileBy.ByCustom()5by_custom.set_custom_id("com.example.android.contactmanager:id/contactNameEditText")6element = self.driver.find_element(by_custom)7element = find_element(by_custom)8const byCustom = new appium.MobileBy.ByCustom();9byCustom.setCustomId('com.example.android.contactmanager:id/contactNameEditText');10const element = await driver.findElement(byCustom);11const byCustom = new MobileBy.ByCustom();12byCustom.setCustomId('com.example.android.contactmanager:id/contactNameEditText');13const element = await driver.findElement(byCustom);14const byCustom = new MobileBy.ByCustom();15byCustom.setCustomId('com.example.android.contactmanager:id/contactNameEditText');16const element = await driver.findElement(byCustom);17const byCustom = new MobileBy.ByCustom();18byCustom.setCustomId('com.example.android.contactmanager:id/contactNameEditText');19const element = await driver.findElement(byCustom);20const byCustom = new MobileBy.ByCustom();21byCustom.setCustomId('com.example.android.contactmanager:id/contactNameEditText');22const element = await driver.findElement(byCustom);23const byCustom = new MobileBy.ByCustom();24byCustom.setCustomId('com
MobileBy.ByCustom
Using AI Code Generation
1MobileBy.ByCustom byCustom = new MobileBy.ByCustom();2byCustom.setCustomId("com.example:id/myId");3WebElement element = driver.findElement(byCustom);4MobileBy.ByCustom byCustom = new MobileBy.ByCustom();5byCustom.setCustomId("com.example:id/myId");6WebElement element = driver.findElement(byCustom);7MobileBy.ByCustom byCustom = new MobileBy.ByCustom();8byCustom.setCustomId("com.example:id/myId");9WebElement element = driver.findElement(byCustom);10MobileBy.ByCustom byCustom = new MobileBy.ByCustom();11byCustom.setCustomId("com.example:id/myId");12WebElement element = driver.findElement(byCustom);13MobileBy.ByCustom byCustom = new MobileBy.ByCustom();14byCustom.setCustomId("com.example:id/myId");15WebElement element = driver.findElement(byCustom);16MobileBy.ByIosUIAutomation byIosUIAutomation = new MobileBy.ByIosUIAutomation();17byIosUIAutomation.setIosUIAutomation("UIAutomationString");18WebElement element = driver.findElement(byIosUIAutomation);19MobileBy.ByIosUIAutomation byIosUIAutomation = new MobileBy.ByIosUIAutomation();20byIosUIAutomation.setIosUIAutomation("UIAutomationString");21WebElement element = driver.findElement(byIosUIAutomation);22MobileBy.ByIosUIAutomation byIosUIAutomation = new MobileBy.ByIosUIAutomation();23byIosUIAutomation.setIosUIAutomation("UIAutomationString");24WebElement element = driver.findElement(byIosUIAutomation);25MobileBy.ByIosUIAutomation byIosUIAutomation = new MobileBy.ByIosUIAutomation();
MobileBy.ByCustom
Using AI Code Generation
1import io.appium.java_client.MobileBy;2MobileBy.ByCustom by = new MobileBy.ByCustom();3by.setCustomId("com.android.calculator2:id/digit_1");4MobileElement one = driver.findElement(by);5using OpenQA.Selenium.Appium;6MobileBy.ByCustom by = new MobileBy.ByCustom();7by.SetCustomId("com.android.calculator2:id/digit_1");8MobileElement one = driver.FindElement(by);9from appium.webdriver import MobileBy10by = MobileBy.ByCustom()11by.SetCustomId("com.android.calculator2:id/digit_1")12one = driver.find_element(by)13by.SetCustomId("com.android.calculator2:id/digit_1")14one = driver.find_element(by)15const appium = require('appium');16const MobileBy = appium.MobileBy;17const by = new MobileBy.ByCustom();18by.SetCustomId("com.android.calculator2:id/digit_1");19const one = driver.findElement(by);20import "github.com/appium/appium-base-driver"21by := MobileBy.ByCustom{}22by.SetCustomId("com.android.calculator2:id/digit_1")23one := driver.FindElement(by)24use Facebook\WebDriver\Remote\RemoteWebDriver;25use Facebook\WebDriver\Remote\DesiredCapabilities;26use Facebook\WebDriver\Remote\RemoteWebElement;27use Facebook\WebDriver\WebDriverBy;28use Facebook\WebDriver\WebDriverDimension;29use Facebook\WebDriver\WebDriverPoint;30use Facebook\WebDriver\WebDriverTimeouts;31use Facebook\WebDriver\WebDriverWindow;32use Facebook\WebDriver\Appium\WebDriverBy;33use Facebook\WebDriver\Appium\WebDriverElement;
MobileBy.ByCustom
Using AI Code Generation
1MobileBy.ByCustom byCustom = new MobileBy.ByCustom();2byCustom.setStrategy("accessibility id");3byCustom.addSelector("name", "login");4byCustom.addSelector("name", "login");5driver.findElement(byCustom);6from appium.webdriver.common.mobileby import MobileBy7by_custom = MobileBy()8by_custom.set_strategy("accessibility id")9by_custom.add_selector("name", "login")10by_custom.add_selector("name", "login")11driver.find_element(by_custom)12from appium.webdriver.common.mobileby import MobileBy13from appium import webdriver14desired_caps = {}15by_custom = MobileBy()16by_custom.set_strategy("accessibility id")17by_custom.add_selector("name", "login")18by_custom.add_selector("name", "login")19driver.find_element(by_custom)20import io.appium.java_client.MobileBy;21import io.appium.java_client.android.AndroidDriver;22import org.openqa.selenium.remote.DesiredCapabilities;23import java.net.MalformedURLException;24import java.net.URL;25public class MobileByCustomExample {26 public static void main(String[] args) throws MalformedURLException {27 DesiredCapabilities caps = new DesiredCapabilities();28 caps.setCapability("platformName", "Android");29 caps.setCapability("platformVersion", "9");30 caps.setCapability("deviceName", "Pixel 2");31 caps.setCapability("automationName", "UiAutomator2");32 caps.setCapability("app", "path/to/the/downloaded/app.apk");33 MobileBy.ByCustom byCustom = new MobileBy.ByCustom();34 byCustom.setStrategy("accessibility id");35 byCustom.addSelector("name", "login");
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!