Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.pogamadores.test.action.ViewActions.waitText;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTestCase {
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void confirmAction() {
String confirmationString = mActivityRule.getActivity().getString(R.string.confirmation);
onView(withId(R.id.confirmation_button)).perform(click());
onView(isRoot()).perform(waitText(confirmationString, 10000));
pressBack();
}
}
package com.pogamadores.test.action;
import android.support.test.espresso.PerformException;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.util.HumanReadables;
import android.support.test.espresso.util.TreeIterables;
import android.view.View;
import org.hamcrest.Matcher;
import java.util.concurrent.TimeoutException;
import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
public class ViewActions
{
/**
* <p> Action to wait for a text to show on the screen
*
* @param text String that need to be searched
* @param millis Timeout of the search
*
* @return Formatted View Action
*/
public static ViewAction waitText(final String text, final long millis) {
return waitForMatcher(
withText(text),
millis,
"wait for a specific view with text \"" + text + "\" during " + millis + " millis.");
}
/**
* <p>Perform action of waiting for a specific Matcher.</p>
*
* <p>Code based on the answer provided by Alex K. on <a href="http://stackoverflow.com/a/22563297">stackoverflow</a>.</p>
*
* @param viewMatcher Matcher that need to be validated
* @param millis Timeout of the search
* @param description Description of the action
* @return ViewAction to be used on .perform
*/
protected static ViewAction waitForMatcher(final Matcher<View> viewMatcher, final long millis, final String description) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isRoot();
}
@Override
public String getDescription() {
return description;
}
@Override
public void perform(UiController uiController, View view) {
uiController.loopMainThreadUntilIdle();
final long startTime = System.currentTimeMillis();
final long endTime = startTime + millis;
do {
for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
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();
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.