androidx.test.uiautomator.BySelector Java Examples

The following examples show how to use androidx.test.uiautomator.BySelector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseElementHandler.java    From za-Farmer with MIT License 6 votes vote down vote up
public UiObject2 getElement(BySelector bySelector, int timeout) throws UiObjectNotFoundException {

        UiObject2 uiObject;
        if (this.order > 0) {
            List<UiObject2> objs = getUiDevice().wait(Until.findObjects(bySelector), timeout);
            if (objs == null) {
                uiObject = null;
            } else {
                uiObject = objs.get(this.order - 1);
            }
        } else {
            uiObject = getUiDevice().wait(Until.findObject(bySelector), timeout);
        }
        if (uiObject == null)
            throw new UiObjectNotFoundException("element not found");

        return uiObject;
    }
 
Example #2
Source File: BaseElementHandler.java    From za-Farmer with MIT License 6 votes vote down vote up
public UiObject2 getElement(BySelector bySelector, int swipCount, String direction) throws UiObjectNotFoundException {
    UiObject2 uiObject = null;
    for (int i = 0; i < swipCount; i++) {
        uiObject = getUiDevice().wait(Until.findObject(bySelector), 1000);
        if (uiObject != null) {
            break;
        }
        Swipe upSwip = new Swipe();
        upSwip.setDirection(direction);
        upSwip.runSelf();
    }
    if (uiObject == null) {
        throw new UiObjectNotFoundException("element not found");
        // bySelector.
    } else {
        return uiObject;
    }
}
 
Example #3
Source File: UiObjectElement.java    From appium-uiautomator2-server with Apache License 2.0 6 votes vote down vote up
@Override
public List<?> getChildren(final Object selector, final By by) throws UiObjectNotFoundException {
    if (selector instanceof BySelector) {
        /*
         * We can't find the child elements with BySelector on UiObject,
         * as an alternative creating UiObject2 with UiObject's AccessibilityNodeInfo
         * and finding the child elements on UiObject2.
         */
        AccessibilityNodeInfo nodeInfo = AccessibilityNodeInfoGetter.fromUiObject(element);
        UiObject2 uiObject2 = (UiObject2) CustomUiDevice.getInstance().findObject(nodeInfo);
        if (uiObject2 == null) {
            throw new ElementNotFoundException();
        }
        return uiObject2.findObjects((BySelector) selector);
    }
    return this.getChildElements((UiSelector) selector);
}
 
Example #4
Source File: UiObjectElement.java    From appium-uiautomator2-server with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Object getChild(final Object selector) throws UiObjectNotFoundException {
    if (selector instanceof BySelector) {
        /*
         * We can't find the child element with BySelector on UiObject,
         * as an alternative creating UiObject2 with UiObject's AccessibilityNodeInfo
         * and finding the child element on UiObject2.
         */
        AccessibilityNodeInfo nodeInfo = AccessibilityNodeInfoGetter.fromUiObject(element);
        Object uiObject2 = CustomUiDevice.getInstance().findObject(nodeInfo);
        return (uiObject2 instanceof UiObject2)
                ? ((UiObject2) uiObject2).findObject((BySelector) selector) : null;
    }
    return element.getChild((UiSelector) selector);
}
 
Example #5
Source File: UiObject2Element.java    From appium-uiautomator2-server with Apache License 2.0 6 votes vote down vote up
@Override
public List<?> getChildren(final Object selector, final By by) throws UiObjectNotFoundException {
    if (selector instanceof UiSelector) {
        /*
         * We can't find the child elements with UiSelector on UiObject2,
         * as an alternative creating UiObject with UiObject2's AccessibilityNodeInfo
         * and finding the child elements on UiObject.
         */
        AccessibilityNodeInfo nodeInfo = AccessibilityNodeInfoGetter.fromUiObject(element);

        UiSelector uiSelector = new UiSelector();
        CustomUiSelector customUiSelector = new CustomUiSelector(uiSelector);
        uiSelector = customUiSelector.getUiSelector(nodeInfo);
        UiObject uiObject = (UiObject) CustomUiDevice.getInstance().findObject(uiSelector);
        String id = UUID.randomUUID().toString();
        AndroidElement androidElement = getAndroidElement(id, uiObject, true, by, getContextId());
        return androidElement.getChildren(selector, by);
    }
    return element.findObjects((BySelector) selector);
}
 
