UI测试,以验证单击工具栏中的菜单项是否可以更新RecyclerView

时间:2019-03-12 12:22:47

标签: android android-espresso ui-testing

我是第一次使用Espresso进行UI测试。我希望使用示例测试来检查是否按了工具栏中的菜单项是否更新了Recyclerview。

我写了直到执行工具栏菜单上的单击,但是找不到如何检查Recyclerview是否已更新

    import org.junit.Rule;
    import org.junit.Test;

    import androidx.test.rule.ActivityTestRule;

    import static androidx.test.espresso.Espresso.onView;
    import static androidx.test.espresso.action.ViewActions.click;
    import static androidx.test.espresso.assertion.ViewAssertions.matches;
    import static androidx.test.espresso.matcher.ViewMatchers.withId;
    import static androidx.test.espresso.matcher.ViewMatchers.withText;

    public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule =
            new ActivityTestRule<>(MainActivity.class);

    private IdlingResource mIdlingResource;

    @Before
    public void registerIdlingResource() {
    mIdlingResource = mActivityTestRule.getActivity().getIdlingResource();
    Espresso.registerIdlingResources(mIdlingResource);
    }

    @Test
    public void clickFilterBUtton_UpdatesRecyclerview() {

    int adapterSizeBeforeUpdate, adapterSizeAfterUpdate;

    adapterSizeBeforeUpdate = getRVcount();

    onView((withId(R.id.action_filter)))
            .perform(click());

    adapterSizeAfterUpdate= getRVcount();

    Assert.assertTrue(adapterSizeBeforeUpdate!=adapterSizeAfterUpdate);

    }

    @After
    public void unregisterIdlingResource() {
        if (mIdlingResource != null) {
            Espresso.unregisterIdlingResources(mIdlingResource);
        }
    }

    private int getRVcount(){
        GridView gridView = (GridView) 
mActivityTestRule.getActivity().findViewById(R.id.tea_grid_view);
        return gridView.getAdapter().getCount();
    }

}

0 个答案:

没有答案