@Override protected void onResume(){ super.onResume(); View root = this.getWindow().getDecorView(); for(View v : TreeIterables.breadthFirstViewTraversal(root)){ if(v.getId() != -1) { System.out.println("View: " + v.getResources().getResourceEntryName(v.getId()) + " " + v.isClickable()); } else if(v.getContentDescription() != null){ System.out.println("View: " + v.getContentDescription().toString() + " " + v.isClickable()); } else{ System.out.println("View: " + v.toString() + " No Des, No Id " + v.isClickable()); } } }
protected ArrayList<View> getAllViews() { View root = getDecorView(); ArrayList<View> views = new ArrayList<>(); for (View v : TreeIterables.breadthFirstViewTraversal(root)) { // Log.i("Chimp-needs-to-know","Active View: " + v.toString()); views.add(v); } /* View focus = getActivityInstance().getCurrentFocus(); if (focus != null) { for (View v : TreeIterables.breadthFirstViewTraversal(focus)) { Log.i("Chimp-needs-to-know", "In focus View: " + v.toString()); } } */ return views; }
private View matchView(View view, Matcher<View> matcher) { if (view == null) { return null; } if (matcher.matches(view)) { return view; } for (View child : TreeIterables.breadthFirstViewTraversal(view)) { if (matcher.matches(child)) { return child; } } return null; }
public static ViewAction waitForMatch(final Matcher<View> aViewMatcher, final long timeout) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "Waiting for view matching " + aViewMatcher; } @Override public void perform(UiController uiController, View view) { uiController.loopMainThreadUntilIdle(); final long startTime = System.currentTimeMillis(); final long endTime = startTime + timeout; do { for (View child : TreeIterables.breadthFirstViewTraversal(view)) { if (aViewMatcher.matches(child)) { // found return; } } uiController.loopMainThreadForAtLeast(50); } while (System.currentTimeMillis() < endTime); //The action has timed out. throw new PerformException.Builder() .withActionDescription(getDescription()) .withViewDescription("") .withCause(new TimeoutException()) .build(); } }; }
public static ViewAction waitId(final int viewId, final long millis) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "wait for a specific view with id <" + viewId + "> during " + millis + " millis."; } @Override public void perform(final UiController uiController, final View view) { uiController.loopMainThreadUntilIdle(); final long startTime = System.currentTimeMillis(); final long endTime = startTime + millis; final Matcher<View> viewMatcher = withId(viewId); do { for (View child : TreeIterables.breadthFirstViewTraversal(view)) { // found view with required ID if (viewMatcher.matches(child)) { return; } } uiController.loopMainThreadForAtLeast(50); } while (System.currentTimeMillis() < endTime); // timeout happens throw new PerformException.Builder() .withActionDescription(this.getDescription()) .withViewDescription(HumanReadables.describe(view)) .withCause(new TimeoutException()) .build(); } }; }
public static boolean isExist(final int viewId) { final Boolean[] result = {false}; onView(isRoot()).perform(new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "get text from edit text view:" + viewId; } @Override public void perform(UiController uiController, View view) { uiController.loopMainThreadUntilIdle(); final Matcher<View> viewMatcher = withId(viewId); uiController.loopMainThreadForAtLeast(500); for (View child : TreeIterables.breadthFirstViewTraversal(view)) { if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) { result[0] = true; break; } } } }); return result[0]; }
public static Object getTag(final int viewId) { final Object[] result = {null}; onView(isRoot()).perform(new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "get text from edit text view:" + viewId; } @Override public void perform(UiController uiController, View view) { uiController.loopMainThreadUntilIdle(); final Matcher<View> viewMatcher = withId(viewId); uiController.loopMainThreadForAtLeast(500); for (View child : TreeIterables.breadthFirstViewTraversal(view)) { if (viewMatcher.matches(child)) { result[0] = child.getTag(); } } } }); return result[0]; }
@Override protected boolean isConditionMet(View view) { for (View child : TreeIterables.breadthFirstViewTraversal(view)) { if (matcher.matches(child)) { return true; } } return false; }
/** * Perform action of waiting for a specific view id. */ public static ViewAction waitId(final int viewId, final long millis) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "wait for a specific view with id <" + viewId + "> during " + millis + " millis."; } @Override public void perform(final UiController uiController, final View view) { uiController.loopMainThreadUntilIdle(); final long startTime = System.currentTimeMillis(); final long endTime = startTime + millis; final Matcher<View> viewMatcher = withId(viewId); do { for (View child : TreeIterables.breadthFirstViewTraversal(view)) { // found view with required ID if (viewMatcher.matches(child)) { return; } } uiController.loopMainThreadForAtLeast(50); } while (System.currentTimeMillis() < endTime); // timeout happens throw new PerformException.Builder() .withActionDescription(this.getDescription()) .withViewDescription(HumanReadables.describe(view)) .withCause(new TimeoutException()) .build(); } }; }
/** Perform action of waiting for a specific view id. */ public static ViewAction waitId(final int viewId, final long millis) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "wait for a specific view with id <" + viewId + "> during " + millis + " millis."; } @Override public void perform(final UiController uiController, final View view) { uiController.loopMainThreadUntilIdle(); final long startTime = System.currentTimeMillis(); final long endTime = startTime + millis; final Matcher<View> viewMatcher = withId(viewId); do { for (View child : TreeIterables.breadthFirstViewTraversal(view)) { // found view with required ID if (viewMatcher.matches(child)) { return; } } uiController.loopMainThreadForAtLeast(50); } while (System.currentTimeMillis() < endTime); // timeout happens throw new PerformException.Builder() .withActionDescription(this.getDescription()) .withViewDescription(HumanReadables.describe(view)) .withCause(new TimeoutException()) .build(); } }; }
/** * Looks for a view with the given ID for a given millis * @param viewId ID of the view you are looking for * @param millis Millis trying to find it * @return ViewAction so you can use like any other EspressoAction */ public static ViewAction waitId(final int viewId, final long millis) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "wait for a specific view with id <" + viewId + "> during " + millis + " millis."; } @Override public void perform(final UiController uiController, final View view) { uiController.loopMainThreadUntilIdle(); final long startTime = System.currentTimeMillis(); final long endTime = startTime + millis; final Matcher<View> viewMatcher = withId(viewId); do { for (View child : TreeIterables.breadthFirstViewTraversal(view)) { // found view with required ID if (viewMatcher.matches(child)) { return; } } uiController.loopMainThreadForAtLeast(50); } while (System.currentTimeMillis() < endTime); // timeout happens throw new PerformException.Builder() .withActionDescription(this.getDescription()) .withViewDescription(HumanReadables.describe(view)) .withCause(new TimeoutException()) .build(); } }; }
public static ViewAction waitInvisibleGoneId(final int viewId, final long millis) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "wait view id <" + viewId + "> during " + millis + " millis."; } @Override public void perform(final UiController uiController, final View view) { uiController.loopMainThreadUntilIdle(); final long startTime = System.currentTimeMillis(); final long endTime = startTime + millis; final Matcher<View> viewMatcher = withId(viewId); // Log.e("test", "waitInvisibleGoneId view id:" + viewId); uiController.loopMainThreadForAtLeast(200); do { View find = null; for (View child : TreeIterables.breadthFirstViewTraversal(view)) { // if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) { // Log.e("test","original view id:"+viewId+" child view id:"+child.getId()); if (viewMatcher.matches(child)) { find = child; Log.e("test", "macthch visible: " + child.getVisibility()); } } if (find != null && find.getVisibility() == View.VISIBLE) { // Log.e("test", "find startTime:" + startTime); // Log.e("test","find endTime:"+endTime); // Log.e("test","find System.currentTimeMillis():"+System.currentTimeMillis()); } else { Log.e("test", "waitInvisibleGoneId wait time :" + (System.currentTimeMillis() - startTime) / 1000 + " id:" + viewId); return; } // Log.e("test","startTime:"+startTime); // Log.e("test","endTime:"+endTime); uiController.loopMainThreadForAtLeast(50); } while (System.currentTimeMillis() < endTime); Log.e("test", "waitInvisibleGoneId time out :" + (System.currentTimeMillis() - startTime) / 1000 + " id:" + viewId); throw new PerformException.Builder() .withActionDescription(this.getDescription()) .withViewDescription(HumanReadables.describe(view)) .withCause(new TimeoutException()) .build(); } }; }
public static ViewAction waitId(final int viewId, final long millis) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "wait view id <" + viewId + "> during " + millis + " millis."; } @Override public void perform(final UiController uiController, final View view) { uiController.loopMainThreadUntilIdle(); final long startTime = System.currentTimeMillis(); final long endTime = startTime + millis; final Matcher<View> viewMatcher = withId(viewId); Log.e("test", "waitId original view id:" + viewId); do { for (View child : TreeIterables.breadthFirstViewTraversal(view)) { // if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) { // Log.e("test","original view id:"+viewId+" child view id:"+child.getId()); if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) { // Log.e("test", "find startTime:" + startTime); // Log.e("test","find endTime:"+endTime); // Log.e("test","find System.currentTimeMillis():"+System.currentTimeMillis()); Log.e("test", "waitId wait time :" + (System.currentTimeMillis() - startTime) / 1000 + " id:" + viewId); return; } } // Log.e("test","startTime:"+startTime); // Log.e("test","endTime:"+endTime); uiController.loopMainThreadForAtLeast(50); } while (System.currentTimeMillis() < endTime); Log.e("test", "waitId time out :" + (System.currentTimeMillis() - startTime) / 1000 + " id:" + viewId); throw new PerformException.Builder() .withActionDescription(this.getDescription()) .withViewDescription(HumanReadables.describe(view)) .withCause(new TimeoutException()) .build(); } }; }
/** * Perform action of waiting for a specific view id. * <p/> * E.g.: * onView(isRoot()).perform(waitId(R.id.dialogEditor, Sampling.SECONDS_15)); * * @param viewId * @param millis * @return */ public static ViewAction waitId(final int viewId, final long millis) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "wait for a specific view with id <" + viewId + "> during " + millis + " millis."; } @Override public void perform(final UiController uiController, final View view) { uiController.loopMainThreadUntilIdle(); final long startTime = System.currentTimeMillis(); final long endTime = startTime + millis; final Matcher<View> viewMatcher = withId(viewId); do { for (View child : TreeIterables.breadthFirstViewTraversal(view)) { // found view with required ID if (viewMatcher.matches(child)) { return; } } uiController.loopMainThreadForAtLeast(50); } while (System.currentTimeMillis() < endTime); // timeout happens throw new PerformException.Builder() .withActionDescription(this.getDescription()) .withViewDescription(HumanReadables.describe(view)) .withCause(new TimeoutException()) .build(); } }; }
/** * Performs screenshot of activity with specified view * use like onView(isRoot()).perform(screenshot(R.id.MainActivity)); * @param viewId view to look at * @param testTag description of screenshoshot * @return */ public static ViewAction screenshot(final int viewId, final String testTag) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "screenshoting current activity with root id <" + viewId + "> ."; } @Override public void perform(final UiController uiController, final View view) { uiController.loopMainThreadUntilIdle(); final Matcher<View> viewMatcher = withId(viewId); for (View child : TreeIterables.breadthFirstViewTraversal(view)) { // found view with required ID if (viewMatcher.matches(child)) { Activity host=(Activity) child.getContext(); try { Screenshot.capture(testTag+"_"+host.getLocalClassName().toString(),host); } catch (IOException e) { //something bad happened.no screenshot. Log.d(TAG,e.toString()); e.printStackTrace(); throw new RuntimeException(e); } return; } } // nothing found throw new PerformException.Builder() .withActionDescription(this.getDescription()) .withViewDescription(HumanReadables.describe(view)) .withCause(new NoRootViewFoundWithIdException()) .build(); } }; }
/** * Perform action of waiting for a specific view id * based off https://stackoverflow.com/questions/21417954/espresso-thread-sleep * @param viewId - view to look for * @param millis - how long to wait for in milliseconds */ public static ViewAction waitId(final int viewId, final long millis) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "wait for a specific view with id <" + viewId + "> during " + millis + " millis."; } @Override public void perform(final UiController uiController, final View view) { uiController.loopMainThreadUntilIdle(); final long startTime = System.currentTimeMillis(); final long endTime = startTime + millis; final Matcher<View> viewMatcher = withId(viewId); do { for (View child : TreeIterables.breadthFirstViewTraversal(view)) { // found view with required ID if (viewMatcher.matches(child)) { return; } } uiController.loopMainThreadForAtLeast(50); } while (System.currentTimeMillis() < endTime); // timeout happens throw new PerformException.Builder() .withActionDescription(this.getDescription()) .withViewDescription(HumanReadables.describe(view)) .withCause(new TimeoutException()) .build(); } }; }