How to use AppiumBy.ById class of io.appium.java_client package

Best io.appium code snippet using io.appium.java_client.AppiumBy.ById

AppiumBy.java

Source:AppiumBy.java Github

copy

Full Screen

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.Getter;18import org.apache.commons.lang3.Validate;19import org.openqa.selenium.By;20import org.openqa.selenium.By.Remotable;21import org.openqa.selenium.SearchContext;22import org.openqa.selenium.WebElement;23import java.io.Serializable;24import java.util.List;25public abstract class AppiumBy extends By implements Remotable {26 @Getter private final Parameters remoteParameters;27 private final String locatorName;28 protected AppiumBy(String selector, String locatorString, String locatorName) {29 Validate.notBlank(locatorString, "Must supply a not empty locator value.");30 this.remoteParameters = new Parameters(selector, locatorString);31 this.locatorName = locatorName;32 }33 @Override public List<WebElement> findElements(SearchContext context) {34 return context.findElements(this);35 }36 @Override public WebElement findElement(SearchContext context) {37 return context.findElement(this);38 }39 @Override public String toString() {40 return String.format("%s.%s: %s", AppiumBy.class.getSimpleName(), locatorName, remoteParameters.value());41 }42 /**43 * About Android accessibility44 * https://developer.android.com/intl/ru/training/accessibility/accessible-app.html45 * About iOS accessibility46 * https://developer.apple.com/library/ios/documentation/UIKit/Reference/47 * UIAccessibilityIdentification_Protocol/index.html48 * @param accessibilityId id is a convenient UI automation accessibility Id.49 * @return an instance of {@link AppiumBy.ByAndroidUIAutomator}50 */51 public static By accessibilityId(final String accessibilityId) {52 return new ByAccessibilityId(accessibilityId);53 }54 /**55 * This locator strategy is only available in Espresso Driver mode.56 * @param dataMatcherString is a valid json string detailing hamcrest matcher for Espresso onData().57 * See <a href="http://appium.io/docs/en/writing-running-appium/android/espresso-datamatcher-selector/">58 * the documentation</a> for more details59 * @return an instance of {@link AppiumBy.ByAndroidDataMatcher}60 */61 public static By androidDataMatcher(final String dataMatcherString) {62 return new ByAndroidDataMatcher(dataMatcherString);63 }64 /**65 * Refer to https://developer.android.com/training/testing/ui-automator66 * @param uiautomatorText is Android UIAutomator string67 * @return an instance of {@link AppiumBy.ByAndroidUIAutomator}68 */69 public static By androidUIAutomator(final String uiautomatorText) {70 return new ByAndroidUIAutomator(uiautomatorText);71 }72 /**73 * This locator strategy is only available in Espresso Driver mode.74 * @param viewMatcherString is a valid json string detailing hamcrest matcher for Espresso onView().75 * See <a href="http://appium.io/docs/en/writing-running-appium/android/espresso-datamatcher-selector/">76 * the documentation</a> for more details77 * @return an instance of {@link AppiumBy.ByAndroidViewMatcher}78 */79 public static By androidViewMatcher(final String viewMatcherString) {80 return new ByAndroidViewMatcher(viewMatcherString);81 }82 /**83 * This locator strategy is available in Espresso Driver mode.84 * @since Appium 1.8.2 beta85 * @param tag is a view tag string86 * @return an instance of {@link ByAndroidViewTag}87 */88 public static By androidViewTag(final String tag) {89 return new ByAndroidViewTag(tag);90 }91 /**92 * For IOS it is the full name of the XCUI element and begins with XCUIElementType.93 * For Android it is the full name of the UIAutomator2 class (e.g.: android.widget.TextView)94 * @param selector the class name of the element95 * @return an instance of {@link ByClassName}96 */97 public static By className(final String selector) {98 return new ByClassName(selector);99 }100 /**101 * For IOS the element name.102 * For Android it is the resource identifier.103 * @param selector element id104 * @return an instance of {@link ById}105 */106 public static By id(final String selector) {107 return new ById(selector);108 }109 /**110 * For IOS the element name.111 * For Android it is the resource identifier.112 * @param selector element id113 * @return an instance of {@link ByName}114 */115 public static By name(final String selector) {116 return new ByName(selector);117 }118 /**119 * This type of locator requires the use of the 'customFindModules' capability and a120 * separately-installed element finding plugin.121 *122 * @param selector selector to pass to the custom element finding plugin123 * @return an instance of {@link ByCustom}124 * @since Appium 1.9.2125 */126 public static By custom(final String selector) {127 return new ByCustom(selector);128 }129 /**130 * This locator strategy is available only if OpenCV libraries and131 * NodeJS bindings are installed on the server machine.132 *133 * @see <a href="https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/image-comparison.md">134 * The documentation on Image Comparison Features</a>135 * @see <a href="https://github.com/appium/appium-base-driver/blob/master/lib/basedriver/device-settings.js">136 * The settings available for lookup fine-tuning</a>137 * @since Appium 1.8.2138 * @param b64Template base64-encoded template image string. Supported image formats are the same139 * as for OpenCV library.140 * @return an instance of {@link ByImage}141 */142 public static By image(final String b64Template) {143 return new ByImage(b64Template);144 }145 /**146 * This locator strategy is available in XCUITest Driver mode.147 * @param iOSClassChainString is a valid class chain locator string.148 * See <a href="https://github.com/facebookarchive/WebDriverAgent/wiki/Class-Chain-Queries-Construction-Rules">149 * the documentation</a> for more details150 * @return an instance of {@link AppiumBy.ByIosClassChain}151 */152 public static By iOSClassChain(final String iOSClassChainString) {153 return new ByIosClassChain(iOSClassChainString);154 }155 /**156 * This locator strategy is available in XCUITest Driver mode.157 * @param iOSNsPredicateString is an iOS NsPredicate String158 * @return an instance of {@link AppiumBy.ByIosNsPredicate}159 */160 public static By iOSNsPredicateString(final String iOSNsPredicateString) {161 return new ByIosNsPredicate(iOSNsPredicateString);162 }163 public static class ByAccessibilityId extends AppiumBy implements Serializable {164 public ByAccessibilityId(String accessibilityId) {165 super("accessibility id", accessibilityId, "accessibilityId");166 }167 }168 public static class ByAndroidDataMatcher extends AppiumBy implements Serializable {169 protected ByAndroidDataMatcher(String locatorString) {170 super("-android datamatcher", locatorString, "androidDataMatcher");171 }172 }173 public static class ByAndroidUIAutomator extends AppiumBy implements Serializable {174 public ByAndroidUIAutomator(String uiautomatorText) {175 super("-android uiautomator", uiautomatorText, "androidUIAutomator");176 }177 }178 public static class ByAndroidViewMatcher extends AppiumBy implements Serializable {179 protected ByAndroidViewMatcher(String locatorString) {180 super("-android viewmatcher", locatorString, "androidViewMatcher");181 }182 }183 public static class ByAndroidViewTag extends AppiumBy implements Serializable {184 public ByAndroidViewTag(String tag) {185 super("-android viewtag", tag, "androidViewTag");186 }187 }188 public static class ById extends AppiumBy implements Serializable {189 protected ById(String selector) {190 super("id", selector, "id");191 }192 }193 public static class ByName extends AppiumBy implements Serializable {194 protected ByName(String selector) {195 super("name", selector, "name");196 }197 }198 public static class ByClassName extends AppiumBy implements Serializable {199 protected ByClassName(String selector) {200 super("class name", selector, "className");201 }202 }203 public static class ByCustom extends AppiumBy implements Serializable {204 protected ByCustom(String selector) {205 super("-custom", selector, "custom");206 }207 }208 public static class ByImage extends AppiumBy implements Serializable {209 protected ByImage(String b64Template) {210 super("-image", b64Template, "image");211 }212 }213 public static class ByIosClassChain extends AppiumBy implements Serializable {214 protected ByIosClassChain(String locatorString) {215 super("-ios class chain", locatorString, "iOSClassChain");216 }217 }218 public static class ByIosNsPredicate extends AppiumBy implements Serializable {219 protected ByIosNsPredicate(String locatorString) {220 super("-ios predicate string", locatorString, "iOSNsPredicate");221 }222 }223}...

