Best io.appium code snippet using io.appium.java_client.pagefactory.bys.builder.ByChained
DefaultElementByBuilder.java
Source:DefaultElementByBuilder.java
...20import io.appium.java_client.pagefactory.bys.ContentMappedBy;21import io.appium.java_client.pagefactory.bys.ContentType;22import io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder;23import io.appium.java_client.pagefactory.bys.builder.ByAll;24import io.appium.java_client.pagefactory.bys.builder.ByChained;25import io.appium.java_client.pagefactory.bys.builder.HowToUseSelectors;26import org.openqa.selenium.By;27import org.openqa.selenium.support.ByIdOrName;28import org.openqa.selenium.support.CacheLookup;29import org.openqa.selenium.support.FindAll;30import org.openqa.selenium.support.FindBy;31import org.openqa.selenium.support.FindBys;32import java.lang.annotation.Annotation;33import java.lang.reflect.AnnotatedElement;34import java.lang.reflect.Field;35import java.lang.reflect.InvocationTargetException;36import java.lang.reflect.Method;37import java.util.ArrayList;38import java.util.Arrays;39import java.util.Comparator;40import java.util.HashMap;41import java.util.List;42import java.util.Map;43import java.util.Optional;44public class DefaultElementByBuilder extends AppiumByBuilder {45 private static final String PRIORITY = "priority";46 private static final String VALUE = "value";47 private static final Class[] ANNOTATION_ARGUMENTS = new Class[]{};48 private static final Object[] ANNOTATION_PARAMETERS = new Object[]{};49 public DefaultElementByBuilder(String platform, String automation) {50 super(platform, automation);51 }52 private static void checkDisallowedAnnotationPairs(Annotation a1, Annotation a2)53 throws IllegalArgumentException {54 if (a1 != null && a2 != null) {55 throw new IllegalArgumentException(56 "If you use a '@" + a1.getClass().getSimpleName() + "' annotation, "57 + "you must not also use a '@" + a2.getClass().getSimpleName()58 + "' annotation");59 }60 }61 private static By buildMobileBy(LocatorGroupStrategy locatorGroupStrategy, By[] bys) {62 if (bys.length == 0) {63 return null;64 }65 LocatorGroupStrategy strategy = ofNullable(locatorGroupStrategy)66 .orElse(LocatorGroupStrategy.CHAIN);67 if (strategy.equals(LocatorGroupStrategy.ALL_POSSIBLE)) {68 return new ByAll(bys);69 }70 return new ByChained(bys);71 }72 @Override73 protected void assertValidAnnotations() {74 AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();75 FindBy findBy = annotatedElement.getAnnotation(FindBy.class);76 FindBys findBys = annotatedElement.getAnnotation(FindBys.class);77 checkDisallowedAnnotationPairs(findBy, findBys);78 FindAll findAll = annotatedElement.getAnnotation(FindAll.class);79 checkDisallowedAnnotationPairs(findBy, findAll);80 checkDisallowedAnnotationPairs(findBys, findAll);81 }82 @Override83 protected By buildDefaultBy() {84 AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();...
AppiumByBuilder.java
Source:AppiumByBuilder.java
...19import static io.appium.java_client.remote.MobilePlatform.IOS;20import org.openqa.selenium.By;21import org.openqa.selenium.support.pagefactory.AbstractAnnotations;22import org.openqa.selenium.support.pagefactory.ByAll;23import org.openqa.selenium.support.pagefactory.ByChained;24import java.lang.annotation.Annotation;25import java.lang.reflect.AnnotatedElement;26import java.lang.reflect.Constructor;27import java.lang.reflect.InvocationTargetException;28import java.lang.reflect.Method;29import java.util.ArrayList;30import java.util.List;31/**32 * It is the basic handler of Appium-specific page object annotations33 * About the Page Object design pattern please read these documents:34 * - https://code.google.com/p/selenium/wiki/PageObjects35 * - https://code.google.com/p/selenium/wiki/PageFactory36 */37public abstract class AppiumByBuilder extends AbstractAnnotations {38 static final Class<?>[] DEFAULT_ANNOTATION_METHOD_ARGUMENTS = new Class<?>[] {};39 private static final List<String> METHODS_TO_BE_EXCLUDED_WHEN_ANNOTATION_IS_READ =40 new ArrayList<String>() {41 private static final long serialVersionUID = 1L; {42 List<String> objectClassMethodNames =43 getMethodNames(Object.class.getDeclaredMethods());44 addAll(objectClassMethodNames);45 List<String> annotationClassMethodNames =46 getMethodNames(Annotation.class.getDeclaredMethods());47 annotationClassMethodNames.removeAll(objectClassMethodNames);48 addAll(annotationClassMethodNames);49 }50 };51 protected final AnnotatedElementContainer annotatedElementContainer;52 protected final String platform;53 protected final String automation;54 protected AppiumByBuilder(String platform, String automation) {55 this.annotatedElementContainer = new AnnotatedElementContainer();56 this.platform = String.valueOf(platform).toUpperCase().trim();57 this.automation = String.valueOf(automation).toUpperCase().trim();58 }59 private static List<String> getMethodNames(Method[] methods) {60 List<String> names = new ArrayList<>();61 for (Method m : methods) {62 names.add(m.getName());63 }64 return names;65 }66 private static Method[] prepareAnnotationMethods(Class<? extends Annotation> annotation) {67 List<String> targeAnnotationMethodNamesList =68 getMethodNames(annotation.getDeclaredMethods());69 targeAnnotationMethodNamesList.removeAll(METHODS_TO_BE_EXCLUDED_WHEN_ANNOTATION_IS_READ);70 Method[] result = new Method[targeAnnotationMethodNamesList.size()];71 for (String methodName : targeAnnotationMethodNamesList) {72 try {73 result[targeAnnotationMethodNamesList.indexOf(methodName)] =74 annotation.getMethod(methodName, DEFAULT_ANNOTATION_METHOD_ARGUMENTS);75 } catch (NoSuchMethodException | SecurityException e) {76 throw new RuntimeException(e);77 }78 }79 return result;80 }81 private static String getFilledValue(Annotation mobileBy) {82 Method[] values = prepareAnnotationMethods(mobileBy.getClass());83 for (Method value : values) {84 try {85 String strategyParameter = value.invoke(mobileBy, new Object[] {}).toString();86 if (!"".equals(strategyParameter)) {87 return value.getName();88 }89 } catch (IllegalAccessException90 | IllegalArgumentException91 | InvocationTargetException e) {92 throw new RuntimeException(e);93 }94 }95 throw new IllegalArgumentException(96 "@" + mobileBy.getClass().getSimpleName() + ": one of " + Strategies.strategiesNames()97 .toString() + " should be filled");98 }99 private static By getMobileBy(Annotation annotation, String valueName) {100 Strategies[] strategies = Strategies.values();101 for (Strategies strategy : strategies) {102 if (strategy.returnValueName().equals(valueName)) {103 return strategy.getBy(annotation);104 }105 }106 throw new IllegalArgumentException(107 "@" + annotation.getClass().getSimpleName() + ": There is an unknown strategy "108 + valueName);109 }110 @SuppressWarnings("unchecked")111 private static <T extends By> T getComplexMobileBy(Annotation[] annotations,112 Class<T> requiredByClass) {113 By[] byArray = new By[annotations.length];114 for (int i = 0; i < annotations.length; i++) {115 byArray[i] = getMobileBy(annotations[i], getFilledValue(annotations[i]));116 }117 try {118 Constructor<?> c = requiredByClass.getConstructor(By[].class);119 Object[] values = new Object[] {byArray};120 return (T) c.newInstance(values);121 } catch (Exception e) {122 throw new RuntimeException(e);123 }124 }125 protected static By createBy(Annotation[] annotations, HowToUseSelectors howToUseLocators) {126 if (annotations == null || annotations.length == 0) {127 return null;128 }129 switch (howToUseLocators) {130 case USE_ONE: {131 return getMobileBy(annotations[0], getFilledValue(annotations[0]));132 }133 case BUILD_CHAINED: {134 return getComplexMobileBy(annotations, ByChained.class);135 }136 case USE_ANY: {137 return getComplexMobileBy(annotations, ByAll.class);138 }139 default: {140 return null;141 }142 }143 }144 /**145 * This method should be used for the setting up of146 * AnnotatedElement instances before the building of147 * By-locator strategies148 *...
WidgetFieldDecorator.java
Source:WidgetFieldDecorator.java
...20import org.openqa.selenium.support.FindBy;21import org.openqa.selenium.support.FindBys;22import org.openqa.selenium.support.PageFactory;23import org.openqa.selenium.support.pagefactory.Annotations;24import org.openqa.selenium.support.pagefactory.ByChained;25import ui.auto.core.context.PageComponentContext;26import ui.auto.core.data.ComponentData;27import ui.auto.core.data.DataTypes;28import java.lang.reflect.Field;29import java.util.Map;30import static io.appium.java_client.pagefactory.utils.WebDriverUnpackUtility.unpackWebDriverFromSearchContext;31import static java.util.Optional.ofNullable;32public class WidgetFieldDecorator extends AppiumFieldDecorator {33 private PageObject page;34 private PageComponentContext context;35 public WidgetFieldDecorator(PageComponentContext context, PageObject page) {36 super(context.getDriver());37 this.context = context;38 this.page = page;39 }40 @Override41 public Object decorate(ClassLoader ignored, Field field) {42 String dataValue = null;43 String initialValue = null;44 String expectedValue = null;45 Map<String, String> customData = null;46 field.setAccessible(true);47 if (PageComponent.class.isAssignableFrom(field.getType())) {48 String platform = null;49 String automation = null;50 WebDriver webDriver = unpackWebDriverFromSearchContext(this.context.getDriver());51 HasSessionDetails hasSessionDetails = ofNullable(webDriver).map(driver -> {52 if (!HasSessionDetails.class.isAssignableFrom(driver.getClass())) {53 return null;54 }55 return (HasSessionDetails) driver;56 }).orElse(null);57 if (hasSessionDetails != null) {58 platform = hasSessionDetails.getPlatformName();59 automation = hasSessionDetails.getAutomationName();60 }61 DefaultElementByBuilder builder = new DefaultElementByBuilder(platform, automation);62 builder.setAnnotated(field);63 By by = builder.buildBy();64 if (by == null) return null;65 if (page.getLocator() != null) {66 by = new ByChained(page.getLocator(), by);67 }68 ComponentData componentData;69 try {70 componentData = (ComponentData) field.get(page);71 } catch (IllegalArgumentException | IllegalAccessException e) {72 throw new RuntimeException(e);73 }74 if (componentData != null) {75 dataValue = componentData.getData(DataTypes.Data, false);76 initialValue = componentData.getData(DataTypes.Initial, false);77 expectedValue = componentData.getData(DataTypes.Expected, false);78 customData = componentData.getCustomData();79 }80 Enhancer enhancer = new Enhancer();...
ByChained.java
Source:ByChained.java
...22import org.openqa.selenium.TimeoutException;23import org.openqa.selenium.WebElement;24import org.openqa.selenium.support.ui.FluentWait;25import java.util.Optional;26public class ByChained extends org.openqa.selenium.support.pagefactory.ByChained {27 private final By[] bys;28 private static AppiumFunction<SearchContext, WebElement> getSearchingFunction(By by) {29 return input -> {30 try {31 return input.findElement(by);32 } catch (NoSuchElementException e) {33 return null;34 }35 };36 }37 /**38 * Finds elements that matches each of the locators in sequence.39 *40 * @param bys is a set of {@link By} which forms the chain of the searching.41 */42 public ByChained(By[] bys) {43 super(bys);44 checkNotNull(bys);45 if (bys.length == 0) {46 throw new IllegalArgumentException("By array should not be empty");47 }48 this.bys = bys;49 }50 @Override51 public WebElement findElement(SearchContext context) {52 AppiumFunction<SearchContext, WebElement> searchingFunction = null;53 for (By by: bys) {54 searchingFunction = Optional.ofNullable(searchingFunction != null55 ? searchingFunction.andThen(getSearchingFunction(by)) : null).orElse(getSearchingFunction(by));56 }...
SearchBys.java
Source:SearchBys.java
2 * Created by hemanthsridhar on 1/6/19.3 */4import com.github.hemanthsridhar1992.builder.CustomPageFactoryFinder;5import com.github.hemanthsridhar1992.pagefactory.AbstractCustomFindByBuilder;6import io.appium.java_client.pagefactory.bys.builder.ByChained;7import org.openqa.selenium.By;8import java.lang.annotation.ElementType;9import java.lang.annotation.Retention;10import java.lang.annotation.RetentionPolicy;11import java.lang.annotation.Target;12import java.lang.reflect.Field;13@Retention(RetentionPolicy.RUNTIME)14@Target({ElementType.FIELD, ElementType.TYPE})15@CustomPageFactoryFinder(SearchBys.FindBysBuilder.class)16public @interface SearchBys {17 SearchBy[] value();18 class FindBysBuilder extends AbstractCustomFindByBuilder {19 @Override20 public By buildIt(Object annotation, Field field) {21 SearchBys findBys = (SearchBys) annotation;22 SearchBy[] findByArray = findBys.value();23 By[] byArray = new By[findByArray.length];24 for (int i = 0; i < findByArray.length; i++) {25 byArray[i] = buildByFromFindBy(findByArray[i]);26 }27 return new ByChained(byArray);28 }29 }30}...
ByChained
Using AI Code Generation
1ByChained byChained = new ByChained(By.id("com.android.calculator2:id/digit_1"),By.id("com.android.calculator2:id/digit_2"),By.id("com.android.calculator2:id/digit_3"));2WebElement element = driver.findElement(byChained);3element.click();4ByAll byAll = new ByAll(By.id("com.android.calculator2:id/digit_1"),By.id("com.android.calculator2:id/digit_2"),By.id("com.android.calculator2:id/digit_3"));5WebElement element = driver.findElement(byAll);6element.click();7var byChained = new ByChained(By.id("com.android.calculator2:id/digit_1"),By.id("com.android.calculator2:id/digit_2"),By.id("com.android.calculator2:id/digit_3"));8var element = driver.findElement(byChained);9element.click();10var byAll = new ByAll(By.id("com.android.calculator2:id/digit_1"),By.id("com.android.calculator2:id/digit_2"),By.id("com.android.calculator2:id/digit_3"));11var element = driver.findElement(byAll);12element.click();13byChained = ByChained(By.id("com.android.calculator2:id/digit_1"),By.id("com.android.calculator2:id/digit_2"),By.id("com.android.calculator2:id/digit_3"))14element = driver.find_element(byChained)15element.click()16byAll = ByAll(By.id("com.android.calculator2:id/digit_1"),By.id("com.android.calculator2:id/digit_2"),By.id("com.android.calculator2:id/digit_3"))17element = driver.find_element(byAll)18element.click()
ByChained
Using AI Code Generation
1import io.appium.java_client.pagefactory.ByChained;2import org.openqa.selenium.By;3public class ByChainedDemo {4 public static void main(String[] args) {5 System.out.println(by.toString());6 }7}8import io.appium.java_client.pagefactory.ByChained;9import org.openqa.selenium.By;10public class ByChainedDemo {11 public static void main(String[] args) {12 System.out.println(by.toString());13 }14}
ByChained
Using AI Code Generation
1ByChained byChained = new ByChained(By.id("id1"), By.id("id2"), By.id("id3"));2driver.findElement(byChained);3ByAll byAll = new ByAll(By.id("id1"), By.id("id2"), By.id("id3"));4driver.findElement(byAll);5ByAll byAll = new ByAll(By.id("id1"), By.id("id2"), By.id("id3"));6driver.findElement(byAll);7ByAll byAll = new ByAll(By.id("id1"), By.id("id2"), By.id("id3"));8driver.findElement(byAll);9ByAll byAll = new ByAll(By.id("id1"), By.id("id2"), By.id("id3"));10driver.findElement(byAll);11ByAll byAll = new ByAll(By.id("id1"), By.id("id2"), By.id("id3"));12driver.findElement(byAll);13ByAll byAll = new ByAll(By.id("id1"), By.id("id2"), By.id("id3"));14driver.findElement(byAll);15ByAll byAll = new ByAll(By.id("id1"), By.id("id2"), By.id("id3"));16driver.findElement(byAll);17ByAll byAll = new ByAll(By.id("id1"), By.id("id2"), By.id("id3"));18driver.findElement(byAll
ByChained
Using AI Code Generation
1import io.appium.java_client.pagefactory.bys.builder.ByChained;2ByChained by = new ByChained(By.id("com.android.calculator2:id/digit_2"),By.id("com.android.calculator2:id/digit_5"));3driver.findElement(by).click();4import io.appium.java_client.pagefactory.bys.builder.ByAll;5ByAll by = new ByAll(By.id("com.android.calculator2:id/digit_2"),By.id("com.android.calculator2:id/digit_5"));6driver.findElement(by).click();7import io.appium.java_client.pagefactory.bys.builder.ByAny;8ByAny by = new ByAny(By.id("com.android.calculator2:id/digit_2"),By.id("com.android.calculator2:id/digit_5"));9driver.findElement(by).click();10import io.appium.java_client.pagefactory.bys.builder.ByText;11ByText by = new ByText("2");12driver.findElement(by).click();13import io.appium.java_client.pagefactory.bys.builder.ByAndroidUIAutomator;14ByAndroidUIAutomator by = new ByAndroidUIAutomator("new UiSelector().text(\"2\")");15driver.findElement(by).click();16import io.appium.java_client.pagefactory.bys.builder.ByIosUIAutomation;17ByIosUIAutomation by = new ByIosUIAutomation(".textFields()[0]");18driver.findElement(by).click();19import io.appium.java_client.pagefactory.bys.builder.ByIosNsPredicate;20ByIosNsPredicate by = new ByIosNsPredicate("type == 'XCUIElementTypeTextField' AND value == 'my value'");21driver.findElement(by).click();22import io.appium.java_client.pagefactory.b
ByChained
Using AI Code Generation
1By by = new ByChained(By.id("com.android.calculator2:id/digit_1"), By.id("com.android.calculator2:id/digit_2"));2WebElement el = driver.findElement(by);3el.click();4By by = new ByChained(By.id("com.android.calculator2:id/digit_1"), By.id("com.android.calculator2:id/digit_2"));5WebElement el = driver.findElement(by);6el.click();7by.add_id("com.android.calculator2:id/digit_1")8by.add_id("com.android.calculator2:id/digit_2")9el = @driver.find_element(by)10by.add_id("com.android.calculator2:id/digit_1")11by.add_id("com.android.calculator2:id/digit_2")12el = @driver.find_element(by)13by.add_id("com.android.calculator2:id/digit_1")14by.add_id("com.android.calculator2:id/digit_2")15el = @driver.find_element(by)16by.add_id("com.android.calculator2:id/digit_1")17by.add_id("com.android.calculator2:id/digit_2")18el = @driver.find_element(by)19by.add_id("com.android.calculator2:id/digit_1")20by.add_id("com.android.calculator2:id/digit_2")21el = @driver.find_element(by)
ByChained
Using AI Code Generation
1ByChained byChained = new ByChained(By.id("com.androidsample.generalstore:id/nameField"),By.className("android.widget.EditText"));2driver.findElement(byChained).sendKeys("Hello");3from appium.webdriver.common.mobileby import MobileBy4by_chained = MobileBy.ACCESSIBILITY_ID('Accessibility')5driver.find_element(by_chained, "Accessibility")6opts = {7 caps: {8 }9}10Appium::Driver.new(opts, true)11by_chained = Appium::Core::Android::Device.find_element_by_android_uiautomator('new UiSelector().text("Accessibility")')12find_element(by_chained, "Accessibility")13const {By, ByChained} = require('appium-support');14const byChained = new ByChained(By.id("com.androidsample.generalstore:id/nameField"),By.className("android.widget.EditText"));15driver.findElement(byChained).sendKeys("Hello");16using OpenQA.Selenium.Appium;17using OpenQA.Selenium.Appium.Android;18using OpenQA.Selenium.Appium.MultiTouch;19using OpenQA.Selenium.Appium.Service;20using OpenQA.Selenium.Appium.Service.Options;21using OpenQA.Selenium.Remote;22using OpenQA.Selenium.Support.UI;23using System;24using System.Collections.Generic;25using System.Diagnostics;26using System.Threading;27using System.Threading.Tasks;
ByChained
Using AI Code Generation
1public By getLocator(String locatorName) throws Exception {2 String[] locator = locatorName.split("::");3 String locatorType = locator[0];4 String locatorValue = locator[1];5 By by = null;6 switch (locatorType) {7 by = By.id(locatorValue);8 break;9 by = By.name(locatorValue);10 break;11 by = By.cssSelector(locatorValue);12 break;13 by = By.xpath(locatorValue);14 break;15 by = By.linkText(locatorValue);16 break;17 by = By.partialLinkText(locatorValue);18 break;19 by = By.tagName(locatorValue);20 break;21 by = By.className(locatorValue);22 break;23 by = getChainedLocator(locatorValue);24 break;25 throw new Exception("Locator type '" + locatorType + "' not defined!!");26 }27 return by;28}29public By getChainedLocator(String locatorValue) throws Exception {30 String[] locator = locatorValue.split(",");31 By[] by = new By[locator.length];32 for (int i = 0; i < locator.length; i++) {33 by[i] = getLocator(locator[i]);34 }35 return new ByChained(by);36}37public void click(String locatorName) throws Exception {38 By by = getLocator(locatorName);39 driver.findElement(by).click();40}41public void sendKeys(String locatorName, String value) throws Exception {42 By by = getLocator(locatorName);43 driver.findElement(by).sendKeys(value);44}45public String getText(String locatorName) throws Exception {
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!!