Espresso android -- Evoke assert? (NOT a view assert)
By : user3324038
Date : March 29 2020, 07:55 AM
will help you It's possible to enable keyword asserts, but it requires a manual step, and it would be unusual to use keyword asserts in test code. It's best to use the JUnit methods (assertTrue, etc.) as you would in unit tests.
|
Espresso: How do i assert if a particular activity is launched when i click on a item using espresso
By : RayB
Date : March 29 2020, 07:55 AM
Hope that helps You should simulate the process of clicking a button and then test if the activity at the top of the stack is the one you're looking for code :
@RunWith(AndroidJUnit4.class)
public class YourTestClass{
@Test
public void testButton() {
Espresso.onView(ViewMatchers.withId(R.id.yourButtonId)).perform(ViewActions.click());
Assert.assertEquals(getActivityInstance().getClass(), YourActivityThatShouldStart.class);
}
private Activity getActivityInstance() {
final Activity[] currentActivity = {null};
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
if (resumedActivities.iterator().hasNext()) {
currentActivity[0] = (Activity) resumedActivities.iterator().next();
}
}
});
return currentActivity[0];
}
}
|
It is possible to test view below a progress dialog, using espresso?
By : franceska
Date : March 29 2020, 07:55 AM
wish helps you Yes, you use a RootMatcher. For example: code :
// Specifically target the main window view hierarchy
onView(withId(R.id.view_to_match))
.inRoot(withDecorView(is(getActivity().getWindow().getDecorView())))
.perform(click());
// Specifically disregard the dialog window view hierarchy
onView(withId(R.id.view_to_match))
.inRoot(not(isDialog()))
.perform(click());
|
any method to wait until invisible progressbar in espresso test?
By : jameslopez
Date : March 29 2020, 07:55 AM
wish of those help So, you want to wait until your ProgressBar is hidden. You can either create an idling resource or use a custom ViewAction as this one: code :
/**
* Perform action of waiting until the element is accessible & not shown.
* @param viewId The id of the view to wait for.
* @param millis The timeout of until when to wait for.
*/
public static ViewAction waitUntilNotShown(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 + "> is hidden 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) && !child.isShown()) {
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();
}
};
}
onView(isRoot()).perform(waitUntilNotShown(R.id.theIdToWaitFor, 5000));
|
Assert exceptions in Android Espresso test
By : cloudsoft
Date : March 29 2020, 07:55 AM
may help you . Eventually I found a way to do it. I've created a custom Hamcrest matcher that allows you to verify a nested exception. code :
public class NestedExceptionMatcher extends TypeSafeMatcher<Throwable> {
private final Class<?> mExpectedType;
private final Matcher<String> mMessageMatcher;
static NestedExceptionMatcher expectNestedThrowable(Class<?> expectedType, Matcher<String> messageMatcher) {
return new NestedExceptionMatcher(expectedType, messageMatcher);
}
NestedExceptionMatcher(Class<?> expectedType, Matcher<String> messageMatcher) {
mExpectedType = expectedType;
mMessageMatcher = messageMatcher;
}
@Override
protected boolean matchesSafely(Throwable item) {
boolean matches = isMatch(item);
Throwable currentThrowable = item.getCause();
while (!matches && currentThrowable != null) {
matches = isMatch(currentThrowable);
currentThrowable = currentThrowable.getCause();
}
return matches;
}
@Override
public void describeTo(Description description) {
description
.appendText("expects type ")
.appendValue(mExpectedType)
.appendText(" with a message ")
.appendDescriptionOf(mMessageMatcher);
}
private boolean isMatch(Throwable t) {
return t.getClass().isAssignableFrom(mExpectedType) && mMessageMatcher.matches(t.getMessage());
}
}
public class SomeActivityTest {
@Rule
public ActivityTestRule<SomeActivity> mRule = new ActivityTestRule<>(
SomeActivity.class, false, false);
@Rule
public ExpectedException mExceptionRule = ExpectedException.none();
@Test
public void testClick_whenInvalidParamsAreSet_shouldThrowException() {
mRule.launchActivity(getIntentWithInvalidParams());
mExceptionRule.expect(expectNestedThrowable(
IllegalArgumentException.class,
containsString("parameter isn't defined")
));
onView(withId(R.id.my_btn)).perform(click());
}
private Intent getIntentWithInvalidParams() {
...
}
}
|