Example #6
Source File: UiObject2Element.java    From appium-uiautomator2-server with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public Object getChild(final Object selector) throws UiObjectNotFoundException {
    if (selector instanceof UiSelector) {
        /*
         * We can't find the child element with UiSelector on UiObject2,
         * as an alternative creating UiObject with UiObject2's AccessibilityNodeInfo
         * and finding the child element on UiObject.
         */
        AccessibilityNodeInfo nodeInfo = AccessibilityNodeInfoGetter.fromUiObject(element);

        UiSelector uiSelector = new UiSelector();
        CustomUiSelector customUiSelector = new CustomUiSelector(uiSelector);
        uiSelector = customUiSelector.getUiSelector(nodeInfo);
        Object uiObject = CustomUiDevice.getInstance().findObject(uiSelector);
        if (uiObject instanceof UiObject) {
            AccessibilityNodeInfoGetter.fromUiObject(element);
            return ((UiObject) uiObject).getChild((UiSelector) selector);
        }
        return null;
    }
    return element.findObject((BySelector) selector);
}
 
Example #7
Source File: CustomUiDevice.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
@Nullable
private static BySelector toSelector(@Nullable AccessibilityNodeInfo nodeInfo) {
    if (nodeInfo == null) {
        return null;
    }
    final CharSequence className = nodeInfo.getClassName();
    return className == null ? null : By.clazz(className.toString());
}
 
Example #8
Source File: UiObjectMatcher.java    From device-automator with MIT License 5 votes vote down vote up
/**
 * Find a view based on the exact text contained within the view. Matching is case-insensitive.
 *
 * @param text Exact text in the view.
 * @param klass Expected class of the view.
 * @return
 */
public static UiObjectMatcher withText(String text, Class klass) {
    Pattern pattern = Pattern.compile("(?i)" + Pattern.quote(text));

    UiSelector uiSelector = new UiSelector()
            .textMatches(pattern.pattern());
    BySelector bySelector = By.text(pattern);

    if (klass != null) {
        uiSelector = uiSelector.className(klass);
        bySelector.clazz(klass);
    }

    return new UiObjectMatcher(uiSelector, bySelector);
}
 
Example #9
Source File: UiObjectMatcher.java    From device-automator with MIT License 5 votes vote down vote up
/**
 * Find a view based on the text contained within the view. The matching is case-sensitive.
 *
 * @param text Text to search for inside a view.
 * @param klass Expected class of the view.
 * @return
 */
public static UiObjectMatcher withTextContaining(String text, Class klass) {
    UiSelector uiSelector = new UiSelector()
            .textContains(text);
    BySelector bySelector = By.textContains(text);

    if (klass != null) {
        uiSelector = uiSelector.className(klass);
        bySelector.clazz(klass);
    }

    return new UiObjectMatcher(uiSelector, bySelector);
}
 
Example #10
Source File: UiObjectMatcher.java    From device-automator with MIT License 5 votes vote down vote up
/**
 * Find a view based on the prefixed text in the view. The matching is case-insensitive.
 *
 * @param text Prefix to search for.
 * @param klass Expected class of the view.
 * @return
 */
public static UiObjectMatcher withTextStartingWith(String text, Class klass) {
    UiSelector uiSelector = new UiSelector()
            .textStartsWith(text);
    BySelector bySelector = By.textStartsWith(text);

    if (klass != null) {
        uiSelector = uiSelector.className(klass);
        bySelector.clazz(klass);
    }

    return new UiObjectMatcher(uiSelector, bySelector);
}
 
