met*_*ink 3 android automated-tests android-espresso
我有使用coordinatlayout,AppBarLayout和CollapsingToolbarLayout的布局.我想写一个点击列表中最后一项的测试.我滚动列表直到结束,然而,espresso中隐藏了所需的项目.要滚动列表,我使用:
onView(withId(R.id.widget_recycler_view)).perform(RecyclerViewActions.scrollToPosition(3));
要隐藏,滚动工具栏,我尝试了以下代码:
onView(withId(R.id.coordinator_layout)).perform(swipeUp());
onView(withId(R.id.toolbar)).perform(swipeUp());
Run Code Online (Sandbox Code Playgroud)
但是这段代码只隐藏了工具栏的一半,但这还不够.这是布局的代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/coordinator_layout"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="?attr/colorPrimary">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Dark"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_gravity="top"
app:layout_collapseMode="parallax"
android:background="?attr/colorPrimary"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.makeramen.RoundedImageView
style="@style/AvatarImage.MainActivity"
android:id="@+id/iv_avatar"
android:layout_marginRight="18dp"/>
<com.akbars.bankok.views.custom.TextViewFonted
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmallInverse"
android:layout_toRightOf="@id/iv_avatar"
android:id="@+id/name"
android:textSize="12sp"
app:customFont="Roboto-Medium.ttf"
android:textColor="@android:color/white"/>
<com.akbars.bankok.views.custom.TextViewFonted
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp"
app:customFont="Roboto-Light.ttf"
android:id="@+id/tv_balance"
android:layout_toRightOf="@id/iv_avatar"
android:layout_below="@id/name"
android:textColor="@android:color/white"/>
</RelativeLayout>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleSmall"
android:id="@+id/progress_bar"
android:visibility="gone"
android:layout_marginLeft="20dp"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<com.akbars.bankok.views.custom.SwipeEnableViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_view_pager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_marginBottom="@dimen/tab_bar_layout_height"/>
<com.akbars.bankok.views.custom.CustomTabLayout
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_navigation_tab"
android:id="@+id/tabs"
app:tabMode="fixed"
app:tabIndicatorColor="@android:color/transparent"
android:layout_gravity="center_horizontal|bottom"
style="@style/MyCustomTabLayout"
android:background="@android:color/white"
android:elevation="10dp"
/>
</android.support.design.widget.CoordinatorLayout>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/shadow"
android:background="@color/shadow"
android:visibility="invisible"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
style="@style/FABButton"
android:src="@drawable/ic_import_export_white_24dp"
app:backgroundTint="?attr/colorPrimary"
/>
<include layout="@layout/fab_layout" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
我附上我的考试.如果你滚动手,它的工作原理:)但它是必要的机器人化过程.
@Test
public void addNewPhone(){
onView(withId(R.id.widget_recycler_view)).perform(RecyclerViewActions.scrollToPosition(3));
onView(withId(R.id.coordinator_layout)).perform(swipeUp());
putDelay(2500);
onView(withId(R.id.layout_add_phone)).perform(click());
onView(withId(R.id.edit_letay_phone)).perform(replaceText("9586222922"));
closeSoftKeyboard();
onView(withText(R.string.add)).perform(click());
closeSoftKeyboard();
onView(withId(R.id.edit_code_letay_phone)).perform(replaceText("52461"));
onView(withId(R.id.bt_code_request)).perform(click());
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:滚动列表或布局时如何完全隐藏工具栏?
我在测试中使用以下自定义视图操作:
public static ViewAction collapseAppBarLayout() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(AppBarLayout.class);
}
@Override
public String getDescription() {
return "Collapse App Bar Layout";
}
@Override
public void perform(UiController uiController, View view) {
AppBarLayout appBarLayout = (AppBarLayout) view;
appBarLayout.setExpanded(false);
uiController.loopMainThreadUntilIdle();
}
};
}
Run Code Online (Sandbox Code Playgroud)
用法:
onView(withId(R.id.app_bar_layout)).perform(CustomViewActions.collapseAppBarLayout());
Run Code Online (Sandbox Code Playgroud)
请注意,您需要在设备设置中禁用动画,否则,Espresso将不会等到它完全崩溃,然后才能继续下一步.
| 归档时间: |
|
| 查看次数: |
816 次 |
| 最近记录: |