Best io.appium code snippet using io.appium.java_client.AppiumBy.ByClassName
AppiumBy.java
Source:AppiumBy.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.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}...
Strategies.java
Source:Strategies.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.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}...
AppiumBy.ByClassName
Using AI Code Generation
1package appium.java;2import java.net.MalformedURLException;3import java.net.URL;4import java.util.concurrent.TimeUnit;5import org.openqa.selenium.By;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.remote.DesiredCapabilities;8import io.appium.java_client.AppiumDriver;9import io.appium.java_client.MobileElement;10import io.appium.java_client.android.AndroidDriver;11import io.appium.java_client.android.AndroidKeyCode;12import io.appium.java_client.android.AndroidKeyMetastate;13import io.appium.java_client.remote.MobileCapabilityType;14import io.appium.java_client.service.local.AppiumDriverLocalService;15import io.appium.java_client.service.local.AppiumServiceBuilder;16import io.appium.java_client.service.local.flags.GeneralServerFlag;17public class AppiumByClassName {18 public static AppiumDriverLocalService service;19 public static String appiumServiceUrl;20 public static void startAppiumServer() throws Exception {21 DesiredCapabilities cap = new DesiredCapabilities();22 cap.setCapability("noReset", false);23 service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()24 .usingDriverExecutable(new File("C:\\Program Files\\nodejs\\node.exe"))25 .withAppiumJS(new File("C:\\Users\\girishm\\AppData\\Roaming\\npm\\node_modules\\appium\\build\\lib\\main.js"))26 .withIPAddress("
AppiumBy.ByClassName
Using AI Code Generation
1public class AppiumBy extends By {2 public static By ByClassName(String className) {3 return new ByClassName(className);4 }5}6public class AppiumBy extends By {7 public static By ByClassName(String className) {8 return new ByClassName(className);9 }10}11public class AppiumBy extends By {12 public static By ByClassName(String className) {13 return new ByClassName(className);14 }15}16public class AppiumBy extends By {17 public static By ByClassName(String className) {18 return new ByClassName(className);19 }20}21public class AppiumBy extends By {22 public static By ByClassName(String className) {23 return new ByClassName(className);24 }25}26public class AppiumBy extends By {27 public static By ByClassName(String className) {28 return new ByClassName(className);29 }30}31public class AppiumBy extends By {32 public static By ByClassName(String className) {33 return new ByClassName(className);34 }35}36public class AppiumBy extends By {37 public static By ByClassName(String className) {38 return new ByClassName(className);39 }40}41public class AppiumBy extends By {42 public static By ByClassName(String className) {43 return new ByClassName(className);44 }45}46public class AppiumBy extends By {47 public static By ByClassName(String className) {
AppiumBy.ByClassName
Using AI Code Generation
1WebElement element = driver.findElement(AppiumBy.ByClassName("android.widget.TextView"));2WebElement element = driver.findElement(AppiumBy.ByAccessibilityId("Content"));3WebElement element = driver.findElement(AppiumBy.ById("android:id/text1"));4WebElement element = driver.findElement(AppiumBy.ByLinkText("Accessibility"));5WebElement element = driver.findElement(AppiumBy.ByPartialLinkText("Acces"));6WebElement element = driver.findElement(AppiumBy.ByName("Animation"));7WebElement element = driver.findElement(AppiumBy.ByTagName("android.widget.TextView"));8WebElement element = driver.findElement(AppiumBy.ByAndroidUIAutomator("new UiSelector().text(\"Animation\")"));9WebElement element = driver.findElement(AppiumBy.ByIosUIAutomation(".elements()[\"Animation\"]"));
AppiumBy.ByClassName
Using AI Code Generation
1package appium.java;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.remote.DesiredCapabilities;7import org.openqa.selenium.support.ui.ExpectedConditions;8import org.openqa.selenium.support.ui.WebDriverWait;9import io.appium.java_client.AppiumDriver;10import io.appium.java_client.AppiumElement;11import io.appium.java_client.MobileBy;12import io.appium.java_client.MobileElement;13import io.appium.java_client.android.AndroidDriver;14import io.appium.java_client.android.AndroidElement;15import io.appium.java_client.pagefactory.AppiumFieldDecorator;16import io.appium.java_client.remote.MobileCapabilityType;17import io.appium.java_client.service.local.AppiumDriverLocalService;18import io.appium.java_client.service.local.AppiumServiceBuilder;19import io.appium.java_client.service.local.flags.GeneralServerFlag;20import io.appium.java_client.service.local.flags.ServerArgument;21import java.io.File;22import java.io.IOException;23import java.net.URL;24import java.util.concurrent.TimeUnit;25import org.openqa.selenium.remote.DesiredCapabilities;26import org.openqa.selenium.remote.RemoteWebDriver;27public class AppiumByClassName {28 public static void main(String[] args) throws Exception {29 AppiumDriverLocalService service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()30 .usingDriverExecutable(new File("C:\\Program Files\\nodejs\\node.exe"))31 .withAppiumJS(new File("C:\\Users\\User\\AppData\\Roaming\\npm\\node_modules\\appium\\build\\lib\\main.js"))32 .withIPAddress("
AppiumBy.ByClassName
Using AI Code Generation
1WebElement element = driver.findElement(By.className("android.widget.TextView"));2WebElement element = driver.findElement(By.AccessibilityId("AccessibilityId"));3WebElement element = driver.findElement(By.AndroidUIAutomator("UiSelector().text(\"AccessibilityId\")"));4WebElement element = driver.findElement(By.IosUIAutomation("UiSelector().text(\"AccessibilityId\")"));5WebElement element = driver.findElement(By.image("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg"));6WebElement element = driver.findElement(By.WindowsAutomation("UiSelector().text(\"AccessibilityId\")"));7WebElement element = driver.findElement(By.AndroidViewTag("AccessibilityId"));8WebElement element = driver.findElement(By.AndroidDataMatcher("AccessibilityId"));9WebElement element = driver.findElement(By.AndroidUIAutomator("UiSelector().text(\"AccessibilityId\")"));10WebElement element = driver.findElement(By.IosUIAutomation("UiSelector().text(\"AccessibilityId\")"));11WebElement element = driver.findElement(By.image("C:\\Users\\Public\\Pictures\\SamplebPiitures\\Desert.jpg"));12import io.appium.java_client.MobileElement;13WebElement element = driver.findElement(By.WindowsAutomation("UiSelector().text(\"AccessibilityId\")"));14WebElement element = driver.findElement(By.AndroidViewTag("AccessibilityId"));15port io.appium.java_client.androilement(By.AndroidDataMatcher("AccessibidityId"));16import io.appium.java_client.android.AndroidElement;17import io.appium.java_client.pagefactory.AppiumFieldDecorator;18import io.appium.java_client.remote.MobileCapabilityType;19import io.appium.java_client.service.local.AppiumDriverLocalService;20import io.appium.java_client.service.local.AppiumServiceBuilder;21import io.appium.java_client.service.local.flags.GeneralServerFlag;22import io.appium.java_client.service.local.flags.ServerArgument;23import java.io.File;24import java.io.IOException;25import java.net.URL;26import java.util.concurrent.TimeUnit;27import org.openqa.selenium.remote.DesiredCapabilities;28import org.openqa.selenium.remote.RemoteWebDriver;29public class AppiumByClassName {30 public static void main(String[] args) throws Exception {31 AppiumDriverLocalService service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()32 .usingDriverExecutable(new File("C:\\Program Files\\nodejs\\node.exe"))33 .withAppiumJS(new File("C:\\Users\\User\\AppData\\Roaming\\npm\\node_modules\\appium\\build\\lib\\main.js"))34 .withIPAddress("
AppiumBy.ByClassName
Using AI Code Generation
1WebElement element = driver.findElement(By.className("android.widget.TextView"));2WebElement element = driver.findElement(By.AccessibilityId("AccessibilityId"));3WebElement element = driver.findElement(By.AndroidUIAutomator("UiSelector().text(\"AccessibilityId\")"));4WebElement element = driver.findElement(By.IosUIAutomation("UiSelector().text(\"AccessibilityId\")"));5WebElement element = driver.findElement(By.image("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg"));6WebElement element = driver.findElement(By.WindowsAutomation("UiSelector().text(\"AccessibilityId\")"));7WebElement element = driver.findElement(By.AndroidViewTag("AccessibilityId"));8WebElement element = driver.findElement(By.AndroidDataMatcher("AccessibilityId"));
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!!