https://github.com/android/testing-samples
Espresso是Google 开源的Android自动化测试框架,使用 Espresso,书写简洁、优雅、可信赖的 Android UI 测试。
BasicSample 效果:
ChangeTextBehaviorTest
**
* Basic tests showcasing simple view matchers and actions like {@link ViewMatchers#withId},
* {@link ViewActions#click} and {@link ViewActions#typeText}.
* <p>
* Note that there is no need to tell Espresso that a view is in a different {@link Activity}.
*/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class ChangeTextBehaviorTest {
public static final String STRING_TO_BE_TYPED = "Espresso";
/**
* Use {@link ActivityScenarioRule} to create and launch the activity under test, and close it
* after test completes. This is a replacement for {@link androidx.test.rule.ActivityTestRule}.
*/
@Rule public ActivityScenarioRule<MainActivity> activityScenarioRule
= new ActivityScenarioRule<>(MainActivity.class);
@Test
public void changeText_sameActivity() {
// Type text and then press the button.
onView(withId(R.id.editTextUserInput))
.perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
onView(withId(R.id.changeTextBt)).perform(click());
// Check that the text was changed.
onView(withId(R.id.textToBeChanged)).check(matches(withText(STRING_TO_BE_TYPED)));
}
@Test
public void changeText_newActivity() {
// Type text and then press the button.
onView(withId(R.id.editTextUserInput)).perform(typeText(STRING_TO_BE_TYPED),
closeSoftKeyboard());
onView(withId(R.id.activityChangeTextBtn)).perform(click());
// This view is in a different Activity, no need to tell Espresso.
onView(withId(R.id.show_text_view)).check(matches(withText(STRING_TO_BE_TYPED)));
}
}
RecyclerViewSample 效果:
RecyclerViewSampleTest
**
* Test class showcasing some {@link RecyclerViewActions} from Espresso.
*/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class RecyclerViewSampleTest {
private static final int ITEM_BELOW_THE_FOLD = 40;
/**
* Use {@link ActivityScenario} to create and launch the activity under test. This is a
* replacement for {@link androidx.test.rule.ActivityTestRule}.
*/
@Rule
public ActivityScenarioRule<MainActivity> activityScenarioRule =
new ActivityScenarioRule<MainActivity>(MainActivity.class);
@Test
public void scrollToItemBelowFold_checkItsText() {
// First scroll to the position that needs to be matched and click on it.
onView(ViewMatchers.withId(R.id.recyclerView))
.perform(RecyclerViewActions.actionOnItemAtPosition(ITEM_BELOW_THE_FOLD, click()));
// Match the text in an item below the fold and check that it's displayed.
String itemElementText = getApplicationContext().getResources().getString(
R.string.item_element_text) + String.valueOf(ITEM_BELOW_THE_FOLD);
onView(withText(itemElementText)).check(matches(isDisplayed()));
}
@Test
public void itemInMiddleOfList_hasSpecialText() {
// First, scroll to the view holder using the isInTheMiddle matcher.
onView(ViewMatchers.withId(R.id.recyclerView))
.perform(RecyclerViewActions.scrollToHolder(isInTheMiddle()));
// Check that the item has the special text.
String middleElementText =
getApplicationContext().getResources().getString(R.string.middle);
onView(withText(middleElementText)).check(matches(isDisplayed()));
}
/**
* Matches the {@link CustomAdapter.ViewHolder}s in the middle of the list.
*/
private static Matcher<CustomAdapter.ViewHolder> isInTheMiddle() {
return new TypeSafeMatcher<CustomAdapter.ViewHolder>() {
@Override
protected boolean matchesSafely(CustomAdapter.ViewHolder customHolder) {
return customHolder.getIsInTheMiddle();
}
@Override
public void describeTo(Description description) {
description.appendText("item in the middle");
}
};
}
}