Tac*_*nko 3 android android-layout android-espresso android-nestedscrollview
我正在尝试用Espresso测试NestedScrollView,但出现错误:
“将不会执行操作,因为目标视图与以下一个或多个约束不匹配:至少90%的视图区域显示给用户。”
我知道此错误是因为Android未检测到要单击的按钮,也就是说,我需要滚动到底部才能看到该按钮。我也已经阅读了scrollTo()对NestedScrollView不可用,所以我不能使用它。我想我必须滚动到NestedScrollView的底部才能看到该按钮,我既不确定这一点,也不知道该怎么做。
我想单击红色按钮,但它没有可见性。我已经看到了几个堆栈问题和一些教程,但是我不知道如何滚动到嵌套的底部。
应用程序工具栏的代码为:
<?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>
Run Code Online (Sandbox Code Playgroud)
NestedScrollView的代码为:
<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>
Run Code Online (Sandbox Code Playgroud)
如您所见,按钮的id是idBEventoNuevoRegistrar,而contentDescription是eventoNuevoRegistrar
最终在浓缩咖啡中,我尝试了以下方法。
onData(withContentDescription(R.string.eventoNuevoRegristar)).check(matches(isDisplayed())).perform(click());
Run Code Online (Sandbox Code Playgroud)
会产生错误:可从类分配:类android.widget.AdapterView匹配层次结构中的多个视图。
onView(withId(R.id.idBEventoNuevoRegistrar)).check(matches(isDisplayed())).perform(click());
Run Code Online (Sandbox Code Playgroud)
这将产生错误:由于目标视图与以下一个或多个约束不匹配,因此将不执行操作:至少90%的视图区域显示给用户。
我也尝试过:
onData(withId(R.id.idBEventoNuevoRegistrar))
.check(matches(isDisplayed()))
.perform(click());
Run Code Online (Sandbox Code Playgroud)
产生错误:android.support.test.espresso.AmbiguousViewMatcherException:'可从类分配:类android.widget.AdapterView'匹配层次结构中的多个视图
在错误日志中,我可以看到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}
Run Code Online (Sandbox Code Playgroud)
我要单击的按钮:
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}
Run Code Online (Sandbox Code Playgroud)
提前致谢。
编辑:
终于我找到了解决方案。答案是在UiAutomator测试中。
简单如:
UiScrollable appViews = new UiScrollable(
new UiSelector().scrollable(true));
appViews.scrollForward();
Run Code Online (Sandbox Code Playgroud)
希望对您有所帮助。
-先前的反应-
阅读@Be_Negative答案后,我发现我必须创建一个新类才能在NestedScrollView中滚动,因为它不是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";
}}
Run Code Online (Sandbox Code Playgroud)
尝试后:onView(withId(R.id.fragment_evento_crear))。perform(ScrollToAction.betterScrollTo());
它再次崩溃。查找信息后,我发现了这一点: 尝试滚动查看,但未显示该视图
最后,解决方案如下:
1-创建一个类,就像@Be_Negative所建议的那样,我放在下面。
2-在NestedScrollView中删除填充。
3-调用:onView(withId(R.id.button))。perform(ScrollToAction.betterScrollTo());
我面临着完全相同的问题,我非常希望 @be-negative 和 @tachenko 提供的解决方案能够发挥作用,但他们没有。对我有用的只是简单地使用swipeUp()而不是scrollTo().
| 归档时间: |
|
| 查看次数: |
5427 次 |
| 最近记录: |