Skip to content

Commit 194fa88

Browse files
Merge pull request #335 from fstanis/patch-1
Add itemWithText_exists
2 parents 14d4814 + b7e0492 commit 194fa88

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

  • ui/espresso/RecyclerViewSample/app/src/androidTest/java/com/example/android/testing/espresso/RecyclerViewSample

ui/espresso/RecyclerViewSample/app/src/androidTest/java/com/example/android/testing/espresso/RecyclerViewSample/RecyclerViewSampleTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import static androidx.test.espresso.Espresso.onView;
2121
import static androidx.test.espresso.action.ViewActions.click;
2222
import static androidx.test.espresso.assertion.ViewAssertions.matches;
23+
import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant;
2324
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
2425
import static androidx.test.espresso.matcher.ViewMatchers.withText;
2526

2627
import androidx.test.core.app.ActivityScenario;
2728
import androidx.test.espresso.contrib.RecyclerViewActions;
2829
import androidx.test.espresso.matcher.ViewMatchers;
30+
import androidx.test.espresso.PerformException;
2931
import androidx.test.ext.junit.rules.ActivityScenarioRule;
3032
import androidx.test.filters.LargeTest;
3133
import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -56,6 +58,16 @@ public class RecyclerViewSampleTest {
5658
public ActivityScenarioRule<MainActivity> activityScenarioRule =
5759
new ActivityScenarioRule<MainActivity>(MainActivity.class);
5860

61+
@Test(expected = PerformException.class)
62+
public void itemWithText_doesNotExist() {
63+
// Attempt to scroll to an item that contains the special text.
64+
onView(ViewMatchers.withId(R.id.recyclerView))
65+
// scrollTo will fail the test if no item matches.
66+
.perform(RecyclerViewActions.scrollTo(
67+
hasDescendant(withText("not in the list"))
68+
));
69+
}
70+
5971
@Test
6072
public void scrollToItemBelowFold_checkItsText() {
6173
// First scroll to the position that needs to be matched and click on it.
@@ -96,4 +108,4 @@ public void describeTo(Description description) {
96108
}
97109
};
98110
}
99-
}
111+
}

0 commit comments

Comments
 (0)