Example #11
Source File: UiObjectMatcher.java    From device-automator with MIT License 5 votes vote down vote up
/**
 * Find a view based on the resource id. Resource ids should be the fully qualified id,
 * ex: com.android.browser:id/url
 *
 * @param id The fully qualified id of the view, ex: com.android.browser:id/url
 * @param klass Expected class of the view.
 * @return
 */
public static UiObjectMatcher withResourceId(String id, Class klass) {
    UiSelector uiSelector = new UiSelector()
            .resourceId(id);
    BySelector bySelector = By.res(id);

    if (klass != null) {
        uiSelector = uiSelector.className(klass);
        bySelector.clazz(klass);
    }

    return new UiObjectMatcher(uiSelector, bySelector);
}
 
Example #12
Source File: CustomUiDevice.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
/**
 * UiDevice in android open source project will Support multi-window searches for API level 21,
 * which has not been implemented in UiAutomatorViewer capture layout hierarchy, to be in sync
 * with UiAutomatorViewer customizing getWindowRoots() method to skip the multi-window search
 * based user passed property
 */
private CustomUiDevice() {
    try {
        this.mInstrumentation = (Instrumentation) getField(UiDevice.class, FIELD_M_INSTRUMENTATION, Device.getUiDevice());
        this.API_LEVEL_ACTUAL = getField(UiDevice.class, FIELD_API_LEVEL_ACTUAL, Device.getUiDevice());
        this.ByMatcherClass = ReflectionUtils.getClass("androidx.test.uiautomator.ByMatcher");
        this.METHOD_FIND_MATCH = method(ByMatcherClass, "findMatch", UiDevice.class, BySelector.class, AccessibilityNodeInfo[].class);
        this.METHOD_FIND_MATCHES = method(ByMatcherClass, "findMatches", UiDevice.class, BySelector.class, AccessibilityNodeInfo[].class);
        this.uiObject2Constructor = UiObject2.class.getDeclaredConstructors()[0];
        this.uiObject2Constructor.setAccessible(true);
    } catch (Exception e) {
        Logger.error("Cannot create CustomUiDevice instance", e);
        throw e;
    }
}
 
Example #13
Source File: ElementHelpers.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
public static AndroidElement findElement(final BySelector ui2BySelector) throws UiAutomator2Exception {
    Object ui2Object = getInstance().findObject(ui2BySelector);
    if (ui2Object == null) {
        throw new ElementNotFoundException();
    }
    return getAndroidElement(UUID.randomUUID().toString(), ui2Object, true);
}
 
Example #14
Source File: ClassInstancePair.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
public BySelector getSelector() {
    String androidClass = getAndroidClass();

    //TODO: remove below comments once code get reviewed
    //below commented line related to UiAutomator V1(bootstrap) version, as we don't have possibility
    // in V2 version to use instance, so directly returning By.clazz
    // new UiSelector().className(androidClass).instance(Integer.parseInt(instance));
    return By.clazz(androidClass);
}
 
Example #15
Source File: UiObjectMatcher.java    From device-automator with MIT License 5 votes vote down vote up
/**
 * Find a view based on it's class.
 *
 * @param klass The class of the view to find.
 * @return
 */
public static UiObjectMatcher withClass(Class klass) {
    UiSelector uiSelector = new UiSelector()
            .className(klass);
    BySelector bySelector = By.clazz(klass);

    return new UiObjectMatcher(uiSelector, bySelector);
}
 
Example #16
Source File: BaseElementHandler.java    From za-Farmer with MIT License 5 votes vote down vote up
public UiObject2 getElement() throws UiObjectNotFoundException {
    BySelector selector = getElementSelector();
    if (this.scrollFind == 1) {
        return getElement(selector, this.scrollCount, "up");
    } else {
        return getElement(selector, 10000);
    }
}
 
