Best io.appium code snippet using io.appium.java_client.pagefactory.AppiumElementLocatorFactory
AppiumFieldDecorator.java
Source:AppiumFieldDecorator.java
...60 public static long DEFAULT_IMPLICITLY_WAIT_TIMEOUT = 1;61 public static TimeUnit DEFAULT_TIMEUNIT = TimeUnit.SECONDS;62 private final WebDriver originalDriver;63 private final DefaultFieldDecorator defaultElementFieldDecoracor;64 private final AppiumElementLocatorFactory widgetLocatorFactory;65 private final String platform;66 private final String automation;67 private final TimeOutDuration timeOutDuration;68 private final HasSessionDetails hasSessionDetails;69 public AppiumFieldDecorator(SearchContext context, long implicitlyWaitTimeOut,70 TimeUnit timeUnit) {71 this(context, new TimeOutDuration(implicitlyWaitTimeOut, timeUnit));72 }73 /**74 * @param context is an instance of {@link org.openqa.selenium.SearchContext}75 * It may be the instance of {@link org.openqa.selenium.WebDriver}76 * or {@link org.openqa.selenium.WebElement} or77 * {@link io.appium.java_client.pagefactory.Widget} or some other user's78 * extension/implementation.79 * @param timeOutDuration is a desired duration of the waiting for an element presence.80 */81 public AppiumFieldDecorator(SearchContext context, TimeOutDuration timeOutDuration) {82 this.originalDriver = unpackWebDriverFromSearchContext(context);83 if (originalDriver == null84 || !HasSessionDetails.class.isAssignableFrom(originalDriver.getClass())) {85 hasSessionDetails = null;86 platform = null;87 automation = null;88 } else {89 hasSessionDetails = HasSessionDetails.class.cast(originalDriver);90 platform = hasSessionDetails.getPlatformName();91 automation = hasSessionDetails.getAutomationName();92 }93 this.timeOutDuration = timeOutDuration;94 defaultElementFieldDecoracor = new DefaultFieldDecorator(95 new AppiumElementLocatorFactory(context, timeOutDuration, originalDriver,96 new DefaultElementByBuilder(platform, automation))) {97 @Override98 protected WebElement proxyForLocator(ClassLoader ignored, ElementLocator locator) {99 return proxyForAnElement(locator);100 }101 @Override102 @SuppressWarnings("unchecked")103 protected List<WebElement> proxyForListLocator(ClassLoader ignored,104 ElementLocator locator) {105 ElementListInterceptor elementInterceptor = new ElementListInterceptor(locator);106 return getEnhancedProxy(ArrayList.class, elementInterceptor);107 }108 @Override protected boolean isDecoratableList(Field field) {109 if (!List.class.isAssignableFrom(field.getType())) {110 return false;111 }112 Type genericType = field.getGenericType();113 if (!(genericType instanceof ParameterizedType)) {114 return false;115 }116 Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0];117 for (Class<? extends WebElement> webElementClass : availableElementClasses) {118 if (webElementClass.equals(listType)) {119 return true;120 }121 }122 return false;123 }124 };125 widgetLocatorFactory =126 new AppiumElementLocatorFactory(context, timeOutDuration, originalDriver,127 new WidgetByBuilder(platform, automation));128 }129 public AppiumFieldDecorator(SearchContext context) {130 this(context, DEFAULT_IMPLICITLY_WAIT_TIMEOUT, DEFAULT_TIMEUNIT);131 }132 /**133 * @param ignored class loader is ignored by current implementation134 * @param field is {@link java.lang.reflect.Field} of page object which is supposed to be135 * decorated.136 * @return a field value or null.137 */138 public Object decorate(ClassLoader ignored, Field field) {139 Object result = defaultElementFieldDecoracor.decorate(ignored, field);140 if (result != null) {...
DefaultFieldDecorator.java
Source:DefaultFieldDecorator.java
...3import com.tradays.metaquotes.core.field.MobileElementFacade;4import com.tradays.metaquotes.core.page.proxyhandlers.CollectionProxyHandler;5import com.tradays.metaquotes.core.page.proxyhandlers.ElementListProxyHandler;6import com.tradays.metaquotes.core.page.proxyhandlers.ElementProxyHandler;7import io.appium.java_client.pagefactory.AppiumElementLocatorFactory;8import io.appium.java_client.pagefactory.DefaultElementByBuilder;9import org.junit.Assert;10import org.openqa.selenium.SearchContext;11import org.openqa.selenium.WebElement;12import org.openqa.selenium.support.pagefactory.FieldDecorator;13import java.lang.reflect.Field;14import java.lang.reflect.InvocationHandler;15import java.time.Duration;16import java.util.List;17/**18 * РаÑÑиÑÑÐµÑ ÑÑнкÑионалÑноÑÑÑ ÑÑандаÑÑного FieldDecorator Ð´Ð»Ñ ÑеалиаÑии ÑипизаÑии ÑлеменÑов,19 * ÐобавлÑÐµÑ Ð²Ð¾Ð·Ð¼Ð¾Ð¶Ð½Ð¾ÑÑÑ ÑабоÑÑ Ñо ÑпиÑком ÑлеменÑов и коллекÑий(одинаковÑÑ
ÑлеменÑов)20 *21 * @author Nikolay Streltsov on 01.11.202022 */23public class DefaultFieldDecorator implements FieldDecorator {24 SearchContext searchContext;25 public DefaultFieldDecorator(SearchContext searchContext){26 this.searchContext = searchContext;27 }28 public Object decorate(ClassLoader classLoader, Field field) {29 try {30 if (PageFactoryUtils.isCollectionElement(field)) {31 return decorateElementCollection(classLoader, field);32 }33 if (PageFactoryUtils.isListElement(field)){34 return decorateElementList(classLoader, field);35 }36 if (PageFactoryUtils.isElement(field)){37 return decorateWebElement(classLoader, field);38 }39 return null;40 } catch (ClassCastException e) {41 e.printStackTrace();42 Assert.fail("Ðе ÑдалоÑÑ ÑоздаÑÑ Ð¿ÑокÑи Ð´Ð»Ñ ÑлеменÑа: " + field.getName());43 return null;44 }45 }46 /**47 * ÐекоÑиÑование ÑлеменÑа в конкÑеÑнÑй Ñип (Button, CheckBox, RadioButton и Ñ.д.)48 *49 * @param loader - загÑÑзÑик клаÑÑов50 * @param field - reflection поле PageObject51 * @return - proxy обÑекÑ, коÑоÑÑй бÑÐ´ÐµÑ Ñоздан пÑи пеÑвом обÑаÑении к немÑ52 */53 private <T extends MobileElementFacade> T decorateWebElement(ClassLoader loader, Field field) {54 WebElement elementToWrap = proxyWebElement(loader, field);55 return PageFactoryUtils.createElement((Class<T>) field.getType(), elementToWrap,field.getName());56 }57 /**58 * ÐекоÑиÑование ÑпиÑка ÑлеменÑов ( List<Button>, List<CheckBox>, List<RadioButton> и Ñ.д.)59 *60 * @param loader - загÑÑзÑик клаÑÑов61 * @param field - reflection поле PageObject62 * @return - proxy обÑекÑ, коÑоÑÑй бÑÐ´ÐµÑ Ñоздан пÑи пеÑвом обÑаÑении к немÑ63 */64 protected <T extends MobileElementFacade> List<T> decorateElementList(ClassLoader loader, Field field) {65 AppiumElementLocatorFactory locator = new AppiumElementLocatorFactory(66 searchContext,67 Duration.ofSeconds(FrameworkConfig.get().implicitlywait()),68 new DefaultElementByBuilder("android",69 "this.automation"));70 InvocationHandler handler = new ElementListProxyHandler(PageFactoryUtils.getGenericParameterClass(field), locator.createLocator(field), PageFactoryUtils.getElementName(field));71 return ProxyFactory.creatElementListProxy(loader, handler);72 }73 /**74 * ÐекоÑиÑование ÑпиÑка коллекÑий(одинаковÑе ÑлеменÑÑ, ÑодеÑжаÑие одинаковÑе ÑлеменÑÑ) ( List<T extends ICollectionPageObject>>)75 *76 * @param loader - загÑÑзÑик клаÑÑов77 * @param field - reflection поле PageObject78 * @return - proxy обÑекÑ, коÑоÑÑй бÑÐ´ÐµÑ Ñоздан пÑи пеÑвом обÑаÑении к немÑ79 */80 private Object decorateElementCollection(ClassLoader loader, Field field) {81 AppiumElementLocatorFactory locator = new AppiumElementLocatorFactory(82 searchContext,83 Duration.ofSeconds(FrameworkConfig.get().implicitlywait()),84 new DefaultElementByBuilder("android", "automation")85 );86 InvocationHandler handler = new CollectionProxyHandler(PageFactoryUtils.getGenericParameterClass(field), locator.createLocator(field), PageFactoryUtils.getElementName(field));87 return ProxyFactory.createElementCollectionProxy(loader, handler);88 }89 private WebElement proxyWebElement(ClassLoader loader, Field field) {90 AppiumElementLocatorFactory locator = new AppiumElementLocatorFactory(91 searchContext,92 Duration.ofSeconds(FrameworkConfig.get().implicitlywait()),93 new DefaultElementByBuilder("android", "automation")94 );95 InvocationHandler handler = new ElementProxyHandler(locator.createLocator(field), field.getName());96 return ProxyFactory.createWebElementProxy(loader, handler);97 }98}...
AppiumElementLocatorFactory.java
Source:AppiumElementLocatorFactory.java
...28import io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder;29import io.appium.java_client.pagefactory.locator.CacheableElementLocatorFactory;30import io.appium.java_client.pagefactory.locator.CacheableLocator;3132public class AppiumElementLocatorFactory implements CacheableElementLocatorFactory {33 private final AppiumByBuilder builder;34 private final WebDriver originalWebDriver;35 private final SearchContext searchContext;36 private final TimeOutDuration timeOutDuration;3738 public AppiumElementLocatorFactory(SearchContext searchContext, TimeOutDuration timeOutDuration,39 WebDriver originalWebDriver, AppiumByBuilder builder) {40 this.searchContext = searchContext;41 this.originalWebDriver = originalWebDriver;42 this.timeOutDuration = timeOutDuration;43 this.builder = builder;44 }4546 @Override public CacheableLocator createLocator(AnnotatedElement annotatedElement) {47 TimeOutDuration customDuration;48 if (annotatedElement.isAnnotationPresent(WithTimeout.class)) {49 WithTimeout withTimeout = annotatedElement.getAnnotation(WithTimeout.class);50 customDuration = new TimeOutDuration(withTimeout.time(), withTimeout.unit());51 } else {52 customDuration = timeOutDuration;
...
WebDriverAPI.java
Source:WebDriverAPI.java
...3import cucumber.api.java.gl.E;4import io.appium.java_client.AppiumDriver;5import io.appium.java_client.android.AndroidElement;6import io.appium.java_client.pagefactory.AndroidFindBy;7import io.appium.java_client.pagefactory.AppiumElementLocatorFactory;8import io.appium.java_client.pagefactory.AppiumFieldDecorator;9import org.openqa.selenium.By;10import org.openqa.selenium.WebDriver;11import org.openqa.selenium.WebElement;12import org.openqa.selenium.support.PageFactory;13import org.openqa.selenium.support.pagefactory.FieldDecorator;14import org.openqa.selenium.support.ui.ExpectedConditions;15import org.openqa.selenium.support.ui.WebDriverWait;1617import java.util.concurrent.TimeUnit;1819/**20 * Created by pravin.kumbhare on 23-07-2018.21 */
...
WinAppDriver.java
Source:WinAppDriver.java
1package driver;2import io.appium.java_client.pagefactory.AppiumElementLocatorFactory;3import io.appium.java_client.pagefactory.AppiumFieldDecorator;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.support.PageFactory;6public class WinAppDriver {7 //JsonToWebElementConverter(RemoteWebDriver driver)8 public WinAppDriver(WebDriver driver){9 PageFactory.initElements(new AppiumFieldDecorator(driver),15);10 }11}...
AppiumElementLocatorFactory
Using AI Code Generation
1AppiumDriver<MobileElement> driver;2AppiumElementLocatorFactory factory = new AppiumElementLocatorFactory(driver);3PageFactory.initElements(new AppiumFieldDecorator(factory), this);4AppiumDriver<MobileElement> driver;5PageFactory.initElements(new AppiumFieldDecorator(driver), this);6AppiumDriver<MobileElement> driver;7PageFactory.initElements(new AppiumFieldDecorator(driver, 10, TimeUnit.SECONDS), this);8AppiumDriver<MobileElement> driver;9PageFactory.initElements(new AppiumFieldDecorator(driver, 10, TimeUnit.SECONDS, new SystemClock()), this);10AppiumDriver<MobileElement> driver;11PageFactory.initElements(new AppiumFieldDecorator(driver, 10, TimeUnit.SECONDS, new SystemClock(), new DefaultElementByBuilder()), this);12AppiumDriver<MobileElement> driver;13PageFactory.initElements(new AppiumFieldDecorator(driver, 10, TimeUnit.SECONDS, new SystemClock(), new DefaultElementByBuilder(), new DefaultElementByBuilder()), this);14AppiumDriver<MobileElement> driver;15PageFactory.initElements(new AppiumFieldDecorator(driver, 10, TimeUnit.SECONDS, new SystemClock(), new DefaultElementByBuilder(), new DefaultElementByBuilder(), new DefaultElementByBuilder()), this);
AppiumElementLocatorFactory
Using AI Code Generation
1package appium;2import java.net.URL;3import org.openqa.selenium.remote.DesiredCapabilities;4import io.appium.java_client.AppiumDriver;5import io.appium.java_client.MobileElement;6import io.appium.java_client.android.AndroidDriver;7import io.appium.java_client.pagefactory.AppiumElementLocatorFactory;8import io.appium.java_client.pagefactory.AppiumFieldDecorator;9import io.appium.java_client.pagefactory.AndroidFindBy;10import io.appium.java_client.pagefactory.iOSFindBy;11import io.appium.java_client.pagefactory.iOSXCUITFindBy;12import io.appium.java_client.pagefactory.WithTimeout;13import io.appium.java_client.remote.MobileCapabilityType;14import io.appium.java_client.remote.MobilePlatform;15import org.openqa.selenium.support.PageFactory;16public class appium {17 public static AppiumDriver<MobileElement> driver;18 @WithTimeout(time = 20, unit = TimeUnit.SECONDS)19 @AndroidFindBy(id = "com.android.calculator2:id/digit_4")20 @iOSFindBy(id = "4")21 @iOSXCUITFindBy(id = "4")22 public static MobileElement digit_4;23 @WithTimeout(time = 20, unit = TimeUnit.SECONDS)24 @AndroidFindBy(id = "com.android.calculator2:id/digit_5")25 @iOSFindBy(id = "5")26 @iOSXCUITFindBy(id = "5")27 public static MobileElement digit_5;28 @WithTimeout(time = 20, unit = TimeUnit.SECONDS)29 @AndroidFindBy(id = "com.android.calculator2:id/eq")30 @iOSFindBy(id = "=")31 @iOSXCUITFindBy(id = "=")32 public static MobileElement eq;33 @WithTimeout(time = 20, unit = TimeUnit.SECONDS)34 @AndroidFindBy(id = "com.android.calculator2:id/result")35 @iOSFindBy(id = "Result is")36 @iOSXCUITFindBy(id = "Result is")37 public static MobileElement result;38 public static void main(String[] args) throws MalformedURLException {39 DesiredCapabilities cap = new DesiredCapabilities();40 cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");
AppiumElementLocatorFactory
Using AI Code Generation
1AppiumElementLocatorFactory factory = new AppiumElementLocatorFactory(driver);2PageFactory.initElements(factory, this);3}4AppiumFieldDecorator decorator = new AppiumFieldDecorator(driver);5PageFactory.initElements(decorator, this);6AppiumFieldDecorator decorator = new AppiumFieldDecorator(driver, 30, TimeUnit.SECONDS);7PageFactory.initElements(decorator, this);8AppiumFieldDecorator decorator = new AppiumFieldDecorator(driver, 30);9PageFactory.initElements(decorator, this);10AppiumFieldDecorator decorator = new AppiumFieldDecorator(driver);11PageFactory.initElements(decorator, this);12driver.findElement(By.id("com.androidsample.generalstore:id/nameField")).sendKeys("Hello");13driver.findElement(By.id("com.androidsample.generalstore:id/radioFemale")).click();14driver.findElement(By.id("com.androidsample.generalstore:id/b
AppiumElementLocatorFactory
Using AI Code Generation
1import io.appium.java_client.pagefactory.AppiumElementLocatorFactory;2import org.openqa.selenium.support.PageFactory;3PageFactory.initElements(new AppiumElementLocatorFactory(driver), this);4@FindBy(id = "com.android.calculator2:id/digit_1")5public MobileElement one;6import io.appium.java_client.pagefactory.AppiumFieldDecorator;7import org.openqa.selenium.support.PageFactory;8PageFactory.initElements(new AppiumFieldDecorator(driver), this);9@FindBy(id = "com.android.calculator2:id/digit_1")10public MobileElement one;11import io.appium.java_client.pagefactory.AppiumFieldDecorator;12import org.openqa.selenium.support.PageFactory;13PageFactory.initElements(new AppiumFieldDecorator(driver), this);14@FindBy(id = "com.android.calculator2:id/digit_1")15public MobileElement one;16import io.appium.java_client.pagefactory.AppiumFieldDecorator;17import org.openqa.selenium.support.PageFactory;18PageFactory.initElements(new AppiumFieldDecorator(driver), this);19@FindBy(id = "com.android.calculator2:id/digit_1")20public MobileElement one;
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!!