Full Screen

Full Screen

Strategies.java

Source:Strategies.java Github

copy

Full Screen

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.pagefactory.bys.builder;17import java.lang.annotation.Annotation;18import java.lang.reflect.InvocationTargetException;19import java.lang.reflect.Method;20import java.util.List;21import java.util.stream.Collectors;22import java.util.stream.Stream;23import org.openqa.selenium.By;24import io.appium.java_client.AppiumBy;25import io.appium.java_client.MobileBy;26import io.appium.java_client.pagefactory.AndroidBy;27import io.appium.java_client.pagefactory.AndroidFindBy;28enum Strategies {29 BYUIAUTOMATOR("uiAutomator") {30 @Override By getBy(Annotation annotation) {31 String value = getValue(annotation, this);32 if (annotation.annotationType().equals(AndroidFindBy.class)33 || annotation.annotationType().equals(AndroidBy.class)) {34 return AppiumBy.androidUIAutomator(value);35 }36 return super.getBy(annotation);37 }38 },39 BYACCESSABILITY("accessibility") {40 @Override By getBy(Annotation annotation) {41 return AppiumBy.accessibilityId(getValue(annotation, this));42 }43 },44 BYCLASSNAME("className") {45 @Override By getBy(Annotation annotation) {46 return AppiumBy.className(getValue(annotation, this));47 }48 },49 BYID("id") {50 @Override By getBy(Annotation annotation) {51 return AppiumBy.id(getValue(annotation, this));52 }53 },54 BYTAG("tagName") {55 @Override By getBy(Annotation annotation) {56 return By.tagName(getValue(annotation, this));57 }58 },59 BYNAME("name") {60 @Override By getBy(Annotation annotation) {61 return AppiumBy.name(getValue(annotation, this));62 }63 },64 BYXPATH("xpath") {65 @Override By getBy(Annotation annotation) {66 return By67 .xpath(getValue(annotation, this));68 }69 },70 BYLINKTEXT("linkText") {71 @Override By getBy(Annotation annotation) {72 return By73 .linkText(getValue(annotation, this));74 }75 },76 BYPARTIALLINKTEXT("partialLinkText") {77 @Override By getBy(Annotation annotation) {78 return By79 .partialLinkText(getValue(annotation, this));80 }81 },82 /**83 * The Windows UIAutomation strategy.84 * @deprecated Not supported on the server side.85 */86 @Deprecated87 BYWINDOWSAUTOMATION("windowsAutomation") {88 @Override By getBy(Annotation annotation) {89 return MobileBy.windowsAutomation(getValue(annotation, this));90 }91 },92 BY_CLASS_CHAIN("iOSClassChain") {93 @Override By getBy(Annotation annotation) {94 return AppiumBy95 .iOSClassChain(getValue(annotation, this));96 }97 },98 BY_DATA_MATCHER("androidDataMatcher") {99 @Override By getBy(Annotation annotation) {100 return AppiumBy101 .androidDataMatcher(getValue(annotation, this));102 }103 },104 BY_VIEW_MATCHER("androidViewMatcher") {105 @Override By getBy(Annotation annotation) {106 return AppiumBy107 .androidViewMatcher(getValue(annotation, this));108 }109 },110 BY_NS_PREDICATE("iOSNsPredicate") {111 @Override By getBy(Annotation annotation) {112 return AppiumBy113 .iOSNsPredicateString(getValue(annotation, this));114 }115 };116 private final String valueName;117 Strategies(String valueName) {118 this.valueName = valueName;119 }120 static List<String> strategiesNames() {121 return Stream.of(values()).map((s) -> s.valueName).collect(Collectors.toList());122 }123 private static String getValue(Annotation annotation, Strategies strategy) {124 try {125 Method m = annotation.getClass()126 .getMethod(strategy.valueName, AppiumByBuilder.DEFAULT_ANNOTATION_METHOD_ARGUMENTS);127 return m.invoke(annotation).toString();128 } catch (NoSuchMethodException129 | SecurityException130 | IllegalAccessException131 | IllegalArgumentException132 | InvocationTargetException e) {133 throw new RuntimeException(e);134 }135 }136 String returnValueName() {137 return valueName;138 }139 By getBy(Annotation annotation) {140 return null;141 }142}...

