Best io.appium code snippet using io.appium.java_client.MobileBy.ByWindowsAutomation
MobileBy.java
Source:MobileBy.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 org.apache.commons.lang3.StringUtils;18import org.openqa.selenium.By;19import org.openqa.selenium.SearchContext;20import org.openqa.selenium.WebDriverException;21import org.openqa.selenium.WebElement;22import java.io.Serializable;23import java.util.List;24@SuppressWarnings("serial")25public abstract class MobileBy extends By {26    private static final String ERROR_TEXT = "The class %s of the given context "27        + "doesn't implement %s nor %s. Sorry. It is impossible to find something.";28    private final String locatorString;29    private final MobileSelector selector;30    private static IllegalArgumentException formIllegalArgumentException(Class<?> givenClass,31        Class<?> class1, Class<?> class2) {32        return new IllegalArgumentException(String.format(ERROR_TEXT, givenClass.getCanonicalName(),33            class1.getCanonicalName(), class2.getCanonicalName()));34    }35    protected MobileBy(MobileSelector selector, String locatorString) {36        if (StringUtils.isBlank(locatorString)) {37            throw new IllegalArgumentException("Must supply a not empty locator value.");38        }39        this.locatorString = locatorString;40        this.selector = selector;41    }42    protected String getLocatorString() {43        return locatorString;44    }45    @SuppressWarnings("unchecked")46    @Override public List<WebElement> findElements(SearchContext context) {47        return (List<WebElement>) ((FindsByFluentSelector<?>) context)48            .findElements(selector.toString(), getLocatorString());49    }50    @Override public WebElement findElement(SearchContext context) {51        return ((FindsByFluentSelector<?>) context)52            .findElement(selector.toString(), getLocatorString());53    }54    /**55     * Read https://developer.apple.com/library/tvos/documentation/DeveloperTools/56     * Conceptual/InstrumentsUserGuide/UIAutomation.html57     *58     * @param iOSAutomationText is iOS UIAutomation string59     * @return an instance of {@link io.appium.java_client.MobileBy.ByIosUIAutomation}60     */61    public static By IosUIAutomation(final String iOSAutomationText) {62        return new ByIosUIAutomation(iOSAutomationText);63    }64    /**65     * Read http://developer.android.com/intl/ru/tools/testing-support-library/66     * index.html#uia-apis67     * @param uiautomatorText is Android UIAutomator string68     * @return an instance of {@link io.appium.java_client.MobileBy.ByAndroidUIAutomator}69     */70    public static By AndroidUIAutomator(final String uiautomatorText) {71        return new ByAndroidUIAutomator(uiautomatorText);72    }73    /**74     * About Android accessibility75     * https://developer.android.com/intl/ru/training/accessibility/accessible-app.html76     * About iOS accessibility77     * https://developer.apple.com/library/ios/documentation/UIKit/Reference/78     * UIAccessibilityIdentification_Protocol/index.html79     * @param accessibilityId id is a convenient UI automation accessibility Id.80     * @return an instance of {@link io.appium.java_client.MobileBy.ByAndroidUIAutomator}81     */82    public static By AccessibilityId(final String accessibilityId) {83        return new ByAccessibilityId(accessibilityId);84    }85    /**86     * This locator strategy is available in XCUITest Driver mode87     * @param iOSClassChainString is a valid class chain locator string.88     *                            See <a href="https://github.com/facebook/WebDriverAgent/wiki/Queries">89     *                            the documentation</a> for more details90     * @return an instance of {@link io.appium.java_client.MobileBy.ByIosClassChain}91     */92    public static By iOSClassChain(final String iOSClassChainString) {93        return new ByIosClassChain(iOSClassChainString);94    }95    /**96    * This locator strategy is available in XCUITest Driver mode97    * @param iOSNsPredicateString is an an iOS NsPredicate String98    * @return an instance of {@link io.appium.java_client.MobileBy.ByIosNsPredicate}99    */100    public static By iOSNsPredicateString(final String iOSNsPredicateString) {101        return new ByIosNsPredicate(iOSNsPredicateString);102    }103    public static By windowsAutomation(final String windowsAutomation) {104        return new ByWindowsAutomation(windowsAutomation);105    }106    107    public static class ByIosUIAutomation extends MobileBy implements Serializable {108        public ByIosUIAutomation(String iOSAutomationText) {109            super(MobileSelector.IOS_UI_AUTOMATION, iOSAutomationText);110        }111        /**112         * @throws WebDriverException when current session doesn't support the given selector or when113         *      value of the selector is not consistent.114         * @throws IllegalArgumentException when it is impossible to find something on the given115         * {@link SearchContext} instance116         */117        @SuppressWarnings("unchecked")118        @Override119        public List<WebElement> findElements(SearchContext context) throws WebDriverException,120            IllegalArgumentException {121            Class<?> contextClass = context.getClass();122            if (FindsByIosUIAutomation.class.isAssignableFrom(contextClass)) {123                return FindsByIosUIAutomation.class.cast(context)124                    .findElementsByIosUIAutomation(getLocatorString());125            }126            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {127                return super.findElements(context);128            }129            throw formIllegalArgumentException(contextClass, FindsByIosUIAutomation.class,130                FindsByFluentSelector.class);131        }132        /**133         * @throws WebDriverException when current session doesn't support the given selector or when134         *      value of the selector is not consistent.135         * @throws IllegalArgumentException when it is impossible to find something on the given136         * {@link SearchContext} instance137         */138        @Override public WebElement findElement(SearchContext context) throws WebDriverException,139            IllegalArgumentException {140            Class<?> contextClass = context.getClass();141            if (FindsByIosUIAutomation.class.isAssignableFrom(contextClass)) {142                return ((FindsByIosUIAutomation<?>) context)143                    .findElementByIosUIAutomation(getLocatorString());144            }145            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {146                return super.findElement(context);147            }148            throw formIllegalArgumentException(contextClass, FindsByIosUIAutomation.class,149                FindsByFluentSelector.class);150        }151        @Override public String toString() {152            return "By.IosUIAutomation: " + getLocatorString();153        }154    }155    public static class ByAndroidUIAutomator extends MobileBy implements Serializable {156        public ByAndroidUIAutomator(String uiautomatorText) {157            super(MobileSelector.ANDROID_UI_AUTOMATOR, uiautomatorText);158        }159        /**160         * @throws WebDriverException when current session doesn't support the given selector or when161         *      value of the selector is not consistent.162         * @throws IllegalArgumentException when it is impossible to find something on the given163         * {@link SearchContext} instance164         */165        @SuppressWarnings("unchecked")166        @Override167        public List<WebElement> findElements(SearchContext context) throws WebDriverException,168            IllegalArgumentException {169            Class<?> contextClass = context.getClass();170            if (FindsByAndroidUIAutomator.class.isAssignableFrom(contextClass)) {171                return FindsByAndroidUIAutomator.class.cast(context)172                    .findElementsByAndroidUIAutomator(getLocatorString());173            }174            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {175                return super.findElements(context);176            }177            throw formIllegalArgumentException(contextClass, FindsByAndroidUIAutomator.class,178                FindsByFluentSelector.class);179        }180        /**181         * @throws WebDriverException when current session doesn't support the given selector or when182         *      value of the selector is not consistent.183         * @throws IllegalArgumentException when it is impossible to find something on the given184         * {@link SearchContext} instance185         */186        @Override public WebElement findElement(SearchContext context) throws WebDriverException,187            IllegalArgumentException {188            Class<?> contextClass = context.getClass();189            if (FindsByAndroidUIAutomator.class.isAssignableFrom(contextClass)) {190                return FindsByAndroidUIAutomator.class.cast(context)191                    .findElementByAndroidUIAutomator(getLocatorString());192            }193            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {194                return super.findElement(context);195            }196            throw formIllegalArgumentException(contextClass, FindsByAndroidUIAutomator.class,197                FindsByFluentSelector.class);198        }199        @Override public String toString() {200            return "By.AndroidUIAutomator: " + getLocatorString();201        }202    }203    public static class ByAccessibilityId extends MobileBy implements Serializable {204        public ByAccessibilityId(String accessibilityId) {205            super(MobileSelector.ACCESSIBILITY, accessibilityId);206        }207        /**208         * @throws WebDriverException when current session doesn't support the given selector or when209         *      value of the selector is not consistent.210         * @throws IllegalArgumentException when it is impossible to find something on the given211         * {@link SearchContext} instance212         */213        @SuppressWarnings("unchecked")214        @Override215        public List<WebElement> findElements(SearchContext context) throws WebDriverException,216            IllegalArgumentException {217            Class<?> contextClass = context.getClass();218            if (FindsByAccessibilityId.class.isAssignableFrom(contextClass)) {219                return FindsByAccessibilityId.class.cast(context)220                    .findElementsByAccessibilityId(getLocatorString());221            }222            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {223                return super.findElements(context);224            }225            throw formIllegalArgumentException(contextClass, FindsByAccessibilityId.class,226                FindsByFluentSelector.class);227        }228        /**229         * @throws WebDriverException when current session doesn't support the given selector or when230         *      value of the selector is not consistent.231         * @throws IllegalArgumentException when it is impossible to find something on the given232         * {@link SearchContext} instance233         */234        @Override public WebElement findElement(SearchContext context) throws WebDriverException,235            IllegalArgumentException {236            Class<?> contextClass = context.getClass();237            if (FindsByAccessibilityId.class.isAssignableFrom(contextClass)) {238                return FindsByAccessibilityId.class.cast(context)239                    .findElementByAccessibilityId(getLocatorString());240            }241            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {242                return super.findElement(context);243            }244            throw formIllegalArgumentException(contextClass, FindsByAccessibilityId.class,245                FindsByFluentSelector.class);246        }247        @Override public String toString() {248            return "By.AccessibilityId: " + getLocatorString();249        }250    }251    public static class ByIosClassChain extends MobileBy implements Serializable {252        protected ByIosClassChain(String locatorString) {253            super(MobileSelector.IOS_CLASS_CHAIN, locatorString);254        }255        /**256         * @throws WebDriverException when current session doesn't support the given selector or when257         *      value of the selector is not consistent.258         * @throws IllegalArgumentException when it is impossible to find something on the given259         * {@link SearchContext} instance260         */261        @SuppressWarnings("unchecked")262        @Override public List<WebElement> findElements(SearchContext context) {263            Class<?> contextClass = context.getClass();264            if (FindsByIosClassChain.class.isAssignableFrom(contextClass)) {265                return FindsByIosClassChain.class.cast(context)266                        .findElementsByIosClassChain(getLocatorString());267            }268            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {269                return super.findElements(context);270            }271            throw formIllegalArgumentException(contextClass, FindsByIosClassChain.class,272                    FindsByFluentSelector.class);273        }274        /**275         * @throws WebDriverException when current session doesn't support the given selector or when276         *      value of the selector is not consistent.277         * @throws IllegalArgumentException when it is impossible to find something on the given278         * {@link SearchContext} instance279         */280        @Override public WebElement findElement(SearchContext context) {281            Class<?> contextClass = context.getClass();282            if (FindsByIosClassChain.class.isAssignableFrom(contextClass)) {283                return FindsByIosClassChain.class.cast(context)284                        .findElementByIosClassChain(getLocatorString());285            }286            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {287                return super.findElement(context);288            }289            throw formIllegalArgumentException(contextClass, FindsByIosClassChain.class,290                    FindsByFluentSelector.class);291        }292        @Override public String toString() {293            return "By.IosClassChain: " + getLocatorString();294        }295    }296    public static class ByIosNsPredicate extends MobileBy implements Serializable {297        protected ByIosNsPredicate(String locatorString) {298            super(MobileSelector.IOS_PREDICATE_STRING, locatorString);299        }300        /**301         * @throws WebDriverException when current session doesn't support the given selector or when302         *      value of the selector is not consistent.303         * @throws IllegalArgumentException when it is impossible to find something on the given304         * {@link SearchContext} instance305         */306        @SuppressWarnings("unchecked")307        @Override public List<WebElement> findElements(SearchContext context) {308            Class<?> contextClass = context.getClass();309            if (FindsByIosNSPredicate.class.isAssignableFrom(contextClass)) {310                return FindsByIosNSPredicate.class.cast(context)311                        .findElementsByIosNsPredicate(getLocatorString());312            }313            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {314                return super.findElements(context);315            }316            throw formIllegalArgumentException(contextClass, FindsByIosNSPredicate.class,317                    FindsByFluentSelector.class);318        }319        /**320         * @throws WebDriverException when current session doesn't support the given selector or when321         *      value of the selector is not consistent.322         * @throws IllegalArgumentException when it is impossible to find something on the given323         * {@link SearchContext} instance324         */325        @Override public WebElement findElement(SearchContext context) {326            Class<?> contextClass = context.getClass();327            if (FindsByIosNSPredicate.class.isAssignableFrom(contextClass)) {328                return FindsByIosNSPredicate.class.cast(context)329                        .findElementByIosNsPredicate(getLocatorString());330            }331            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {332                return super.findElement(context);333            }334            throw formIllegalArgumentException(contextClass, FindsByIosNSPredicate.class,335                    FindsByFluentSelector.class);336        }337        @Override public String toString() {338            return "By.IosNsPredicate: " + getLocatorString();339        }340    }341    public static class ByWindowsAutomation extends MobileBy implements Serializable {342        protected ByWindowsAutomation(String locatorString) {343            super(MobileSelector.WINDOWS_UI_AUTOMATION, locatorString);344        }345        /**346         * @throws WebDriverException when current session doesn't support the given selector or when347         *      value of the selector is not consistent.348         * @throws IllegalArgumentException when it is impossible to find something on the given349         * {@link SearchContext} instance350         */351        @SuppressWarnings("unchecked")352        @Override public List<WebElement> findElements(SearchContext context) {353            Class<?> contextClass = context.getClass();354            if (FindsByWindowsAutomation.class.isAssignableFrom(contextClass)) {355                return FindsByWindowsAutomation.class.cast(context)356                    .findElementsByWindowsUIAutomation(getLocatorString());357            }358            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {359                return super.findElements(context);360            }361            throw formIllegalArgumentException(contextClass, FindsByWindowsAutomation.class,362                FindsByFluentSelector.class);363        }364        /**365         * @throws WebDriverException when current session doesn't support the given selector or when366         *      value of the selector is not consistent.367         * @throws IllegalArgumentException when it is impossible to find something on the given368         * {@link SearchContext} instance369         */370        @Override public WebElement findElement(SearchContext context) {371            Class<?> contextClass = context.getClass();372            if (FindsByWindowsAutomation.class.isAssignableFrom(contextClass)) {373                return FindsByWindowsAutomation.class.cast(context)374                    .findElementByWindowsUIAutomation(getLocatorString());375            }376            if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {377                return super.findElement(context);378            }379            throw formIllegalArgumentException(contextClass, FindsByIosNSPredicate.class,380                FindsByWindowsAutomation.class);381        }382    }383}...ByParserTest.java
Source:ByParserTest.java  
1/*2 * MIT License3 *4 * Copyright (c) 2018 Excelium5 *6 * Permission is hereby granted, free of charge, to any person obtaining a copy7 * of this software and associated documentation files (the "Software"), to deal8 * in the Software without restriction, including without limitation the rights9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell10 * copies of the Software, and to permit persons to whom the Software is11 * furnished to do so, subject to the following conditions:12 *13 * The above copyright notice and this permission notice shall be included in all14 * copies or substantial portions of the Software.15 *16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22 * SOFTWARE.23 */24package excelium.core.by;25import excelium.core.driver.ContextAwareWebDriver;26import io.appium.java_client.MobileBy;27import mockit.Expectations;28import mockit.Mocked;29import org.junit.Assert;30import org.junit.Test;31import org.openqa.selenium.By;32import org.openqa.selenium.support.ByIdOrName;33/**34 * Tests for {@link ByParser}.35 *36 * @author PhungDucKien37 * @since 2018.05.1638 */39public class ByParserTest {40    @Mocked41    private ContextAwareWebDriver webDriver;42    @Test43    public void testParseByWeb() {44        new Expectations() {{45            webDriver.isWebContext(); result = true;46        }};47        By by = ByParser.parseBy("id=id", webDriver);48        Assert.assertTrue(by instanceof By.ById);49        Assert.assertEquals("By.id: id", by.toString());50        by = ByParser.parseBy("link=link", webDriver);51        Assert.assertTrue(by instanceof By.ByLinkText);52        Assert.assertEquals("By.linkText: link", by.toString());53        by = ByParser.parseBy("partial link=link", webDriver);54        Assert.assertTrue(by instanceof By.ByPartialLinkText);55        Assert.assertEquals("By.partialLinkText: link", by.toString());56        by = ByParser.parseBy("tag=button", webDriver);57        Assert.assertTrue(by instanceof By.ByTagName);58        Assert.assertEquals("By.tagName: button", by.toString());59        by = ByParser.parseBy("name=name", webDriver);60        Assert.assertTrue(by instanceof By.ByName);61        Assert.assertEquals("By.name: name", by.toString());62        by = ByParser.parseBy("class=class", webDriver);63        Assert.assertTrue(by instanceof By.ByClassName);64        Assert.assertEquals("By.className: class", by.toString());65        by = ByParser.parseBy("css=css", webDriver);66        Assert.assertTrue(by instanceof ByCss);67        Assert.assertEquals("By.css: css", by.toString());68        by = ByParser.parseBy("xpath=//xpath", webDriver);69        Assert.assertTrue(by instanceof By.ByXPath);70        Assert.assertEquals("By.xpath: //xpath", by.toString());71        by = ByParser.parseBy("identifier=identifier", webDriver);72        Assert.assertTrue(by instanceof ByIdOrName);73        Assert.assertEquals("by id or name \"identifier\"", by.toString());74        by = ByParser.parseBy("alt=alt", webDriver);75        Assert.assertTrue(by instanceof ByAlt);76        Assert.assertEquals("By.alt: alt", by.toString());77        by = ByParser.parseBy("dom=dom", webDriver);78        Assert.assertTrue(by instanceof ByDom);79        Assert.assertEquals("By.dom: dom", by.toString());80        by = ByParser.parseBy("index=1", webDriver);81        Assert.assertTrue(by instanceof ByIndex);82        Assert.assertEquals("By.index: [null][1]", by.toString());83        by = ByParser.parseBy("variable=variable", webDriver);84        Assert.assertTrue(by instanceof ByVariable);85        Assert.assertEquals("By.variable: variable", by.toString());86        by = ByParser.parseBy("//xpath", webDriver);87        Assert.assertTrue(by instanceof By.ByXPath);88        Assert.assertEquals("By.xpath: //xpath", by.toString());89        by = ByParser.parseBy("document.getElementById('login-btn')", webDriver);90        Assert.assertTrue(by instanceof ByDom);91        Assert.assertEquals("By.dom: document.getElementById('login-btn')", by.toString());92        by = ByParser.parseBy("identifier", webDriver);93        Assert.assertTrue(by instanceof ByIdOrName);94        Assert.assertEquals("by id or name \"identifier\"", by.toString());95    }96    @Test97    public void testParseByMobile() {98        new Expectations() {{99            webDriver.isWebContext(); result = false;100        }};101        By by = ByParser.parseBy("accessibility id=accessibility id", webDriver);102        Assert.assertTrue(by instanceof MobileBy.ByAccessibilityId);103        Assert.assertEquals("By.AccessibilityId: accessibility id", by.toString());104        by = ByParser.parseBy("class=class", webDriver);105        Assert.assertTrue(by instanceof MobileBy.ByClassName);106        Assert.assertEquals("By.className: class", by.toString());107        by = ByParser.parseBy("id=id", webDriver);108        Assert.assertTrue(by instanceof MobileBy.ById);109        Assert.assertEquals("By.id: id", by.toString());110        by = ByParser.parseBy("name=name", webDriver);111        Assert.assertTrue(by instanceof MobileBy.ByName);112        Assert.assertEquals("By.name: name", by.toString());113        by = ByParser.parseBy("xpath=//xpath", webDriver);114        Assert.assertTrue(by instanceof MobileBy.ByXPath);115        Assert.assertEquals("By.xpath: //xpath", by.toString());116        by = ByParser.parseBy("android uiautomator=android uiautomator", webDriver);117        Assert.assertTrue(by instanceof MobileBy.ByAndroidUIAutomator);118        Assert.assertEquals("By.AndroidUIAutomator: android uiautomator", by.toString());119        by = ByParser.parseBy("android viewtag=android viewtag", webDriver);120        Assert.assertTrue(by instanceof MobileBy.ByAndroidViewTag);121        Assert.assertEquals("By.AndroidViewTag: android viewtag", by.toString());122        by = ByParser.parseBy("android datamatcher=android datamatcher", webDriver);123        Assert.assertTrue(by instanceof MobileBy.ByAndroidDataMatcher);124        Assert.assertEquals("By.FindsByAndroidDataMatcher: android datamatcher", by.toString());125        by = ByParser.parseBy("ios predicate string=ios predicate string", webDriver);126        Assert.assertTrue(by instanceof MobileBy.ByIosNsPredicate);127        Assert.assertEquals("By.IosNsPredicate: ios predicate string", by.toString());128        by = ByParser.parseBy("ios class chain=ios class chain", webDriver);129        Assert.assertTrue(by instanceof MobileBy.ByIosClassChain);130        Assert.assertEquals("By.IosClassChain: ios class chain", by.toString());131        by = ByParser.parseBy("windows uiautomation=windows uiautomation", webDriver);132        Assert.assertTrue(by instanceof MobileBy.ByWindowsAutomation);133        by = ByParser.parseBy("index=1", webDriver);134        Assert.assertTrue(by instanceof ByIndex);135        Assert.assertEquals("By.index: [null][1]", by.toString());136        by = ByParser.parseBy("variable=variable", webDriver);137        Assert.assertTrue(by instanceof ByVariable);138        Assert.assertEquals("By.variable: variable", by.toString());139        by = ByParser.parseBy("//xpath", webDriver);140        Assert.assertTrue(by instanceof MobileBy.ByXPath);141        Assert.assertEquals("By.xpath: //xpath", by.toString());142        by = ByParser.parseBy("accessibility id", webDriver);143        Assert.assertTrue(by instanceof MobileBy.ByAccessibilityId);144        Assert.assertEquals("By.AccessibilityId: accessibility id", by.toString());145    }146}...InjectionAnnotationsTest.java
Source:InjectionAnnotationsTest.java  
1package org.fluentlenium.core.inject;2import io.appium.java_client.AppiumBy;3import io.appium.java_client.MobileBy;4import io.appium.java_client.pagefactory.*;5import io.appium.java_client.pagefactory.bys.ContentMappedBy;6import io.appium.java_client.remote.MobileCapabilityType;7import org.fluentlenium.configuration.ConfigurationException;8import org.fluentlenium.core.domain.FluentWebElement;9import org.junit.Ignore;10import org.junit.Test;11import org.openqa.selenium.By;12import org.openqa.selenium.Capabilities;13import org.openqa.selenium.remote.CapabilityType;14import org.openqa.selenium.remote.DesiredCapabilities;15import org.openqa.selenium.support.FindBy;16import org.openqa.selenium.support.pagefactory.ByChained;17import java.lang.reflect.Field;18import static org.assertj.core.api.Assertions.assertThat;19import static org.assertj.core.api.Assertions.assertThatThrownBy;20public class InjectionAnnotationsTest {21    @FindBy(css = "css")22    private FluentWebElement css;23    @FindBy(xpath = "xpath")24    private FluentWebElement xpath;25    @iOSXCUITFindBy(accessibility = "iosAccessibilityId")26    private FluentWebElement iosAccessibilityId;27    @iOSXCUITFindBys({@iOSXCUITBy(id = "oneline"), @iOSXCUITBy(className = "small")})28    private FluentWebElement iosFindBys;29    @AndroidFindBy(uiAutomator = "androidUiAutomator")30    private FluentWebElement androidUiAutomator;31    @WindowsFindBy(windowsAutomation = "windows")32    private FluentWebElement windowsAutomation;33    @AndroidFindBy(accessibility = "android")34    @iOSXCUITFindBy(tagName = "ios")35    private FluentWebElement multiPlatformElement;36    @Test37    public void shouldInjectCssField() throws NoSuchFieldException {38        Field cssField = this.getClass().getDeclaredField("css");39        InjectionAnnotations annotations = new InjectionAnnotations(cssField, null);40        By by = annotations.buildBy();41        assertThat(by).isInstanceOf(By.ByCssSelector.class).isEqualTo(By.cssSelector("css"));42    }43    @Test44    public void shouldInjectXpathField() throws NoSuchFieldException {45        Field xpathField = this.getClass().getDeclaredField("xpath");46        InjectionAnnotations annotations = new InjectionAnnotations(xpathField, null);47        By by = annotations.buildBy();48        assertThat(by).isInstanceOf(By.ByXPath.class).isEqualTo(By.xpath("xpath"));49    }50    @Test51    public void shouldInjectIosAccessibilityIdField() throws NoSuchFieldException {52        Field accessibilityField = this.getClass().getDeclaredField("iosAccessibilityId");53        InjectionAnnotations annotations = new InjectionAnnotations(accessibilityField, getIosCapablities());54        By by = annotations.buildBy();55        assertThat(by).isInstanceOf(ContentMappedBy.class)56                .isEqualTo(new ByChained(AppiumBy.accessibilityId("iosAccessibilityId")));57    }58    @Test59    public void shouldInjectIosFindAllField() throws NoSuchFieldException {60        Field iosFindAllField = this.getClass().getDeclaredField("iosFindBys");61        InjectionAnnotations annotations = new InjectionAnnotations(iosFindAllField, getIosCapablities());62        By by = annotations.buildBy();63        assertThat(by).isInstanceOf(ContentMappedBy.class);64        ByChained expectedBy = new ByChained(new ByChained(MobileBy.id("oneline"), MobileBy.className("small")));65        assertThat(by).isEqualTo(expectedBy);66    }67    @Test68    public void shouldInjectAndroidAccessibilityIdField() throws NoSuchFieldException {69        Field uiAutomator = this.getClass().getDeclaredField("androidUiAutomator");70        InjectionAnnotations annotations = new InjectionAnnotations(uiAutomator, getAndroidCapablities());71        By by = annotations.buildBy();72        assertThat(by).isInstanceOf(ContentMappedBy.class)73                .isEqualTo(new ByChained(AppiumBy.androidUIAutomator("androidUiAutomator")));74    }75    @Test76    @Ignore77    public void shouldInjectWindowsAutomationField() throws NoSuchFieldException {78        Field windowsField = this.getClass().getDeclaredField("windowsAutomation");79        InjectionAnnotations annotations = new InjectionAnnotations(windowsField, getWindowsCapabilities());80        By by = annotations.buildBy();81        assertThat(by).isInstanceOf(ContentMappedBy.class)82                .isEqualTo(new ByChained(MobileBy.ByWindowsAutomation.windowsAutomation("windowsAutomation")));83    }84    @Test85    public void shouldThrowExceptionWhenCapabilitiesAreIncomplete() throws NoSuchFieldException {86        Field androidUiAutomatorField = this.getClass().getDeclaredField("androidUiAutomator");87        assertThatThrownBy(() -> new InjectionAnnotations(androidUiAutomatorField, getIncompleteAndroidCapabilties()))88                .isInstanceOf(ConfigurationException.class)89                .hasMessageContaining("You have annotated elements with Appium @FindBys but capabilities are incomplete");90    }91    @Test92    public void voidShouldPickCorrectSelectorWhenOnMultiplePlatform() throws NoSuchFieldException {93        Field field = this.getClass().getDeclaredField("multiPlatformElement");94        By androidBy = new InjectionAnnotations(field, getAndroidCapablities()).buildBy();95        assertThat(androidBy).isEqualTo(new ByChained(AppiumBy.accessibilityId("android")));96        By iosBy = new InjectionAnnotations(field, getIosCapablities()).buildBy();97        assertThat(iosBy).isEqualTo(new ByChained(MobileBy.tagName("ios")));98    }99    private Capabilities getIncompleteAndroidCapabilties() {100        DesiredCapabilities capabilities = new DesiredCapabilities();101        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");102        return capabilities;103    }104    private Capabilities getAndroidCapablities() {105        DesiredCapabilities capabilities = new DesiredCapabilities();106        capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");107        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");108        return capabilities;109    }110    private Capabilities getIosCapablities() {111        DesiredCapabilities capabilities = new DesiredCapabilities();112        capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");113        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");114        return capabilities;115    }116    private Capabilities getWindowsCapabilities() {117        DesiredCapabilities capabilities = new DesiredCapabilities();118        capabilities.setCapability(CapabilityType.PLATFORM_NAME, "Windows");119        return capabilities;120    }121}...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 io.appium.java_client.MobileBy;18import io.appium.java_client.pagefactory.AndroidBy;19import io.appium.java_client.pagefactory.AndroidFindBy;20import io.appium.java_client.pagefactory.iOSBy;21import io.appium.java_client.pagefactory.iOSFindBy;22import org.openqa.selenium.By;23import java.lang.annotation.Annotation;24import java.lang.reflect.InvocationTargetException;25import java.lang.reflect.Method;26import java.util.ArrayList;27import java.util.List;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 MobileBy.AndroidUIAutomator(value);35            }36            if (annotation.annotationType().equals(iOSFindBy.class)37                    || annotation.annotationType().equals(iOSBy.class)) {38                return MobileBy.IosUIAutomation(value);39            }40            return super.getBy(annotation);41        }42    },43    BYACCESSABILITY("accessibility") {44        @Override By getBy(Annotation annotation) {45            return MobileBy.AccessibilityId(getValue(annotation, this));46        }47    },48    BYCLASSNAME("className") {49        @Override By getBy(Annotation annotation) {50            return By.className(getValue(annotation, this));51        }52    },53    BYID("id") {54        @Override By getBy(Annotation annotation) {55            return By.id(getValue(annotation, this));56        }57    },58    BYTAG("tagName") {59        @Override By getBy(Annotation annotation) {60            return By61                .tagName(getValue(annotation, this));62        }63    },64    BYNAME("name") {65        @Override By getBy(Annotation annotation) {66            return By67                .name(getValue(annotation, this));68        }69    },70    BYXPATH("xpath") {71        @Override By getBy(Annotation annotation) {72            return By73                .xpath(getValue(annotation, this));74        }75    },76    BYLINKTEXT("linkText") {77        @Override By getBy(Annotation annotation) {78            return By79                .linkText(getValue(annotation, this));80        }81    },82    BYPARTIALLINKTEXT("partialLinkText") {83        @Override By getBy(Annotation annotation) {84            return By85                .partialLinkText(getValue(annotation, this));86        }87    },88    BYWINDOWSAUTOMATION("windowsAutomation") {89        @Override By getBy(Annotation annotation) {90            return MobileBy91                    .windowsAutomation(getValue(annotation, this));92        }93    },94    BY_CLASS_CHAIN("iOSClassChain") {95        @Override By getBy(Annotation annotation) {96            return MobileBy97                    .iOSClassChain(getValue(annotation, this));98        }99    },100    BY_NS_PREDICATE("iOSNsPredicate") {101        @Override By getBy(Annotation annotation) {102            return MobileBy103                    .iOSNsPredicateString(getValue(annotation, this));104        }105    };106    private final String valueName;107    Strategies(String valueName) {108        this.valueName = valueName;109    }110    static List<String> strategiesNames() {111        Strategies[] strategies = values();112        List<String> result = new ArrayList<>();113        for (Strategies strategy : strategies) {114            result.add(strategy.valueName);115        }116        return result;117    }118    private static String getValue(Annotation annotation, Strategies strategy) {119        try {120            Method m = annotation.getClass()121                .getMethod(strategy.valueName, AppiumByBuilder.DEFAULT_ANNOTATION_METHOD_ARGUMENTS);122            return m.invoke(annotation, new Object[] {}).toString();123        } catch (NoSuchMethodException124                | SecurityException125                | IllegalAccessException126                | IllegalArgumentException127                | InvocationTargetException e) {128            throw new RuntimeException(e);129        }130    }131    String returnValueName() {132        return valueName;133    }134    By getBy(Annotation annotation) {135        return null;136    }137}...MobileBy.ByWindowsAutomation
Using AI Code Generation
1package appium;2import java.net.MalformedURLException;3import java.net.URL;4import java.util.concurrent.TimeUnit;5import org.openqa.selenium.By;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.remote.DesiredCapabilities;8import io.appium.java_client.MobileBy;9import io.appium.java_client.windows.WindowsDriver;10public class WindowsApp {11	public static void main(String[] args) throws MalformedURLException, InterruptedException {12		DesiredCapabilities cap = new DesiredCapabilities();13		cap.setCapability("app", "C:\\Windows\\System32\\calc.exe");14		cap.setCapability("platformName", "Windows");15		cap.setCapability("deviceName", "WindowsPC");MobileBy.ByWindowsAutomation
Using AI Code Generation
1MobileElement element = driver.findElement(MobileBy.ByWindowsAutomation2  .windowsAutomation("Appium"));3MobileElement element = driver.findElement(MobileBy.ByWindowsUIAutomation4  .windowsUIAutomation("Appium"));5MobileElement element = driver.findElement(MobileBy.ByAccessibilityId.accessibilityId("Appium"));6MobileElement element = driver.findElement(MobileBy.ByIosUIAutomation.iOSUIAutomation("Appium"));7MobileElement element = driver.findElement(MobileBy.ByAndroidUIAutomator8  .androidUIAutomator("Appium"));9MobileElement element = driver.findElement(MobileBy.ByAndroidViewTag.androidViewTag("Appium"));10MobileElement element = driver.findElement(MobileBy.ByAndroidDataMatcher11  .androidDataMatcher("Appium"));12MobileElement element = driver.findElement(MobileBy.ByAndroidViewMatcher13  .androidViewMatcher("Appium"));14MobileElement element = driver.findElement(MobileBy.ByIosNsPredicate.iOSNsPredicate("Appium"));15MobileElement element = driver.findElement(MobileBy.ByIosClassChain.iOSClassChain("Appium"));16MobileElement element = driver.findElement(MobileBy.ByIosUIAutomation.iOSUIAutomation("Appium"));17MobileElement element = driver.findElement(MobileBy.ByIosClassChain.iOSClassChain("Appium"));18MobileElement element = driver.findElement(MobileMobileBy.ByWindowsAutomation
Using AI Code Generation
1import io.appium.java_client.MobileBy;2import org.openqa.selenium.By;3By by = MobileBy.ByWindowsAutomation.id("id");4driver.findElement(by);5from appium.webdriver.common.mobileby import MobileBy6from appium.webdriver.webdriver import WebDriver7by = MobileBy.ByWindowsAutomation.id("id")8driver.find_element(by)9by = Appium::MobileBy::ByWindowsAutomation.id("id")10driver.find_element(by)11var webdriver = require('appium').webdriver;12var MobileBy = webdriver.MobileBy;13var by = MobileBy.ByWindowsAutomation.id("id");14driver.findElement(by);15import { webdriver } from 'appium';16const MobileBy = webdriver.MobileBy;17const by = MobileBy.ByWindowsAutomation.id("id");18driver.findElement(by);19using Appium.WebDriver;20using Appium.WebDriver.Extensions;21var by = MobileBy.ByWindowsAutomation.Id("id");22driver.FindElement(by);23import (24func main() {25	by := mobileby.ByWindowsAutomation.Id("id")26	driver.FindElement(by)27}28use Facebook\WebDriver\Remote\DesiredCapabilities;29use Facebook\WebDriver\Remote\RemoteWebDriver;30use Facebook\WebDriver\WebDriverBy;31use Facebook\WebDriver\WebDriverElement;32use Facebook\WebDriver\WebDriverWait;33use Facebook\WebDriver\WebDriverExpectedCondition;34use Facebook\WebDriver\WebDriverDimension;35use Facebook\WebDriver\WebDriverPoint;36use Facebook\WebDriver\WebDriverSelect;37use Facebook\WebDriver\WebDriverKeys;38use Facebook\WebDriver\WebDriverAction;39use Facebook\WebDriver\WebDriverNavigation;MobileBy.ByWindowsAutomation
Using AI Code Generation
1MobileElement element = driver.findElement(MobileBy.ByWindowsAutomation("name=SomeName"));2element = driver.find_element_by_android_uiautomator('name=SomeName')3element = driver.findElement(MobileBy.ByWindowsAutomation('name=SomeName'))4element = driver.find_element(:windows_automation, 'name=SomeName')5element = driver.find_element(:windows_automation, 'name=SomeName')6$element = $driver->findElement(WebDriverBy::windowsAutomation('name=SomeName'))7IWebElement element = driver.FindElement(MobileBy.ByWindowsAutomation("name=SomeName"));8let element = try! driver.findElement(MobileBy.ByWindowsAutomation("name=SomeName"))9let element = driver.findElement(MobileBy.ByWindowsAutomation("name=SomeName"))10element, _ := driver.FindElement(MobileBy.ByWindowsAutomation("name=SomeName"))11element = driver.find_element(:windows_automation, 'name=SomeName')12IWebElement element = driver.FindElement(MobileBy.ByWindowsAutomation("name=SomeName"));MobileBy.ByWindowsAutomation
Using AI Code Generation
1MobileElement el1 = (MobileElement) driver.findElementByWindowsAutomation("Name of the element");2MobileElement el2 = (MobileElement) driver.findElementByAccessibilityId("Name of the element");3MobileElement el3 = (MobileElement) driver.findElementByImage("Path of the image");4MobileElement el4 = (MobileElement) driver.findElementByAndroidUIAutomator("Name of the element");5MobileElement el5 = (MobileElement) driver.findElementByAndroidViewTag("Name of the element");6MobileElement el6 = (MobileElement) driver.findElementByAndroidDataMatcher("Name of the element");7MobileElement el7 = (MobileElement) driver.findElementByIosUIAutomation("Name of the element");8MobileElement el8 = (MobileElement) driver.findElementByIosClassChain("Name of the element");9MobileElement el9 = (MobileElement) driver.findElementByIosNsPredicate("Name of the element");10MobileElement el10 = (MobileElement) driver.findElementByIosPredicate("Name of the element");MobileBy.ByWindowsAutomation
Using AI Code Generation
1MobileElement element = driver.findElement(MobileBy.ByWindowsAutomation.id("AutomationId"));2MobileElement element = driver.findElement(MobileBy.ByWindowsAutomation.name("AutomationName"));3MobileElement element = driver.findElement(MobileBy.ByWindowsAutomation.xpath("XPath"));4MobileElement element = driver.findElement(MobileBy.ByWindowsAutomation.className("ClassName"));5MobileElement element = driver.findElement(MobileBy.ByWindowsAutomation.accessibleName("AccessibleName"));6MobileElement element = driver.findElement(MobileBy.ByWindowsAutomation.accessibleRole("AccessibleRole"));7MobileElement element = driver.findElement(MobileBy.ByWindowsAutomation.accessibleValue("AccessibleValue"));8MobileElement element = driver.findElement(MobileBy.ByWindowsAutomation.image("Image"));9MobileElement element = driver.findElement(MobileBy.ByWindowsAutomation.helpText("HelpText"));MobileBy.ByWindowsAutomation
Using AI Code Generation
1import io.appium.java_client.MobileBy;2  .buildByWindowsAutomation("name=MyControlName");3  .build_by_windows_automation("name=MyControlName")4var By = require('appium-support').By;5var by = By.WindowsAutomation("name=MyControlName");6var By = require('appium-support').By;7var by = By.WindowsAutomation("name=MyControlName");8use Facebook\WebDriver\By;9$by = By::WindowsAutomation("name=MyControlName");10from appium.webdriver.common.mobileby import By11by = By.WindowsAutomation("name=MyControlName")12using OpenQA.Selenium.Appium;13using OpenQA.Selenium.Appium.Windows;14WindowsElement element = session.FindElementByWindowsAutomation("name=MyControlName");15  .build_by_windows_automation("name=MyControlName")16var By = require('appium-support').By;17var by = By.WindowsAutomation("name=MyControlName")18import io.appium.java_client.MobileBy;19  .buildByWindowsAutomation("name=MyControlName")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!!
