androidx.test.uiautomator.UiObject2 Java Examples
The following examples show how to use
androidx.test.uiautomator.UiObject2.
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: UiObjectElement.java From appium-uiautomator2-server with Apache License 2.0 | 6 votes |
@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 #2
Source File: ElementHelpers.java From appium-uiautomator2-server with Apache License 2.0 | 6 votes |
private static int getTouchPadding(AndroidElement element) throws UiObjectNotFoundException, ReflectiveOperationException { final UiObject2 uiObject2 = element instanceof UiObject2Element ? getUiDevice().findObject(By.clazz(((UiObject2) element.getUiObject()).getClassName())) : getUiDevice().findObject(By.clazz(((UiObject) element.getUiObject()).getClassName())); Field gestureField = uiObject2.getClass().getDeclaredField("mGestures"); gestureField.setAccessible(true); Object gestureObject = gestureField.get(uiObject2); Field viewConfigField = gestureObject.getClass().getDeclaredField("mViewConfig"); viewConfigField.setAccessible(true); Object viewConfigObject = viewConfigField.get(gestureObject); Method getScaledPagingTouchSlopMethod = viewConfigObject.getClass().getDeclaredMethod("getScaledPagingTouchSlop"); getScaledPagingTouchSlopMethod.setAccessible(true); int touchPadding = (int) getScaledPagingTouchSlopMethod.invoke(viewConfigObject); return touchPadding / 2; }
Example #3
Source File: ElementHelpers.java From appium-uiautomator2-server with Apache License 2.0 | 6 votes |
@Nullable public static String getText(Object element, boolean replaceNull) { if (element instanceof UiObject2) { /* * If the given element is TOAST element, we can't perform any operation on {@link UiObject2} as it * not formed with valid AccessibilityNodeInfo, Instead we are using custom created AccessibilityNodeInfo of * TOAST Element to retrieve the Text. */ AccessibilityNodeInfo nodeInfo = (AccessibilityNodeInfo) getField(UiObject2.class, "mCachedNode", element); if (nodeInfo != null && Objects.equals(nodeInfo.getClassName(), Toast.class.getName())) { return charSequenceToString(nodeInfo.getText(), replaceNull); } } return AccessibilityNodeInfoHelpers.getText(AccessibilityNodeInfoGetter.fromUiObject(element), replaceNull); }
Example #4
Source File: CustomUiDevice.java From appium-uiautomator2-server with Apache License 2.0 | 6 votes |
@Nullable private UiObject2 toUiObject2(Object selector, AccessibilityNodeInfo node) throws IllegalAccessException, InvocationTargetException, InstantiationException { Object[] constructorParams = {getUiDevice(), selector, node}; long end = SystemClock.uptimeMillis() + UIOBJECT2_CREATION_TIMEOUT; while (true) { Object object2 = uiObject2Constructor.newInstance(constructorParams); if (object2 instanceof UiObject2) { return (UiObject2) object2; } long remainingMillis = end - SystemClock.uptimeMillis(); if (remainingMillis < 0) { return null; } SystemClock.sleep(Math.min(200, remainingMillis)); } }
Example #5
Source File: AlertHelpers.java From appium-uiautomator2-server with Apache License 2.0 | 6 votes |
/** * Accept or dismiss on-screen alert. * * @param action either ACCEPT or DISMISS * @param buttonLabel if this parameter is set then the method * will look for the dialog button with this particular * text instead of the default one (usually it is the first button * for ACCEPT and the last one for DISMISS action) * @return the actual label of the clicked button * @throws NoAlertOpenException if no dialog is present on the screen * @throws InvalidElementStateException if no matching button can be found */ public static String handle(AlertAction action, @Nullable String buttonLabel) { final AlertType alertType = getAlertType(); final UiObject2 dstButton = alertType == AlertType.REGULAR ? getRegularAlertButton(action, buttonLabel) : getPermissionAlertButton(action, buttonLabel); if (dstButton == null) { throw new InvalidElementStateException("The expected button cannot be detected on the alert"); } final String actualLabel = dstButton.getText(); Logger.info(String.format("Clicking alert button '%s' in order to %s it", actualLabel, action.name().toLowerCase())); dstButton.click(); return actualLabel; }
Example #6
Source File: AlertHelpers.java From appium-uiautomator2-server with Apache License 2.0 | 6 votes |
@Nullable private static UiObject2 getRegularAlertButton(AlertAction action, @Nullable String buttonLabel) { final Map<String, UiObject2> alertButtonsMapping = new HashMap<>(); final List<Integer> buttonIndexes = new ArrayList<>(); for (final UiObject2 button : getUiDevice().findObjects(By.res(regularAlertButtonResIdPattern))) { final String resId = button.getResourceName(); alertButtonsMapping.put(resId, button); buttonIndexes.add(Integer.parseInt(resId.substring(regularAlertButtonResIdPrefix.length()))); } if (alertButtonsMapping.isEmpty()) { return null; } Log.d(TAG, String.format("Found %d buttons on the alert", alertButtonsMapping.size())); if (buttonLabel != null) { return filterButtonByLabel(alertButtonsMapping.values(), buttonLabel); } final int minIdx = Collections.min(buttonIndexes); return action == AlertAction.ACCEPT ? alertButtonsMapping.get(buttonResIdByIdx(minIdx)) : alertButtonsMapping.get(buttonResIdByIdx(alertButtonsMapping.size() > 1 ? minIdx + 1 : minIdx)); }
Example #7
Source File: UiObject2Element.java From appium-uiautomator2-server with Apache License 2.0 | 6 votes |
@Override public boolean dragTo(Object destObj, int steps) throws UiObjectNotFoundException { if (destObj instanceof UiObject) { int destX = ((UiObject) destObj).getBounds().centerX(); int destY = ((UiObject) destObj).getBounds().centerY(); element.drag(new android.graphics.Point(destX, destY), steps); return true; } if (destObj instanceof UiObject2) { android.graphics.Point coord = ((UiObject2) destObj).getVisibleCenter(); element.drag(coord, steps); return true; } Logger.error("Destination should be either UiObject or UiObject2"); return false; }
Example #8
Source File: UiObjectElement.java From appium-uiautomator2-server with Apache License 2.0 | 6 votes |
@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 #9
Source File: BaseElementHandler.java From za-Farmer with MIT License | 6 votes |
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 #10
Source File: BaseElementHandler.java From za-Farmer with MIT License | 6 votes |
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 #11
Source File: ChangeTextBehaviorTest.java From testing-samples with Apache License 2.0 | 5 votes |
@Test public void testChangeText_newActivity() { // Type text and then press the button. mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "editTextUserInput")) .setText(STRING_TO_BE_TYPED); mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "activityChangeTextBtn")) .click(); // Verify the test is displayed in the Ui UiObject2 changedText = mDevice .wait(Until.findObject(By.res(BASIC_SAMPLE_PACKAGE, "show_text_view")), 500 /* wait 500ms */); assertThat(changedText.getText(), is(equalTo(STRING_TO_BE_TYPED))); }
Example #12
Source File: UiObjectElement.java From appium-uiautomator2-server with Apache License 2.0 | 5 votes |
@Override public boolean dragTo(final Object destObj, final int steps) throws UiObjectNotFoundException { if (destObj instanceof UiObject) { return element.dragTo((UiObject) destObj, steps); } if (destObj instanceof UiObject2) { android.graphics.Point coords = ((UiObject2) destObj).getVisibleCenter(); return dragTo(coords.x, coords.y, steps); } Logger.error("Destination should be either UiObject or UiObject2"); return false; }
Example #13
Source File: UiObjectChildGenerator.java From appium-uiautomator2-server with Apache License 2.0 | 5 votes |
private void init() { if (parent instanceof UiObject) { try { childCount = ((UiObject) parent).getChildCount(); } catch (UiObjectNotFoundException e) { Logger.error("Could not get child count from UiObject"); childCount = 0; } } else { uiObject2Children = ((UiObject2) parent).getChildren(); childCount = uiObject2Children.size(); } curChild = 0; }
Example #14
Source File: UiObject2Element.java From appium-uiautomator2-server with Apache License 2.0 | 5 votes |
public UiObject2Element(String id, UiObject2 element, boolean isSingleMatch, By by, @Nullable String contextId) { this.id = id; this.element = element; this.by = by; this.contextId = contextId; this.isSingleMatch = isSingleMatch; }
Example #15
Source File: AccessibilityNodeInfoGetter.java From appium-uiautomator2-server with Apache License 2.0 | 5 votes |
@Nullable public static AccessibilityNodeInfo fromUiObject(Object object, long timeout) { if (object instanceof UiObject2) { return (AccessibilityNodeInfo) invoke(method(UiObject2.class, "getAccessibilityNodeInfo"), object); } else if (object instanceof UiObject) { return (AccessibilityNodeInfo) invoke(method(UiObject.class, "findAccessibilityNodeInfo", long.class), object, timeout); } throw new UiAutomator2Exception("Unknown object type: " + object.getClass().getName()); }
Example #16
Source File: ChangeTextBehaviorTest.java From testing-samples with Apache License 2.0 | 5 votes |
@Test public void testChangeText_sameActivity() { // Type text and then press the button. mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "editTextUserInput")) .setText(STRING_TO_BE_TYPED); mDevice.findObject(By.res(BASIC_SAMPLE_PACKAGE, "changeTextBt")) .click(); // Verify the test is displayed in the Ui UiObject2 changedText = mDevice .wait(Until.findObject(By.res(BASIC_SAMPLE_PACKAGE, "textToBeChanged")), 500 /* wait 500ms */); assertThat(changedText.getText(), is(equalTo(STRING_TO_BE_TYPED))); }
Example #17
Source File: Input.java From za-Farmer with MIT License | 5 votes |
@Override protected void runSelf() throws UiObjectNotFoundException { UiObject2 uiObject = getElement(); screenshot(TYPE_LOG_SUCCESS,uiObject); uiObject.setText(getReText()); }
Example #18
Source File: CustomUiDevice.java From appium-uiautomator2-server with Apache License 2.0 | 5 votes |
/** * 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 #19
Source File: AlertHelpers.java From appium-uiautomator2-server with Apache License 2.0 | 5 votes |
/** * @return The actual text of the on-screen dialog. An empty * string is going to be returned if the dialog contains no text. * @throws NoAlertOpenException if no dialog is present on the screen */ public static String getText() { final AlertType alertType = getAlertType(); final List<UiObject2> alertRoots = getUiDevice().findObjects(By.res(alertContentResId)); if (alertRoots.isEmpty()) { Log.w(TAG, "Alert content container is missing"); throw new NoAlertOpenException(); } final List<String> result = new ArrayList<>(); final List<UiObject2> alertElements = alertRoots.get(0).findObjects(By.res(alertElementsResIdPattern)); Log.d(TAG, String.format("Detected %d alert elements", alertElements.size())); final String alertButtonsResIdPattern = alertType == AlertType.REGULAR ? regularAlertButtonResIdPattern.toString() : permissionAlertButtonResIdPattern.toString(); for (final UiObject2 element : alertElements) { final String resName = element.getResourceName(); if (resName == null || resName.matches(alertButtonsResIdPattern)) { continue; } final String text = element.getText(); if (StringHelpers.isBlank(text)) { continue; } result.add(text); } return join("\n", result); }
Example #20
Source File: AlertHelpers.java From appium-uiautomator2-server with Apache License 2.0 | 5 votes |
@Nullable private static UiObject2 filterButtonByLabel(Collection<UiObject2> buttons, String label) { for (UiObject2 button : buttons) { if (Objects.equals(button.getText(), label)) { return button; } } return null; }
Example #21
Source File: Device.java From appium-uiautomator2-server with Apache License 2.0 | 5 votes |
public static AndroidElement getAndroidElement(String id, Object element, boolean isSingleMatch, @Nullable By by, @Nullable String contextId) throws UiAutomator2Exception { if (element instanceof UiObject2) { return new UiObject2Element(id, (UiObject2) element, isSingleMatch, by, contextId); } else if (element instanceof UiObject) { return new UiObjectElement(id, (UiObject) element, isSingleMatch, by, contextId); } else { throw new UiAutomator2Exception("Unknown Element type: " + element.getClass().getName()); } }
Example #22
Source File: PlaybackControlTest.java From CastVideos-android with Apache License 2.0 | 5 votes |
/** * Scrub progress bar and verify the current duration */ @Test public void testProgressBarControl() throws UiObjectNotFoundException, InterruptedException { mTestUtils.connectToCastDevice(); mTestUtils.playCastContent(VIDEO_WITHOUT_SUBTITLES); mTestUtils.assertPlayerState(MediaStatus.PLAYER_STATE_PLAYING, MAX_TIMEOUT_MS); UiObject2 progressBar = mDevice.findObject(By.res(resources.getResourceName(R.id.cast_seek_bar))); progressBar.scroll(Direction.LEFT, 0.75f, 500); mTestUtils.assertStreamPosition(0.75f); mTestUtils.disconnectFromCastDevice(); }
Example #23
Source File: BaseElementHandler.java From za-Farmer with MIT License | 5 votes |
public UiObject2 getElement() throws UiObjectNotFoundException { BySelector selector = getElementSelector(); if (this.scrollFind == 1) { return getElement(selector, this.scrollCount, "up"); } else { return getElement(selector, 10000); } }
Example #24
Source File: Click.java From za-Farmer with MIT License | 5 votes |
@Override protected void runSelf() throws UiObjectNotFoundException { UiObject2 uiObject = getElement(); screenshot(TYPE_LOG_SUCCESS, uiObject); uiObject.click(); }
Example #25
Source File: UiObject2Element.java From appium-uiautomator2-server with Apache License 2.0 | 4 votes |
@Override public UiObject2 getUiObject() { return element; }
Example #26
Source File: AndroidTestsUtility.java From line-sdk-android with Apache License 2.0 | 4 votes |
public static boolean checkElementExists(String className) { UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); uiDevice.wait(Until.hasObject(By.clazz(className)), AndroidTestsConfig.TIMEOUT); UiObject2 element = uiDevice.findObject(By.clazz(className)); return element.isEnabled() && element.isClickable() && element.isFocusable(); }
Example #27
Source File: LongClick.java From za-Farmer with MIT License | 4 votes |
@Override protected void runSelf() throws UiObjectNotFoundException { UiObject2 uiObject = getElement(); screenshot(TYPE_LOG_SUCCESS,uiObject); uiObject.longClick(); }
Example #28
Source File: UiObjectChildGenerator.java From appium-uiautomator2-server with Apache License 2.0 | 4 votes |
public UiObjectChildGenerator(Object parent) throws InvalidClassException { if (!(parent instanceof UiObject) && !(parent instanceof UiObject2)) { throw new InvalidClassException("Parent must be UiObject or UiObject 2"); } this.parent = parent; }
Example #29
Source File: BaseStep.java From za-Farmer with MIT License | 4 votes |
public void screenshot(String type, UiObject2 obj) { ICanvasHandler handler = new RectCanvasHandler(obj.getVisibleBounds()); screenShotLog(type, handler); }
Example #30
Source File: GlobalEventListener.java From za-Farmer with MIT License | 4 votes |
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); } } } ); }