Full Screen

Full Screen

AppiumBy.ById

Using AI Code Generation

copy

Full Screen

1import io.appium.java_client.AppiumDriver;2import io.appium.java_client.AppiumElement;3import io.appium.java_client.MobileElement;4import io.appium.java_client.android.AndroidDriver;5import io.appium.java_client.android.AndroidElement;6import io.appium.java_client.ios.IOSDriver;7import io.appium.java_client.ios.IOSElement;8import io.appium.java_client.pagefactory.AppiumFieldDecorator;9import io.appium.java_client.remote.MobileCapabilityType;10import io.appium.java_client.service.local.AppiumDriverLocalService;11import io.appium.java_client.service.local.AppiumServiceBuilder;12import io.appium.java_client.service.local.flags.GeneralServerFlag;13import io.appium.java_client.service.local.flags.ServerArgument;14import org.openqa.selenium.By;15import org.openqa.selenium.WebElement;16import org.openqa.selenium.remote.DesiredCapabilities;17import org.openqa.selenium.support.PageFactory;18import org.testng.annotations.AfterTest;19import org.testng.annotations.BeforeTest;20import org.testng.annotations.Test;21import java.io.File;22import java.net.MalformedURLException;23import java.net.URL;24import java.util.concurrent.TimeUnit;25import static io.appium.java_client.pagefactory.LocatorGroupStrategy.ALL_POSSIBLE;26import static io.appium.java_client.pagefactory.LocatorGroupStrategy.CHAIN;27import static io.appium.java_client.pagefactory.LocatorGroupStrategy.FIRST_FOUND;28import static io.appium.java_client.pagefactory.LocatorGroupStrategy.ORDERED;29import static io.appium.java_client.pagefactory.LocatorGroupStrategy.PARTIAL_TEXT;30import static io.appium.java_client.pagefactory.LocatorGroupStrategy.TEXT;31import static io.appium.java_client.pagefactory.LocatorGroupStrategy.UNIQUE;32import static io.appium.java_client.pagefactory.LocatorGroupStrategy.UNORDERED;33import static io.appium.java_client.pagefactory.LocatorGroupStrategy.VISIBLE;34public class AppiumByTest {35 private AppiumDriverLocalService service;36 private AppiumDriver driver;37 public void beforeTest() throws MalformedURLException {38 service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()39 .usingDriverExecutable(new File("C:\\Program Files\\nodejs\\node.exe"))40 .withAppiumJS(new File("C:\\Users\\admin\\AppData\\Roaming\\npm\\node_modules\\appium\\build\\lib\\main.js"))41 .withIPAddress("

Full Screen

Full Screen

AppiumBy.ById

Using AI Code Generation

copy

Full Screen

1WebElement element = driver.findElement(new AppiumBy.ById("id"));2WebElement element = driver.findElement(new AppiumBy.ByAccessibilityId("id"));3WebElement element = driver.findElement(new AppiumBy.ByIosUIAutomation("id"));4WebElement element = driver.findElement(new AppiumBy.ByAndroidUIAutomator("id"));5WebElement element = driver.findElement(new AppiumBy.ByAndroidViewTag("id"));6WebElement element = driver.findElement(new AppiumBy.ByAndroidDataMatcher("id"));7WebElement element = driver.findElement(new AppiumBy.ByAndroidClass("id"));8WebElement element = driver.findElement(new AppiumBy.ByAndroidText("id"));9WebElement element = driver.findElement(new AppiumBy.ByAndroidContentDesc("id"));10WebElement element = driver.findElement(new AppiumBy.ByWindowsAutomation("id"));11WebElement element = driver.findElement(new AppiumBy.ByWindowsClassName("id"));12WebElement element = driver.findElement(new AppiumBy.ByWindowsName("id"));13WebElement element = driver.findElement(new AppiumBy.ByWindowsId("id"));14WebElement element = driver.findElementByAccessibilityId("id");15WebElement element = driver.findElementByAndroidDataMatcher("id");

Full Screen

Full Screen

AppiumBy.ById

Using AI Code Generation

copy

Full Screen

1package appium.java;2import io.appium.java_client.AppiumDriver;3import io.appium.java_client.MobileElement;4import io.appium.java_client.android.AndroidDriver;5import io.appium.java_client.android.AndroidElement;6import java.net.MalformedURLException;7import java.net.URL;8import org.openqa.selenium.By;9import org.openqa.selenium.remote.DesiredCapabilities;10public class AppiumBy {11 public static void main(String[] args) throws MalformedURLException {12 DesiredCapabilities capabilities = new DesiredCapabilities();13 capabilities.setCapability("deviceName", "Android Emulator");14 capabilities.setCapability("platformName", "Android");15 capabilities.setCapability("platformVersion", "5.1.1");16 capabilities.setCapability("appPackage", "com.example.android.contactmanager");17 capabilities.setCapability("appActivity", ".ContactManager");

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run io.appium automation tests on LambdaTest cloud grid

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful