How to use getCapability method of io.appium.java_client.internal.CapabilityHelpers class

Best io.appium code snippet using io.appium.java_client.internal.CapabilityHelpers.getCapability

Run io.appium automation tests on LambdaTest cloud grid

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

AIFieldDecorator.java

Source: AIFieldDecorator.java Github

copy
1package com.globant.aimate.annotation;
2
3import static io.appium.java_client.internal.ElementMap.getElementClass;
4import static io.appium.java_client.pagefactory.utils.ProxyFactory.getEnhancedProxy;
5
6import java.lang.reflect.Field;
7import java.lang.reflect.ParameterizedType;
8import java.lang.reflect.Type;
9import java.lang.reflect.TypeVariable;
10import java.time.Duration;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.List;
14
15import org.openqa.selenium.Capabilities;
16import org.openqa.selenium.HasCapabilities;
17import org.openqa.selenium.SearchContext;
18import org.openqa.selenium.WebDriver;
19import org.openqa.selenium.WebElement;
20import org.openqa.selenium.remote.RemoteWebElement;
21import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;
22import org.openqa.selenium.support.pagefactory.ElementLocator;
23
24import com.google.common.collect.ImmutableList;
25
26import io.appium.java_client.MobileElement;
27import io.appium.java_client.android.AndroidElement;
28import io.appium.java_client.internal.CapabilityHelpers;
29import io.appium.java_client.ios.IOSElement;
30import io.appium.java_client.pagefactory.AppiumFieldDecorator;
31import io.appium.java_client.windows.WindowsElement;
32
33public class AIFieldDecorator extends AppiumFieldDecorator {
34	
35    private static final List<Class<? extends WebElement>> availableElementClasses = ImmutableList.of(WebElement.class,
36            RemoteWebElement.class, MobileElement.class, AndroidElement.class,
37            IOSElement.class, WindowsElement.class);
38	private final DefaultFieldDecorator fieldDecorator;
39	private WebDriver webDriver;
40    private final String platform;
41    private final String automation;
42    
43    public AIFieldDecorator(SearchContext context) {
44        this(context, DEFAULT_WAITING_TIMEOUT);
45    }
46
47    public AIFieldDecorator(SearchContext context, Duration duration) {
48    	super(context, duration);
49    	
50    	this.webDriver = (WebDriver) context;
51    	
52        if (this.webDriver instanceof HasCapabilities) {
53            Capabilities caps = ((HasCapabilities) this.webDriver).getCapabilities();
54            this.platform = CapabilityHelpers.getCapability(caps, "platformName", String.class);
55            this.automation = CapabilityHelpers.getCapability(caps, "automationName", String.class);
56        } else {
57            this.platform = null;
58            this.automation = null;
59        }
60    	
61    	// The magic inside the brackets is overriding proxyForLocator and proxyForListLocator methods of DefaultFieldDecorator class
62		fieldDecorator = new DefaultFieldDecorator(new AIElementLocatorFactory(context))  {
63            @Override
64            protected WebElement proxyForLocator(ClassLoader ignored, ElementLocator locator) {
65                AIElementInterceptor elementInterceptor = new AIElementInterceptor(locator, webDriver);
66                return getEnhancedProxy(getElementClass(platform, automation), elementInterceptor);
67            }
68
69            @Override
70            @SuppressWarnings("unchecked")
71            protected List<WebElement> proxyForListLocator(ClassLoader ignored,
72                                                           ElementLocator locator) {
73                AIElementListInterceptor elementInterceptor = new AIElementListInterceptor(locator);
74                return getEnhancedProxy(ArrayList.class, elementInterceptor);
75            }
76
77            @Override
78            protected boolean isDecoratableList(Field field) {
79                if (!List.class.isAssignableFrom(field.getType())) {
80                    return false;
81                }
82
83                Type genericType = field.getGenericType();
84                if (!(genericType instanceof ParameterizedType)) {
85                    return false;
86                }
87
88                Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0];
89
90                for (Class<? extends WebElement> webElementClass : availableElementClasses) {
91                    if (webElementClass.equals(listType)) {
92                        return true;
93                    }
94                }
95
96                return ((listType instanceof TypeVariable)
97                        && Arrays.asList(((TypeVariable<?>) listType).getBounds())
98                        .stream().anyMatch(item -> availableElementClasses.contains(item)));
99            }
100        };
101    }
102    
103    @Override
104    public Object decorate(ClassLoader ignored, Field field) {
105		if (field.isAnnotationPresent(AIFindBy.class)) {
106			return fieldDecorator.decorate(ignored, field);
107		}
108    	return super.decorate(ignored, field);
109    }
110    
111}
112
Full Screen

LoginPage.java

Source: LoginPage.java Github

copy
1package models.pages;
2
3import io.appium.java_client.AppiumDriver;
4import io.appium.java_client.MobileBy;
5import io.appium.java_client.MobileElement;
6import io.appium.java_client.internal.CapabilityHelpers;
7import io.appium.java_client.pagefactory.AndroidBy;
8import io.appium.java_client.pagefactory.AndroidFindBy;
9import io.appium.java_client.pagefactory.AppiumFieldDecorator;
10import io.appium.java_client.pagefactory.iOSXCUITFindBy;
11import models.components.global.BottomNavigationComponent;
12import org.openqa.selenium.By;
13import org.openqa.selenium.Capabilities;
14import org.openqa.selenium.support.PageFactory;
15
16import java.time.Duration;
17import java.util.Map;
18
19public class LoginPage {
20
21    private final AppiumDriver<MobileElement> appiumDriver;
22    private static final By usernameSel = MobileBy.AccessibilityId("input-email");
23    private static final By passwordSel = MobileBy.AccessibilityId("input-password");
24    private static final By loginBtnSel = MobileBy.AccessibilityId("button-LOGIN");
25
26    @AndroidFindBy(id = "android:id/alertTitle")
27    @iOSXCUITFindBy(iOSNsPredicate = "label == \"Success\"")
28    private MobileElement msgTitleElem;
29
30
31    public LoginPage(AppiumDriver<MobileElement> appiumDriver) {
32        this.appiumDriver = appiumDriver;
33        PageFactory.initElements(new AppiumFieldDecorator(appiumDriver, Duration.ofSeconds(10)), this);
34    }
35
36    // Return found element(s)
37    private MobileElement usernameElem(){
38        return appiumDriver.findElement(usernameSel);
39    }
40
41    public LoginPage inputUsername(String userName){
42        usernameElem().sendKeys(userName);
43        return this;
44    }
45
46    public LoginPage inputPassword(String password){
47        appiumDriver.findElement(passwordSel).sendKeys(password);
48        return this;
49    }
50
51    public void clickOnLoginBtn(){
52        appiumDriver.findElement(loginBtnSel).click();
53    }
54
55    public String loginMsgText(){
56        Capabilities caps = this.appiumDriver.getCapabilities();
57        String platform = CapabilityHelpers.getCapability(caps, "platformName", String.class);
58        System.out.println("PLATFORM : " + platform);
59        return msgTitleElem.getText();
60    }
61
62    public BottomNavigationComponent bottomNavigationComponent(){
63        return new BottomNavigationComponent(appiumDriver);
64    }
65
66}
67
Full Screen

LoginDialogComponent.java

Source: LoginDialogComponent.java Github

copy
1package models.components.authentication;
2
3import io.appium.java_client.AppiumDriver;
4import io.appium.java_client.MobileBy;
5import io.appium.java_client.MobileElement;
6import io.appium.java_client.internal.CapabilityHelpers;
7import io.appium.java_client.pagefactory.AppiumFieldDecorator;
8import io.appium.java_client.pagefactory.AndroidFindBy;
9import io.appium.java_client.pagefactory.iOSXCUITFindBy;
10import org.openqa.selenium.By;
11import org.openqa.selenium.Capabilities;
12import org.openqa.selenium.HasCapabilities;
13import org.openqa.selenium.WebElement;
14import org.openqa.selenium.support.PageFactory;
15
16import java.time.Duration;
17
18public class LoginDialogComponent {
19
20    private final AppiumDriver<MobileElement> appiumDriver;
21    private static final By okBtnSel = MobileBy.AccessibilityId("OK");
22
23    @AndroidFindBy(id = "android:id/alertTitle")
24    @iOSXCUITFindBy(iOSNsPredicate = "label == \"Success\"")
25    private MobileElement msgTitle;
26
27    @AndroidFindBy(id = "android:id/button1")
28    @iOSXCUITFindBy(accessibility = "OK")
29    private MobileElement okBtn;
30
31    public LoginDialogComponent(AppiumDriver<MobileElement> appiumDriver) {
32        this.appiumDriver = appiumDriver;
33        PageFactory.initElements(new AppiumFieldDecorator(appiumDriver, Duration.ofSeconds(10)), this);
34    }
35
36    public String msgTitle(){
37        return msgTitle.getText();
38    }
39
40    public void clickOnOKBtn(){
41        Capabilities caps = this.appiumDriver.getCapabilities();
42        String platform = CapabilityHelpers.getCapability(caps, "platformName", String.class);
43        if(platform.equalsIgnoreCase("ios")) okBtn.click();
44        okBtn.click();
45    }
46
47}
48
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in CapabilityHelpers

Trigger getCapability code on LambdaTest Cloud Grid

Execute automation tests with getCapability on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)