Best io.appium code snippet using io.appium.java_client.pagefactory.AppiumFieldDecorator.decorate
AppiumFieldDecorator.java
Source:AppiumFieldDecorator.java
...51import io.appium.java_client.pagefactory.locator.CacheableLocator;52import io.appium.java_client.windows.WindowsElement;5354/**55 * Default decorator for use with PageFactory. Will decorate 1) all of the56 * WebElement fields and 2) List of WebElement that have57 * {@literal @AndroidFindBy}, {@literal @AndroidFindBys}, or58 * {@literal @iOSFindBy/@iOSFindBys} annotation with a proxy that locates the59 * elements using the passed in ElementLocatorFactory.60 * Please pay attention: fields of {@link WebElement}, {@link RemoteWebElement},61 * {@link MobileElement}, {@link AndroidElement} and {@link IOSElement} are allowed62 * to use with this decorator63 */64public class AppiumFieldDecorator implements FieldDecorator {6566 public static long DEFAULT_IMPLICITLY_WAIT_TIMEOUT = 1;67 public static TimeUnit DEFAULT_TIMEUNIT = TimeUnit.SECONDS;68 private static final List<Class<? extends WebElement>> availableElementClasses = ImmutableList.of(WebElement.class,69 RemoteWebElement.class, MobileElement.class, TouchableElement.class, AndroidElement.class,70 IOSElement.class, WindowsElement.class);71 protected final AppiumElementLocatorFactory widgetLocatorFactory;72 private final String automation;73 private final DefaultFieldDecorator defaultElementFieldDecoracor;74 private final HasSessionDetails hasSessionDetails;75 private final WebDriver originalDriver;76 private final String platform;77 private final TimeOutDuration timeOutDuration;787980 public AppiumFieldDecorator(SearchContext context) {81 this(context, DEFAULT_IMPLICITLY_WAIT_TIMEOUT, DEFAULT_TIMEUNIT);82 }8384 public AppiumFieldDecorator(SearchContext context, long implicitlyWaitTimeOut,85 TimeUnit timeUnit) {86 this(context, new TimeOutDuration(implicitlyWaitTimeOut, timeUnit));87 }8889 /**90 * @param context is an instance of {@link org.openqa.selenium.SearchContext}91 * It may be the instance of {@link org.openqa.selenium.WebDriver}92 * or {@link org.openqa.selenium.WebElement} or93 * {@link io.appium.java_client.pagefactory.Widget} or some other user's94 * extension/implementation.95 * @param timeOutDuration is a desired duration of the waiting for an element presence.96 */97 public AppiumFieldDecorator(SearchContext context, TimeOutDuration timeOutDuration) {98 this.originalDriver = unpackWebDriverFromSearchContext(context);99 if (originalDriver == null100 || !HasSessionDetails.class.isAssignableFrom(originalDriver.getClass())) {101 hasSessionDetails = null;102 platform = null;103 automation = null;104 } else {105 hasSessionDetails = HasSessionDetails.class.cast(originalDriver);106 platform = hasSessionDetails.getPlatformName();107 automation = hasSessionDetails.getAutomationName();108 }109110 this.timeOutDuration = timeOutDuration;111112 defaultElementFieldDecoracor = new DefaultFieldDecorator(113 new AppiumElementLocatorFactory(context, timeOutDuration, originalDriver,114 new DefaultElementByBuilder(platform, automation))) {115 @Override protected boolean isDecoratableList(Field field) {116 if (!List.class.isAssignableFrom(field.getType())) {117 return false;118 }119120 Type genericType = field.getGenericType();121 if (!(genericType instanceof ParameterizedType)) {122 return false;123 }124125 Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0];126127 for (Class<? extends WebElement> webElementClass : availableElementClasses) {128 if (webElementClass.equals(listType)) {129 return true;130 }131 }132 return false;133 }134135 @Override136 @SuppressWarnings("unchecked")137 protected List<WebElement> proxyForListLocator(ClassLoader ignored,138 ElementLocator locator) {139 ElementListInterceptor elementInterceptor = new ElementListInterceptor(locator);140 return getEnhancedProxy(ArrayList.class, elementInterceptor);141 }142143 @Override144 protected WebElement proxyForLocator(ClassLoader ignored, ElementLocator locator) {145 return proxyForAnElement(locator);146 }147 };148149 widgetLocatorFactory =150 new AppiumElementLocatorFactory(context, timeOutDuration, originalDriver,151 new WidgetByBuilder(platform, automation));152 }153154 /**155 * @param ignored class loader is ignored by current implementation156 * @param field is {@link java.lang.reflect.Field} of page object which is supposed to be157 * decorated.158 * @return a field value or null.159 */160 @Override161 public Object decorate(ClassLoader ignored, Field field) {162 Object result = defaultElementFieldDecoracor.decorate(ignored, field);163 if (result != null) {164 return result;165 }166167 return decorateWidget(field);168 }169170 protected WebElement proxyForAnElement(ElementLocator locator) {171 ElementInterceptor elementInterceptor = new ElementInterceptor(locator, originalDriver);172 return getEnhancedProxy(getElementClass(hasSessionDetails), elementInterceptor);173 }174175 @SuppressWarnings("unchecked")176 private Object decorateWidget(Field field) {177 Class<?> type = field.getType();178 if (!Widget.class.isAssignableFrom(type) && !List.class.isAssignableFrom(type)) {179 return null;180 }181182 Class<? extends Widget> widgetType;183 boolean isAlist = false;184 if (List.class.isAssignableFrom(type)) {185 isAlist = true;186 Type genericType = field.getGenericType();187 if (!(genericType instanceof ParameterizedType)) {188 return null;189 }190
...
SelenideAppiumFieldDecorator.java
Source:SelenideAppiumFieldDecorator.java
...48 return new DefaultElementByBuilder(d.getPlatformName(), d.getAutomationName());49 }50 }51 @Override52 public Object decorate(ClassLoader loader, Field field) {53 builder.setAnnotated(field);54 By selector = builder.buildBy();55 if (selector == null) {56 selector = new Annotations(field).buildBy();57 }58 if (selector instanceof ByIdOrName) {59 return decorateWithAppium(loader, field);60 }61 else if (SelenideElement.class.isAssignableFrom(field.getType())) {62 return ElementFinder.wrap(driver, driver.getWebDriver(), selector, 0);63 }64 else if (ElementsCollection.class.isAssignableFrom(field.getType())) {65 return new ElementsCollection(new BySelectorCollection(driver, selector));66 }67 else if (ElementsContainer.class.isAssignableFrom(field.getType())) {68 return createElementsContainer(selector, field);69 }70 else if (isDecoratableList(field, ElementsContainer.class)) {71 return createElementsContainerList(field);72 }73 else if (isDecoratableList(field, SelenideElement.class)) {74 return SelenideElementListProxy.wrap(driver, factory.createLocator(field));75 }76 return super.decorate(loader, field);77 }78 private Object decorateWithAppium(ClassLoader loader, Field field) {79 Object appiumElement = super.decorate(loader, field);80 if (appiumElement instanceof MobileElement) {81 return Selenide.$((MobileElement) appiumElement);82 }83 return appiumElement;84 }85 private ElementsContainer createElementsContainer(By selector, Field field) {86 try {87 SelenideElement self = ElementFinder.wrap(driver, driver.getWebDriver(), selector, 0);88 return initElementsContainer(field.getType(), self);89 }90 catch (Exception e) {91 throw new RuntimeException("Failed to create elements container for field " + field.getName(), e);92 }93 }...
AIFieldDecorator.java
Source:AIFieldDecorator.java
...87 };88 }89 90 @Override91 public Object decorate(ClassLoader ignored, Field field) {92 if (field.isAnnotationPresent(AIFindBy.class)) {93 return fieldDecorator.decorate(ignored, field);94 }95 return super.decorate(ignored, field);96 }97 98}...
LazyFieldDecorator.java
Source:LazyFieldDecorator.java
...18 /* Constructor for an LazyLocatorFactory. */19 public LazyFieldDecorator(AppiumFieldDecorator factory) {20 this.factory = factory;21 }22 public Object decorate(Field field) {23 if(isDecorableModule(field.getType(), field)) {24 return decorateModule(field);25 } else if (isDecorable(field.getType(), field) || isDecorableList(field)) {26 return decorateField(field);27 } else {28 return null;29 }30 }31 private Object decorateModule(Field field) {32 Class<?> fieldType = field.getType();33 LazyLocator locator = (LazyLocator)factory.createLocator(field);34 if (locator == null) {35 return null;36 }37 if (com.malski.core.web.view.Component.class.isAssignableFrom(fieldType)) {38 return proxyForModule(fieldType ,locator);39 } else {40 return null;41 }42 }43 private Object decorateField(Field field) {44 Class<?> fieldType = field.getType();45 LazyLocator locator = (LazyLocator)factory.createLocator(field);46 if (locator == null) {47 return null;48 }49 if (WebElement.class.equals(fieldType)) {50 fieldType = Element.class;51 }52 if (WebElement.class.isAssignableFrom(fieldType)) {53 return proxyForLocator(fieldType, locator);54 } else if (List.class.isAssignableFrom(fieldType)) {55 Class<?> erasureClass = getErasureClass(field);56 return proxyForListLocator(erasureClass, locator);57 } else {...
LazyLocatorFactory.java
Source:LazyLocatorFactory.java
...51 };52 static {53 DEFAULT_TIMEUNIT = TimeUnit.SECONDS;54 }55 public Object decorate(ClassLoader ignored, Field field) {56 Object result = this.defaultElementFieldDecoracor.decorate(ignored, field);57 return result != null ? result : this.decorateWidget(field);58 }59 private Class<?> getTypeForProxy() {60 Class driverClass = this.originalDriver.getClass();61 Set rules = elementRuleMap.entrySet();62 Iterator var4 = rules.iterator();63 while (var4.hasNext()) {64 Map.Entry e = (Map.Entry) var4.next();65 if (((Class) e.getKey()).isAssignableFrom(driverClass)) {66 return (Class) e.getValue();67 }68 }69 return RemoteWebElement.class;70 }71 private WebElement proxyForAnElement(ElementLocator locator) {...
PageFactory.java
Source:PageFactory.java
...35 decorator = new AppiumFieldDecorator(driver);36 } else {37 decorator = new ElementDecorator(new DefaultElementLocatorFactory(driver));38 }39 proxy = decorator.decorate(page.getClass().getClassLoader(), field);40 if (proxy != null) {41 try {42 field.setAccessible(true);43 field.set(page, proxy);44 } catch (IllegalAccessException e) {45 log.error(e.getMessage(), e);46 }47 }48 }49 }50 /**51 * Similar to the other "initElements" methods, but takes an {@link FieldDecorator} which is used52 * for decorating each of the fields.53 *54 * @param decorator the decorator to use55 * @param page The object to decorate the fields of56 */57 public static void initElements(FieldDecorator decorator, Object page) {58 Class<?> proxyIn = page.getClass();59 while (proxyIn != Object.class) {60 proxyFields(decorator, page, proxyIn);61 proxyIn = proxyIn.getSuperclass();62 }63 }64 private static void proxyFields(FieldDecorator decorator, Object page, Class<?> proxyIn) {65 Field[] fields = proxyIn.getDeclaredFields();66 for (Field field : fields) {67 Object value = decorator.decorate(page.getClass().getClassLoader(), field);68 if (value != null) {69 try {70 field.setAccessible(true);71 field.set(page, value);72 } catch (IllegalAccessException e) {73 throw new RuntimeException(e);74 }75 }76 }77 }78}...
DefaultDecorator.java
Source:DefaultDecorator.java
...16 super(context, time, timeUnit);17 this.decomposable = decomposable;18 } 19 20 public Object decorate(ClassLoader ignored, Field field) {21 Object result = super.decorate(ignored, field);22 if (result != null)23 return result;24 25 field.setAccessible(true);26 try {27 result = field.get(decomposable);28 int modifiers = field.getModifiers();29 if (result!=null && !Modifier.isStatic(modifiers) 30 && !Modifier.isFinal(modifiers))31 return result;32 return null;33 } catch (Exception e) {34 throw new RuntimeException(e);35 }...
decorate
Using AI Code Generation
1package appium.java;2import java.net.MalformedURLException;3import java.net.URL;4import java.util.concurrent.TimeUnit;5import org.openqa.selenium.remote.DesiredCapabilities;6import org.openqa.selenium.support.PageFactory;7import io.appium.java_client.AppiumDriver;8import io.appium.java_client.MobileElement;9import io.appium.java_client.android.AndroidDriver;10import io.appium.java_client.pagefactory.AppiumFieldDecorator;11import io.appium.java_client.pagefactory.TimeOutDuration;12import io.appium.java_client.remote.MobileCapabilityType;13public class AppiumJava {14public static AppiumDriver<MobileElement> driver;15public static void main(String[] args) throws MalformedURLException, InterruptedException {16DesiredCapabilities caps = new DesiredCapabilities();17caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");18caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");19caps.setCapability(MobileCapabilityType.VERSION, "8.1.0");20caps.setCapability("appPackage", "com.android.calculator2");21caps.setCapability("appActivity", ".Calculator");
decorate
Using AI Code Generation
1@AndroidFindBy(id = "com.android.calculator2:id/digit_1")2public WebElement one;3@AndroidFindBy(id = "com.android.calculator2:id/digit_2")4public WebElement two;5@AndroidFindBy(id = "com.android.calculator2:id/plus")6public WebElement plus;7@AndroidFindBy(id = "com.android.calculator2:id/equal")8public WebElement equals;9@AndroidFindBy(id = "com.android.calculator2:id/result")10public WebElement result;11public void addTwoNumbers(){12 one.click();13 plus.click();14 two.click();15 equals.click();16}17public String getResult(){18 return result.getText();19}20public void closeCalculator(){21 driver.closeApp();22}23public void launchCalculator(){24 driver.launchApp();25}26public void clearCalculator(){27 driver.resetApp();28}29public void killCalculator(){30 driver.removeApp("com.android.calculator2");31}32public void installCalculator(){33 driver.installApp("com.android.calculator2");34}35public void openCalculator(){36 driver.startActivity("com.android.calculator2", "com.android.calculator2.Calculator");37}38public void openCalculator(String appPackage, String appActivity){39 driver.startActivity(appPackage, appActivity);40}41public void openCalculator(String appPackage, String appActivity, String appWaitPackage, String appWaitActivity){42 driver.startActivity(appPackage, appActivity, appWaitPackage, appWaitActivity);43}
decorate
Using AI Code Generation
1public void test() {2 PageFactory.initElements(new AppiumFieldDecorator(driver), this);3}4 Appium::Driver::DriverExtensions::HasElement.new(@driver).decorate_element(self)5 Appium::Driver::DriverExtensions::HasElement.new(@driver).decorate_element(self)6 Appium::Driver::DriverExtensions::HasElement.new(@driver).decorate_element(self)7 Appium::Driver::DriverExtensions::HasElement.new(@driver).decorate_element(self)8 Appium::Driver::DriverExtensions::HasElement.new(@driver).decorate_element(self)9 Appium::Driver::DriverExtensions::HasElement.new(@driver).decorate_element(self)10 Appium::Driver::DriverExtensions::HasElement.new(@driver).decorate_element(self)11 Appium::Driver::DriverExtensions::HasElement.new(@driver).decorate_element(self)12 Appium::Driver::DriverExtensions::HasElement.new(@driver).decorate_element(self)
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!!