public static void allowPermissionsIfNeeded(String permissionNeeded) { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(permissionNeeded)) { sleep(PERMISSIONS_DIALOG_DELAY); UiDevice device = UiDevice.getInstance(getInstrumentation()); UiObject allowPermissions = device.findObject(new UiSelector() .clickable(true) .checkable(false) .index(GRANT_BUTTON_INDEX)); if (allowPermissions.exists()) { allowPermissions.click(); } } } catch (UiObjectNotFoundException e) { System.out.println("There is no permissions dialog to interact with"); } }
@Test public void openAppTest() throws Exception { UiDevice mDevice = UiDevice.getInstance(getInstrumentation()); mDevice.pressHome(); // Bring up the default launcher by searching for a UI component // that matches the content description for the launcher button. UiObject allAppsButton = mDevice .findObject(new UiSelector().description("Apps")); // Perform a click on the button to load the launcher. allAppsButton.clickAndWaitForNewWindow(); // Context of the app under test. Context appContext = InstrumentationRegistry.getTargetContext(); assertEquals("com.wbrawner.simplemarkdown", appContext.getPackageName()); UiScrollable appView = new UiScrollable(new UiSelector().scrollable(true)); UiSelector simpleMarkdownSelector = new UiSelector().text("Simple Markdown"); appView.scrollIntoView(simpleMarkdownSelector); mDevice.findObject(simpleMarkdownSelector).clickAndWaitForNewWindow(); }
@Test public void testChangeText_sameActivity() { UiObject skipButton = mDevice.findObject(new UiSelector() .text("SKIP").className("android.widget.TextView")); // Simulate a user-click on the OK button, if found. try { if (skipButton.exists() && skipButton.isEnabled()) { skipButton.click(); } } catch (UiObjectNotFoundException e) { e.printStackTrace(); } }
@Test public void shareTest() throws InterruptedException, UiObjectNotFoundException { UiObject shareBtn = TestHelper.mDevice.findObject(new UiSelector() .resourceId("org.mozilla.focus.debug:id/share") .enabled(true)); /* Go to a webpage */ TestHelper.inlineAutocompleteEditText.waitForExists(waitingTime); TestHelper.inlineAutocompleteEditText.clearTextField(); TestHelper.inlineAutocompleteEditText.setText("mozilla"); TestHelper.hint.waitForExists(waitingTime); TestHelper.pressEnterKey(); assertTrue(TestHelper.webView.waitForExists(waitingTime)); /* Select share */ TestHelper.menuButton.perform(click()); shareBtn.waitForExists(waitingTime); shareBtn.click(); // For simulators, where apps are not installed, it'll take to message app TestHelper.shareMenuHeader.waitForExists(waitingTime); assertTrue(TestHelper.shareMenuHeader.exists()); assertTrue(TestHelper.shareAppList.exists()); TestHelper.pressBackKey(); }
public static boolean viewExistsContains(UiDevice device, String... textToMatch) throws UiObjectNotFoundException { if (textToMatch == null || textToMatch.length < 1) { return false; } UiObject view; boolean contains = false; for (int i = 0; i < textToMatch.length; i++) { view = device.findObject(new UiSelector().textContains(textToMatch[i])); contains = view.exists(); if (contains) return true; } return false; }
/** * Allows location permission if the runtime permission dialog is shown. */ public static void allowLocationPermissionWhenAsked() { try { if (!PermissionUtils.isLocationGranted(InstrumentationRegistry.getTargetContext())) { TestHelper.sleep(PERMISSIONS_DIALOG_DELAY); final UiDevice device = UiDevice.getInstance(getInstrumentation()); final UiObject allowPermissions = device.findObject(new UiSelector() .clickable(true) .checkable(false) .index(GRANT_BUTTON_INDEX)); if (allowPermissions.exists()) { allowPermissions.click(); } } } catch (final UiObjectNotFoundException e) { Log.e(PermissionTestHelper.class.getSimpleName(), "There is no permissions dialog to interact with."); } }
@Ignore @Test public void testUiAutomatorAPI() throws UiObjectNotFoundException, InterruptedException { UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); UiSelector editTextSelector = new UiSelector().className("android.widget.EditText").text("this is a test").focusable(true); UiObject editTextWidget = device.findObject(editTextSelector); editTextWidget.setText("this is new text"); Thread.sleep(2000); UiSelector buttonSelector = new UiSelector().className("android.widget.Button").text("CLICK ME").clickable(true); UiObject buttonWidget = device.findObject(buttonSelector); buttonWidget.click(); Thread.sleep(2000); }
/** * Opens the standalone mode dialog */ private void openStandaloneModeDialog(){ try { // Display the pop-up UiObject menu = mDevice.findObject( new UiSelector().className("android.widget.ImageView") //.description("Plus d'options") // FIXME Raw french string .description("Autres options") // WARNING FIXME French string used, use instead system R values ); menu.click(); UiObject menuItem = mDevice.findObject( new UiSelector().className("android.widget.TextView").text(InstrumentationRegistry.getTargetContext().getString(R.string.action_standalone)) ); menuItem.click(); } catch ( UiObjectNotFoundException uinfe ){ uinfe.printStackTrace(); fail(uinfe.getMessage()); } }
/** * 测试CollapsingToolbarLayout * 被测Demo下载地址:https://github.com/alidili/DesignSupportDemo * * @throws UiObjectNotFoundException */ public void testA() throws UiObjectNotFoundException { // 获取设备对象 Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); UiDevice uiDevice = UiDevice.getInstance(instrumentation); // 获取上下文 Context context = instrumentation.getContext(); // 启动测试App Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.yang.designsupportdemo"); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); context.startActivity(intent); // 打开CollapsingToolbarLayout String resourceId = "com.yang.designsupportdemo:id/CollapsingToolbarLayout"; UiObject collapsingToolbarLayout = uiDevice.findObject(new UiSelector().resourceId(resourceId)); collapsingToolbarLayout.click(); for (int i = 0; i < 5; i++) { // 向上移动 uiDevice.swipe(uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight(), uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight() / 2, 10); // 向下移动 uiDevice.swipe(uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight() / 2, uiDevice.getDisplayHeight(), 10); } // 点击应用返回按钮 UiObject back = uiDevice.findObject(new UiSelector().description("Navigate up")); back.click(); // 点击设备返回按钮 uiDevice.pressBack(); }
/** * 滑动界面,打开About phone选项 * 测试环境为标准Android 7.1.1版本,不同设备控件查找方式会有不同 * * @throws UiObjectNotFoundException */ public void testB() throws UiObjectNotFoundException { // 获取设备对象 Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); UiDevice uiDevice = UiDevice.getInstance(instrumentation); // 获取上下文 Context context = instrumentation.getContext(); // 点击Settings按钮 UiObject uiObject = uiDevice.findObject(new UiSelector().description("Settings")); uiObject.click(); // 滑动列表到最后,点击About phone选项 UiScrollable settings = new UiScrollable(new UiSelector().className("android.support.v7.widget.RecyclerView")); UiObject about = settings.getChildByText(new UiSelector().className("android.widget.LinearLayout"), "About phone"); about.click(); // 点击设备返回按钮 uiDevice.pressBack(); uiDevice.pressBack(); }
@Category(CategoryAppStoreTests_v3_3_15.class) @Test public void test009SearchByTitle() throws UiObjectNotFoundException { TestUtils.screenshotCap("appStoreHome"); UiObject2 hotOne = device.findObject(By.res("woyou.market:id/linear_hot_view")).findObject(By.res("woyou.market:id/tv_name")); String targetAppName = hotOne.getText(); UiObject2 searchObj = device.findObject(By.res("woyou.market:id/tv_search").text("搜索")); searchObj.click(); TestUtils.screenshotCap("afterClickSearchBar"); TestUtils.sleep(SHORT_SLEEP); UiObject2 searchObj1 = device.findObject(By.res("woyou.market:id/et_search").text("搜索").focused(true)); searchObj1.click(); searchObj1.setText(targetAppName); TestUtils.screenshotCap("inputSearchContent"); UiScrollable appList = new UiScrollable(new UiSelector().resourceId("woyou.market:id/list_view")); UiObject appInfo = appList.getChildByInstance(new UiSelector().className("android.widget.FrameLayout"),0); UiObject appNameObj = appInfo.getChild(new UiSelector().resourceId("woyou.market:id/tv_name")); Assert.assertEquals(targetAppName,appNameObj.getText()); }
@Category(CategoryAppStoreTests_v3_3_15.class) @Test public void test026FoldupAppDetail() throws UiObjectNotFoundException { TestUtils.screenshotCap("appStoreHome"); UiObject2 hotObj = device.findObject(By.res("woyou.market:id/tv_hot_all").text("全部")); hotObj.clickAndWait(Until.newWindow(), LONG_WAIT); TestUtils.screenshotCap("hotAllInterface"); UiScrollable hotAllScroll = new UiScrollable(new UiSelector().resourceId("woyou.market:id/list_view")); UiObject fullAppObj = hotAllScroll.getChild(new UiSelector().className("android.widget.FrameLayout")); fullAppObj.clickAndWaitForNewWindow(LONG_WAIT); TestUtils.screenshotCap("enterAppDetail"); UiObject2 foldupButton = device.findObject(By.res("woyou.market:id/iv_arrow")); foldupButton.clickAndWait(Until.newWindow(),LONG_WAIT); TestUtils.screenshotCap("foldUpAppDetail"); UiScrollable hotAllScroll1 = new UiScrollable(new UiSelector().resourceId("woyou.market:id/list_view")); Assert.assertNotNull(hotAllScroll1); }
@Test public void test009SearchByTitle() throws UiObjectNotFoundException { TestUtils.screenshotCap("appStoreHome"); UiObject2 hotOne = device.findObject(By.res("woyou.market:id/linear_hot_view")).findObject(By.res("woyou.market:id/tv_name")); String targetAppName = hotOne.getText(); UiObject2 searchObj = device.findObject(By.res("woyou.market:id/tv_search").text("搜索")); searchObj.click(); TestUtils.screenshotCap("afterClickSearchBar"); TestUtils.sleep(SHORT_SLEEP); UiObject2 searchObj1 = device.findObject(By.res("woyou.market:id/et_search").text("搜索").focused(true)); searchObj1.click(); searchObj1.setText(targetAppName); TestUtils.screenshotCap("inputSearchContent"); UiScrollable appList = new UiScrollable(new UiSelector().resourceId("woyou.market:id/list_view")); UiObject appInfo = appList.getChildByInstance(new UiSelector().resourceId("woyou.market:id/app_view"),0); UiObject appNameObj = appInfo.getChild(new UiSelector().resourceId("woyou.market:id/tv_name")); Assert.assertEquals("搜索结果列表第一个应用不是"+targetAppName,targetAppName,appNameObj.getText()); }
@Test public void test026FoldupAppDetail() throws UiObjectNotFoundException { TestUtils.screenshotCap("appStoreHome"); UiObject2 hotObj = device.findObject(By.res("woyou.market:id/tv_hot_all").text("全部")); hotObj.clickAndWait(Until.newWindow(), LONG_WAIT); TestUtils.screenshotCap("hotAllInterface"); UiScrollable hotAllScroll = new UiScrollable(new UiSelector().resourceId("woyou.market:id/list_view")); UiObject fullAppObj = hotAllScroll.getChild(new UiSelector().resourceId("woyou.market:id/app_view")); fullAppObj.clickAndWaitForNewWindow(LONG_WAIT); TestUtils.screenshotCap("enterAppDetail"); UiObject2 foldupButton = device.findObject(By.res("woyou.market:id/iv_arrow")); foldupButton.clickAndWait(Until.newWindow(),LONG_WAIT); TestUtils.screenshotCap("foldUpAppDetail"); UiScrollable hotAllScroll1 = new UiScrollable(new UiSelector().resourceId("woyou.market:id/list_view")); Assert.assertNotNull(hotAllScroll1); }
@Test public void mainActivity_changeGridSize() throws Exception { UiScrollable recycleView = new UiScrollable(new UiSelector().resourceId("com.fantasyfang.googletrendapp:id/trendRecycleView") .className(RecyclerView.class));//recycleView.click(); UiObject item = recycleView.getChild(new UiSelector().resourceId("com.fantasyfang.googletrendapp:id/googleTrendView")); item.click(); String gridDescription = InstrumentationRegistry.getInstrumentation().getTargetContext().getString(R.string.choose_grid); UiObject grid = mDevice.findObject(new UiSelector().descriptionContains(gridDescription)); grid.click(); UiObject gridList = mDevice.findObject(new UiSelector().text("2*2")); gridList.click(); mDevice.waitForIdle(); takeScreenShot("Change_grid_to_2_2.jpg"); mDevice.pressHome(); takeScreenShot("Home.jpg"); }
private void takeScreenshotsOfOpenWithAndShare() throws Exception { /* Open_With View */ UiObject openWithBtn = device.findObject(new UiSelector() .resourceId("org.mozilla.focus.debug:id/open_select_browser") .enabled(true)); assertTrue(openWithBtn.waitForExists(waitingTime)); openWithBtn.click(); UiObject shareList = device.findObject(new UiSelector() .resourceId("org.mozilla.focus.debug:id/apps") .enabled(true)); assertTrue(shareList.waitForExists(waitingTime)); Screengrab.screenshot("OpenWith_Dialog"); /* Share View */ UiObject shareBtn = device.findObject(new UiSelector() .resourceId("org.mozilla.focus.debug:id/share") .enabled(true)); device.pressBack(); TestHelper.menuButton.perform(click()); assertTrue(shareBtn.waitForExists(waitingTime)); shareBtn.click(); TestHelper.shareAppList.waitForExists(waitingTime); Screengrab.screenshot("Share_Dialog"); device.pressBack(); }
@Override public boolean scrollIntoView(UiObject obj) throws UiObjectNotFoundException { if (obj.exists()) { return true; } // we will need to reset the search from the beginning to start search flingToBeginning(getMaxSearchSwipes()); if (obj.exists()) { return true; } for (int x = 0; x < getMaxSearchSwipes(); x++) { if (!scrollForward()) { return false; } if (obj.exists()) { return true; } } return false; }
@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; } else if (destObj instanceof UiObject2) { android.graphics.Point coord = ((UiObject2) destObj).getVisibleCenter(); element.drag(coord, steps); return true; } else { Logger.error("Destination should be either UiObject or UiObject2"); return false; } }
@Test public void test_singleTap2Taps() throws UiObjectNotFoundException, InterruptedException { BaseTest activity = mActivityRule.getActivity(); final UIGestureRecognizerDelegate delegate = activity.delegate; delegate.clear(); UITapGestureRecognizer recognizer = new UITapGestureRecognizer(context); recognizer.setTag("single-tap"); recognizer.setNumberOfTouchesRequired(1); recognizer.setNumberOfTapsRequired(2); recognizer.setActionListener(mActivityRule.getActivity()); delegate.addGestureRecognizer(recognizer); final UiObject mainView = getMainView(); final UiObject textView = getTextView(); final UiObject title = getTitleView(); title.setText("2 Taps"); textView.setText("None"); onView(ViewMatchers.withId(R.id.activity_main)).perform(ViewActions.doubleClick()); SystemClock.sleep(200); assertEquals(recognizer.getTag() + ": " + State.Ended, textView.getText()); }
public boolean dragTo(final Object destObj, final int steps) throws UiObjectNotFoundException, InvalidCoordinatesException { if (API.API_18) { if (destObj instanceof UiObject) { return element.dragTo((UiObject) destObj, steps); } else if (destObj instanceof UiObject2) { android.graphics.Point coords = ((UiObject2) destObj).getVisibleCenter(); return dragTo(coords.x, coords.y, steps); } else { Logger.error("Destination should be either UiObject or UiObject2"); return false; } } else { Logger.error("Device does not support API >= 18!"); return false; } }
public static void checkScreenShotsPermission() { Context appContext = InstrumentationRegistry.getTargetContext(); Brickator.print("app package is: " + appContext.getApplicationContext().getPackageName()); int permission = ContextCompat.checkSelfPermission(appContext, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (permission == PackageManager.PERMISSION_DENIED) { Brickator.getInstance().goToStateSimple(PermissionState.class); UiObject allowButton = PermissionState.getAllowButton(); try { allowButton.click(); } catch (UiObjectNotFoundException e) { throw new UnknownError("Allow button for storage permission don't exists"); } } permission = ContextCompat.checkSelfPermission(appContext, Manifest.permission.WRITE_EXTERNAL_STORAGE); assertEquals(PackageManager.PERMISSION_GRANTED, permission); SystemClock.sleep(100); }
private static int getScrollableOffset(AndroidElement uiScrollable) throws UiObjectNotFoundException, ClassNotFoundException, InvalidSelectorException { AccessibilityNodeInfo nodeInfo = null; int offset = 0; Object actualObject = uiScrollable.getUiObject(); if (actualObject instanceof UiObject) { UiObject object = (UiObject) uiScrollable.getChild(new UiSelector().index(0)); Method findAccessibilityNodeInfoMethod = ReflectionUtils.method(UiObject.class, "findAccessibilityNodeInfo", long.class); long waitForSelectorTimeout = (long) ReflectionUtils.getField(UiObject.class, "WAIT_FOR_SELECTOR_TIMEOUT", object); nodeInfo = (AccessibilityNodeInfo) ReflectionUtils.invoke(findAccessibilityNodeInfoMethod, object, waitForSelectorTimeout); } else { UiObject2 childObject = ((UiObject2) actualObject).getChildren().get(0); try { nodeInfo = AccessibilityNodeInfoGetter.fromUiObject(childObject); } catch (UiAutomator2Exception ignored) { } } if (nodeInfo != null) { Rect rect = new Rect(); nodeInfo.getBoundsInParent(rect); offset = rect.height(); } return offset; }
public List<Object> getChildren(final Object selector, final By by) throws UiObjectNotFoundException, InvalidSelectorException, ClassNotFoundException { 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, by); return androidElement.getChildren(selector, by); } return (List)element.findObjects((BySelector) selector); }
/** * Remove all duplicate elements from the provided list * * @param elements - elements to remove duplicates from * * @return a new list with duplicates removed */ public static List<Object> dedupe(List<Object> elements) { try { findAccessibilityNodeInfo = method(UiObject.class, "findAccessibilityNodeInfo", long.class); } catch (Exception e) { e.printStackTrace(); } List<Object> result = new ArrayList<Object>(); List<AccessibilityNodeInfo> nodes = new ArrayList<AccessibilityNodeInfo>(); for (Object element : elements) { AccessibilityNodeInfo node = elementToNode(element); if (!nodes.contains(node)) { nodes.add(node); result.add(element); } } return result; }
public static void turnOnAccessibilityPermission() throws UiObjectNotFoundException { UiScrollable texterScreen = new UiScrollable(LauncherHelper.LAUNCHER_CONTAINER); texterScreen.setAsVerticalList(); UiObject permission = new UiObject( new UiSelector().text("Off")); permission.click(); UiScrollable permissionScreen = new UiScrollable(LauncherHelper.LAUNCHER_CONTAINER); permissionScreen.setAsVerticalList(); UiObject permissionButton = new UiObject( new UiSelector().text("OK")); permissionButton.click(); }
/** * */ private void testIfSlide( int index ){ if ( index <= 0 ) throw new IllegalArgumentException("Bad parameter for index"); try { UiObject title = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/title") ); UiObject description = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/description") ); UiObject next = mDevice.findObject( new UiSelector().resourceId(PACKAGE_APP_PATH + ":id/skip") ); assertEquals(sTitles[index - 1], title.getText()); assertEquals(sSummaries[index-1], description.getText()); assertTrue(next.exists()); } catch ( UiObjectNotFoundException uonfe ){ uonfe.printStackTrace(); fail(uonfe.getMessage() ); } }
/** * Inner method to get a dedicated notification and test it * @param textContent - The text to use to get the notification */ private void testIfNotificationExists( String textContent ) { UiObject n = null; if ( Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT_WATCH ){ n = mDevice.findObject( new UiSelector() .resourceId("android:id/text") .className("android.widget.TextView") .packageName("pylapp.smoothclicker.android") .textContains(textContent)); } else { n = mDevice.findObject( new UiSelector() .resourceId("android:id/text") .className("android.widget.TextView") .packageName("com.android.systemui") .textContains(textContent)); } mDevice.openNotification(); n.waitForExists(2000); assertTrue(n.exists()); }
public Object getChild(final Object selector) throws UiObjectNotFoundException, InvalidSelectorException, ClassNotFoundException { 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); UiObject uiObject = (UiObject) CustomUiDevice.getInstance().findObject(uiSelector); AccessibilityNodeInfo uiObject_nodeInfo = AccessibilityNodeInfoGetter.fromUiObject(element); return uiObject.getChild((UiSelector) selector); } return element.findObject((BySelector) selector); }
/** * Inner method to get a dedicated notification and test it * @param textContent - The text to use to get the notification */ private void testNotification( String textContent ){ UiObject n = null; if ( Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT_WATCH ){ n = mDevice.findObject( new UiSelector() .resourceId("android:id/text") .className("android.widget.TextView") .packageName("pylapp.smoothclicker.android") .textContains(textContent)); } else { n = mDevice.findObject( new UiSelector() .resourceId("android:id/text") .className("android.widget.TextView") .packageName("com.android.systemui") .textContains(textContent)); } mDevice.openNotification(); n.waitForExists(60000); assertTrue(n.exists()); }
@Category(CategoryAppStoreTests_v3_3_15.class) @Test public void test023CheckAppDetail() throws UiObjectNotFoundException { TestUtils.screenshotCap("appStoreHome"); UiObject2 hotObj = device.findObject(By.res("woyou.market:id/tv_hot_all").text("全部")); hotObj.clickAndWait(Until.newWindow(), LONG_WAIT); TestUtils.screenshotCap("hotAllInterface"); UiScrollable hotAllScroll = new UiScrollable(new UiSelector().resourceId("woyou.market:id/list_view")); UiObject fullAppObj = hotAllScroll.getChild(new UiSelector().className("android.widget.FrameLayout")); String appName = fullAppObj.getChild(new UiSelector().resourceId("woyou.market:id/tv_name")).getText(); fullAppObj.clickAndWaitForNewWindow(LONG_WAIT); TestUtils.screenshotCap("enterHotAppsFirstOne"); UiObject2 nameObj = device.findObject(By.res("woyou.market:id/tv_name")); Assert.assertEquals("期望的名字是"+appName+",而实际是"+nameObj.getText(),appName,nameObj.getText()); }
@Category(CategoryAppStoreTests_v3_3_15.class) @Test public void test027CheckSearchHistory() throws UiObjectNotFoundException { TestUtils.screenshotCap("appStoreHome"); UiObject2 hotOne = device.findObject(By.res("woyou.market:id/linear_hot_view")).findObject(By.res("woyou.market:id/tv_name")); String targetAppName = hotOne.getText(); UiObject2 searchObj = device.findObject(By.res("woyou.market:id/tv_search").text("搜索")); searchObj.click(); TestUtils.screenshotCap("afterClickSearchBtn"); TestUtils.sleep(SHORT_SLEEP); UiObject2 searchObj1 = device.findObject(By.res("woyou.market:id/et_search").text("搜索").focused(true)); searchObj1.click(); searchObj1.setText(targetAppName); TestUtils.screenshotCap("enterSearchContent"); UiScrollable appList = new UiScrollable(new UiSelector().resourceId("woyou.market:id/list_view")); UiObject appInfo = appList.getChildByInstance(new UiSelector().className("android.widget.FrameLayout"),0); UiObject appNameObj = appInfo.getChild(new UiSelector().resourceId("woyou.market:id/tv_name")); String appName = appNameObj.getText(); appInfo.click(); TestUtils.screenshotCap("enterTheFirstSearchResult"); device.pressBack(); TestUtils.sleep(SHORT_SLEEP); UiObject2 clearButton = device.findObject(By.res("woyou.market:id/iv_delete")); clearButton.click(); TestUtils.screenshotCap("clearSearchBar"); TestUtils.sleep(SHORT_SLEEP); String historyObjName = device.findObject(By.res("woyou.market:id/history_key")).findObject(By.clazz("android.widget.TextView")).getText(); Assert.assertEquals("期望的名字是"+appName+",而实际是"+historyObjName,appName,historyObjName); }
@Test public void test023CheckAppDetail() throws UiObjectNotFoundException { TestUtils.screenshotCap("appStoreHome"); UiObject2 hotObj = device.findObject(By.res("woyou.market:id/tv_hot_all").text("全部")); hotObj.clickAndWait(Until.newWindow(), LONG_WAIT); TestUtils.screenshotCap("hotAllInterface"); UiScrollable hotAllScroll = new UiScrollable(new UiSelector().resourceId("woyou.market:id/list_view")); UiObject fullAppObj = hotAllScroll.getChild(new UiSelector().resourceId("woyou.market:id/app_view")); String appName = fullAppObj.getChild(new UiSelector().resourceId("woyou.market:id/tv_name")).getText(); fullAppObj.clickAndWaitForNewWindow(LONG_WAIT); TestUtils.screenshotCap("enterHotAppsFirstOne"); UiObject2 nameObj = device.findObject(By.res("woyou.market:id/tv_name")); Assert.assertEquals("期望的名字是"+appName+",而实际是"+nameObj.getText(),appName,nameObj.getText()); }
@Test public void test027CheckSearchHistory() throws UiObjectNotFoundException { TestUtils.screenshotCap("appStoreHome"); UiObject2 hotOne = device.findObject(By.res("woyou.market:id/linear_hot_view")).findObject(By.res("woyou.market:id/tv_name")); String targetAppName = hotOne.getText(); UiObject2 searchObj = device.findObject(By.res("woyou.market:id/tv_search").text("搜索")); searchObj.click(); TestUtils.screenshotCap("afterClickSearchBtn"); TestUtils.sleep(SHORT_SLEEP); UiObject2 searchObj1 = device.findObject(By.res("woyou.market:id/et_search").text("搜索").focused(true)); searchObj1.click(); searchObj1.setText(targetAppName); TestUtils.screenshotCap("enterSearchContent"); UiScrollable appList = new UiScrollable(new UiSelector().resourceId("woyou.market:id/list_view")); UiObject appInfo = appList.getChildByInstance(new UiSelector().resourceId("woyou.market:id/app_view"),0); UiObject appNameObj = appInfo.getChild(new UiSelector().resourceId("woyou.market:id/tv_name")); String appName = appNameObj.getText(); appInfo.click(); TestUtils.screenshotCap("enterTheFirstSearchResult"); device.pressBack(); TestUtils.sleep(SHORT_SLEEP); UiObject2 clearButton = device.findObject(By.res("woyou.market:id/iv_delete")); clearButton.click(); TestUtils.screenshotCap("clearSearchBar"); TestUtils.sleep(SHORT_SLEEP); String historyObjName = device.findObject(By.res("woyou.market:id/history_key")).findObject(By.clazz("android.widget.TextView")).getText(); Assert.assertEquals("期望的名字是"+appName+",而实际是"+historyObjName,appName,historyObjName); }
private static void takeCameraPhoto() throws UiObjectNotFoundException { UiDevice device = UiDevice.getInstance(getInstrumentation()); UiSelector shutterSelector = new UiSelector().resourceId("com.android.camera:id/shutter_button"); UiObject shutterButton = device.findObject(shutterSelector); shutterButton.click(); UiSelector doneSelector = new UiSelector().resourceId("com.android.camera:id/btn_done"); UiObject doneButton = device.findObject(doneSelector); doneButton.click(); }
@Test public void mainActivity_RecycleViewChangeCountry() throws Exception { UiScrollable recycleView = new UiScrollable(new UiSelector().resourceId("com.fantasyfang.googletrendapp:id/trendRecycleView") .className(RecyclerView.class));//recycleView.click(); UiObject item = recycleView.getChild(new UiSelector().resourceId("com.fantasyfang.googletrendapp:id/googleTrendView")); item.click(); item.click(); mDevice.waitForIdle(); takeScreenShot("Country_menu_page.jpg"); }
private boolean selectSettingsFor(String name) { try { UiScrollable appsSettingsList = new UiScrollable(SettingsHelper.SCROLL_VIEW); UiObject obj = appsSettingsList.getChildByText(SettingsHelper.LIST_VIEW_ITEM, name); obj.click(); } catch (UiObjectNotFoundException e) { return false; } return true; }
public String getUiObjectDisplay(UiObject object) { String display = ""; try { display = object.getText(); if (display == null || display.length() == 0) { display = object.getContentDescription(); } } catch (UiObjectNotFoundException e) { Log.e("Chimp-wildCardManager","UiObject not found while getting display strings",e); } return display; }