Example #17
Source File: UiObjectMatcher.java    From device-automator with MIT License 4 votes vote down vote up
public BySelector getBySelector() {
    return mBySelector;
}
 
Example #18
Source File: OpenApplication.java    From za-Farmer with MIT License 4 votes vote down vote up
@Override
protected void runSelf() throws Exception {

    //Part One 输入app版本log

    PackageInfo info = InstrumentationRegistry.getContext().getPackageManager().getPackageInfo(this.getPackageName(), 0);
    String appversion = info.versionName;
    LogUtils.getInstance().info("appversion" + appversion);


    int sdk = VERSION.SDK_INT;

    if (this.clear) {
        //清掉缓存

        if (sdk < 21) {
            //清缓存待定
            LogUtils.getInstance().info("sdk版本小于api21,清缓后续处理");
        } else {

            String clearoutput = getUiDevice().executeShellCommand("pm clear " + this.getPackageName());
            LogUtils.getInstance().info("pm clear " + clearoutput);
        }
    }


    Context mContext = InstrumentationRegistry.getContext();
    Intent myIntent = mContext.getPackageManager().getLaunchIntentForPackage(this.getPackageName());
    String AppStartActivity = myIntent.getComponent().getClassName();

    if (sdk < 21) {
        //安卓版本5.0以下的启动方式
        mContext.startActivity(myIntent);

    } else {
        //安卓版本5.0以上的启动方式
        String startNoClearoutput = getUiDevice().executeShellCommand("am start -n " + this.getPackageName() + "/" + AppStartActivity);
        LogUtils.getInstance().info(startNoClearoutput);
    }

    BySelector bySelector = By.base();
    bySelector.pkg(this.getPackageName());
    getElement(bySelector, 10000);


    screenshot(TYPE_LOG_SUCCESS);

}
 
Example #19
Source File: UiObjectMatcher.java    From device-automator with MIT License 4 votes vote down vote up
public UiObjectMatcher(UiSelector uiSelector, BySelector bySelector) {
    mUiSelector = uiSelector;
    mBySelector = bySelector;
}
 
Example #20
Source File: BaseElementHandler.java    From za-Farmer with MIT License 4 votes vote down vote up
public BySelector getElementSelector() {
    BySelector bySelector = By.base();

    if (this.elementId != null) {
        bySelector.res(this.elementId);
    }

    if (this.elementText != null) {
        bySelector.text(this.elementText);
    }

    if (this.elementDesc != null) {
        bySelector.desc(this.elementDesc);
    }

    if (this.elementClazz != null) {
        bySelector.clazz(this.elementClazz);
    }

    if (this.elementPackage != null) {
        bySelector.pkg(this.elementPackage);
    }

    if (this.elementTextDesc != null) {
        bySelector.setCustomCheckCriteria(new customCheckCriteria(this.elementTextDesc));
    }

    //contains

    if (this.elementIdContains != null) {
        bySelector.res(containsPattern(this.elementIdContains));
    }

    if (this.elementTextContains != null) {
        bySelector.textContains(this.elementTextContains);
    }

    if (this.elementDescContains != null) {
        bySelector.descContains(this.elementDescContains);
    }

    if (this.elementClazzContains != null) {
        bySelector.clazz(containsPattern(this.elementClazzContains));
    }

    if (this.elementPackageContains != null) {
        bySelector.pkg(containsPattern(this.elementPackageContains));
    }

    if (this.elementTextDescContains != null) {
        bySelector.setCustomCheckCriteria(
                new customCheckCriteria(containsPattern(this.elementTextDescContains))
        );
    }

    //pattern

    if (this.elementIdPattern != null) {
        bySelector.res(Pattern.compile(this.elementIdPattern));
    }

    if (this.elementTextPattern != null) {
        bySelector.text(Pattern.compile(this.elementTextPattern));
    }

    if (this.elementDescPattern != null) {
        bySelector.desc(Pattern.compile(this.elementDescPattern));
    }

    if (this.elementClazzPattern != null) {
        bySelector.clazz(Pattern.compile(this.elementClazzPattern));
    }

    if (this.elementPackagePattern != null) {
        bySelector.pkg(Pattern.compile(this.elementPackagePattern));
    }

    if (this.elementTextDescPattern != null) {
        bySelector.setCustomCheckCriteria(
                new customCheckCriteria(Pattern.compile(this.elementTextDescPattern))
        );
    }

    return bySelector;
}
 
