Android espresso NestedScrollView,如何滚动到底部
我正在尝试使用Espresso测试NestedScrollView,但出现错误:
I'm trying to test with Espresso a NestedScrollView but I am having an error:
将不执行操作,因为目标视图与以下一个或多个约束不匹配: 至少有90%的视图区域显示给用户."
"Action will not be performed because the target view does not match one or more of the following constraints: at least 90 percent of the view's area is displayed to the user."
我知道此错误是因为Android未检测到我要单击的按钮,也就是说,我需要滚动到底部才能看到该按钮.我也已经阅读了scrollTo()对NestedScrollView不可用,所以我不能使用它. 我想我必须滚动到NestedScrollView的底部才能看到该按钮,我既不确定这一点,也不知道该怎么做.
I know that this error is because Android does not detect the button that I want to click, that is, I need to scroll to bottom in order to see the button. I also have read that scrollTo() is not available for NestedScrollView so I cannot use it. I guess that I have to scroll to bottom of the NestedScrollView to see the button, neither I am not sure of this nor I know how to do it.
我想单击红色的按钮,但是它没有可见性. 我已经看到了几个堆栈问题和一些教程,但是我不知道如何滚动到嵌套的底部.
I want to click in the button in red but it has not visibility. I have seen several stack questions and some tutorials but I can not figure out how to scroll to bottom of the Nested.
应用程序工具栏的代码为:
The code of the app toolbar is:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.findandgo.activity.MenuPrincipal"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll"
app:popupTheme="@style/AppTheme.AppBarOverlay"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />`
</android.support.design.widget.CoordinatorLayout>
NestedScrollView的代码是:
The code of the NestedScrollView is:
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_evento_crear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
android:background="@drawable/fondo"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".Evento"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:nestedScrollingEnabled="false"
android:padding="@dimen/size_20"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<com.findandgo.custom.CustomFontEditText
android:id="@+id/idEtEventoNuevoNombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/idSpEventoNuevoCategoria"
android:layout_alignStart="@+id/idSpEventoNuevoCategoria"
android:layout_below="@id/tool_bar"
android:hint="@string/sEventoNombre"
android:textSize="@dimen/size_12"
app:font="@string/font_name_source_amatic_regular" />
...
<ImageButton
android:id="@+id/idBEventoNuevoRegistrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/idEtEventoNuevoDescripcion"
android:layout_centerHorizontal="true"
android:background="@drawable/btn_save"
android:contentDescription="@string/eventoNuevoRegristar"
android:textStyle="bold"
android:visibility="visible" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
如您所见,按钮的ID为idBEventoNuevoRegistrar,contentDescription为eventoNuevoRegistrar
As you can see, the id of the button is idBEventoNuevoRegistrar and the contentDescription is eventoNuevoRegistrar
最后在浓缩咖啡中,我尝试了以下方法.
Finally in espresso I have tried the following.
onData(withContentDescription(R.string.eventoNuevoRegristar)).check(matches(isDisplayed())).perform(click());
产生错误:可从类分配:android.widget.AdapterView类与层次结构中的多个视图匹配.
That produces the error:is assignable from class: class android.widget.AdapterView matches multiple views in the hierarchy.
onView(withId(R.id.idBEventoNuevoRegistrar)).check(matches(isDisplayed())).perform(click());
产生错误: 由于目标视图与以下一个或多个约束不匹配,因此不会执行操作: 至少有90%的视图区域显示给用户.
That produces the error: Action will not be performed because the target view does not match one or more of the following constraints: at least 90 percent of the view's area is displayed to the user.
我也尝试过:
onData(withId(R.id.idBEventoNuevoRegistrar))
.check(matches(isDisplayed()))
.perform(click());
这会产生错误:android.support.test.espresso.AmbiguousViewMatcherException:'可从类分配:类android.widget.AdapterView'匹配层次结构中的多个视图
That produces error: android.support.test.espresso.AmbiguousViewMatcherException: 'is assignable from class: class android.widget.AdapterView' matches multiple views in the hierarchy
在错误日志中,我可以看到NestedScrollView:
In the error Log, I can see the NestedScrollView:
NestedScrollView{id=2131362239, res-name=fragment_evento_crear, visibility=VISIBLE, width=720, height=1118, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
以及我要单击的按钮:
AppCompatImageButton{id=2131362214, res-name=idBEventoNuevoRegistrar, desc=eventoNuevoRegristar, visibility=VISIBLE, width=128, height=128, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=296.0, y=1313.0}
谢谢.
已
最后,我找到了解决方案.答案是在UiAutomator测试中.
Finally I found the solution. The answer was in UiAutomator Test.
很简单:
UiScrollable appViews = new UiScrollable(
new UiSelector().scrollable(true));
appViews.scrollForward();
希望对您有所帮助.
-先前的反应---
在阅读@Be_Negative答案后,我发现我必须创建一个新类才能在NestedScrollView中滚动,因为它不是Scrollview的后代.
After reading @Be_Negative answer I have found that I have to create a new class in order to scroll in NestedScrollView due to it is not a descendant of Scrollview.
课程是:
public final class ScrollToAction implements ViewAction {
private static final String TAG = ScrollToAction.class.getSimpleName();
@SuppressWarnings("unchecked")
@Override
public Matcher<View> getConstraints() {
return allOf(withEffectiveVisibility(Visibility.VISIBLE), isDescendantOfA(anyOf(
isAssignableFrom(ScrollView.class), isAssignableFrom(HorizontalScrollView.class), isAssignableFrom(NestedScrollView.class))));
}
@Override
public void perform(UiController uiController, View view) {
if (isDisplayingAtLeast(80).matches(view)) {
Log.i(TAG, "View is already displayed. Returning.");
return;
}
Rect rect = new Rect();
view.getDrawingRect(rect);
if (!view.requestRectangleOnScreen(rect, true /* immediate */)) {
Log.w(TAG, "Scrolling to view was requested, but none of the parents scrolled.");
}
uiController.loopMainThreadUntilIdle();
if (!isDisplayingAtLeast(80).matches(view)) {
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(new RuntimeException(
"Scrolling to view was attempted, but the view is not displayed"))
.build();
}
}
public static ViewAction betterScrollTo() {
return ViewActions.actionWithAssertions(new ScrollToAction());
}
@Override
public String getDescription() {
return "scroll to";
}}
尝试后: onView(withId(R.id.fragment_evento_crear)).perform(ScrollToAction.betterScrollTo());
After trying: onView(withId(R.id.fragment_evento_crear)).perform(ScrollToAction.betterScrollTo());
再次崩溃.在查找信息之后,我发现了这一点: 尝试滚动查看,但视图没有显示
It chrashes again. After looking for information, I found this: Scrolling to view was attempted, but the view is not displayed
最后,解决方法如下:
1-按照@Be_Negative的要求创建一个类,我放在下面.
1- Create a class as @Be_Negative suguested and I put below.
2-删除NestedScrollView中的填充.
2- Delete Padding in NestedScrollView.
3-调用:onView(withId(R.id.button)). perform(ScrollToAction.betterScrollTo());
3- Call: onView(withId(R.id.button)). perform(ScrollToAction.betterScrollTo());