Example #21
Source File: GlobalEventListener.java    From za-Farmer with MIT License 4 votes vote down vote up
private void initListener() {

        Instrumentation mInstrumentation = InstrumentationRegistry.getInstrumentation();
        mInstrumentation.getUiAutomation().setOnAccessibilityEventListener(
                new UiAutomation.OnAccessibilityEventListener() {
                    @Override
                    public void onAccessibilityEvent(AccessibilityEvent event) {
                        try {
                            final int eventType = event.getEventType();
                            //处理权限框
                            if (eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && permissionsWindowHandler) {


                                //package 符合
                                final String packageName = event.getPackageName().toString();
                                if (!Pattern.matches(packages, packageName)) {
                                    return;
                                }


                                //部分机型无法获取全部的Texts,故下面部分进行注释
//                                String btnText = null;
//                                final List<CharSequence> texts = event.getText();
//                                for (CharSequence text : texts) {
//                                    if (Pattern.matches(allowButton, text)) {
//                                        btnText = text.toString();
//                                        break;
//                                    }
//                                }
//
//                                //btnText 符合
//                                if (btnText == null) {
//                                    return;
//                                }
//
//                                //文本日志
//                                LogUtils.getInstance().info("permissions window: package " + packageName
//                                        + ",text " + texts);


                                BySelector permissionsSelector = By.pkg(packageName).text(Pattern.compile(allowButton));
                                UiObject2 obj = uiDevice.findObjectOnce(permissionsSelector);
                                if (obj!= null) {

                                    //截图日志
                                    LogUtils.getInstance().infoScreenshot(new RectCanvasHandler(obj.getVisibleBounds()));
                                    obj.click();

                                }

                            } else if (eventType == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
                                //判断是否是通知事件
                                Parcelable parcelable = event.getParcelableData();
                                //如果不是下拉通知栏消息,则为其它通知信息,包括Toast
                                if (!(parcelable instanceof Notification)) {
                                    List <CharSequence> messageList = event.getText();
                                    for (CharSequence toast_Message : messageList) {
                                        if (!TextUtils.isEmpty(toast_Message)) {
                                            for (IGlobalEventChecker toastChecker : toastCheckerSet) {
                                                toastChecker.check(toast_Message.toString());
                                            }
                                            return;
                                        }
                                    }
                                }
                            }
                        } catch (Exception ex) {
                            LogUtils.getInstance().error(ex);
                        }
                    }
                }
        );
    }
 
Example #22
Source File: UiObjectMatcher.java    From device-automator with MIT License 3 votes vote down vote up
/**
 * Find a view based on the content description of the view. The content-description is typically used by the
 * Android Accessibility framework to provide an audio prompt for the widget when the widget is selected.
 * The content-description for the widget must match exactly with the string in your input argument.
 * Matching is case-sensitive.
 *
 * On {@link android.os.Build.VERSION_CODES#LOLLIPOP} devices and higher than matcher can be
 * used to match views in {@link android.webkit.WebView}s and browsers.
 *
 * @param text Content description of the view.
 * @param klass Expected class of the view.
 * @return
 */
public static UiObjectMatcher withContentDescription(String text, Class klass) {
    UiSelector uiSelector = new UiSelector()
            .description(text);
    BySelector bySelector = By.desc(text);

    if (klass != null) {
        uiSelector = uiSelector.className(klass);
        bySelector.clazz(klass);
    }

    return new UiObjectMatcher(uiSelector, bySelector);
}