从在Android 9设备上录制浓缩咖啡测试开始,我得到了一段代码,用于在Spinger下拉列表中选择一个项目,如下所示:
ViewInteraction spinner = onView(
allOf(withId(R.id.ModeDropDown),
childAtPosition(
allOf(withId(R.id.activity_setup_controls),
childAtPosition(
withId(R.id.activity_setup_layout),
0)),
2)));
spinner.perform(scrollTo(), click());
DataInteraction checkedTextView = onData(anything())
.inAdapterView(childAtPosition(
withClassName(is("android.widget.PopupWindow$PopupBackgroundView")),
0))
.atPosition(mode); // drop-down position "mode"
checkedTextView.perform(click());
此代码在Android 9上运行良好,但当我将其带到Android 5设备时,它开始在调用checkedTextView.执行(click())时失败;的:
"androidx. test.espresso.NoMatchingViewException:在层次结构中找不到匹配的视图:父view.getClass().getName()中位置0的儿童匹配:is"android.widget.PopupWindow$PopupBack地View"如果目标视图不是视图层次结构的一部分,您可能需要使用Espresso.onData从以下AdapterViews之一加载它:android.widget.ListPopupWindow$DropDownListView{32dcb2b9VFED.VC……24,24-887,606}"。
所以我在Android 5设备上重新录制了测试,DataInteraction更改为:
DataInteraction checkedTextView = onData(anything())
.inAdapterView(withClassName(is("android.widget.ListPopupWindow$DropDownListView")))
.atPosition(mode); // drop-down position "mode"
请注意,错误建议反映在Android 5设备上浓缩咖啡录音机生成的内容中。但是,第二批代码在Android 5上运行良好,但在Android 9设备上出现以下错误:
"android. test.espresso.NoMatchingViewException:在层次结构中找不到匹配的视图:view.getClass().getName()匹配:is"android.widget.ListPopupWindow$DropDownListView"如果目标视图不是视图层次结构的一部分,您可能需要使用Espresso.onData从以下AdapterViews之一加载它:android.widget.DropDownListView{1172231VFED.VC……16,16-592,406}"。
如果有关系,活动布局XML在ScrollView中包装的RelativeLayout中实现了一个下拉微调器,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_setup_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:background="@drawable/maingadget3x"
android:fillViewport="true"
android:scrollbars="none"
tools:context="com.mycompany.myapp.SetupActivity">
<RelativeLayout
android:id="@+id/activity_setup_controls"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<View
android:id="@+id/separator1"
android:layout_width="match_parent"
android:layout_height="5dp" />
<TextView
android:id="@+id/ModeSetupHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/separator1"
android:layout_margin="5sp"
android:gravity="left"
android:maxLines="2"
android:singleLine="false"
android:text="@string/SetupModeProfileHeadingString"
android:textSize="28sp"
android:textStyle="bold" />
<Spinner
android:id="@+id/ModeDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ModeSetupHeading"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown"
tools:ignore="DuplicateSpeakableTextCheck" />
</RelativeLayout>
</ScrollView>
我是否一直在处理OS依赖于版本的测试代码,还是有一个我没有看到的解决这个问题的方法?
我有点沮丧,没有人愿意回答这个问题,但我遇到了一个类似问题的解决方案@Android浓缩咖啡检查适合我的选定微调文本:
DataInteraction checkedTextView = onData(anything()).atPosition(mode);
checkedTextView.perform(click());
我还提供了一张支票,以确保操作成功:
String modeString = getResourceString(modeStringIndex);
onView(withId(R.id.ModeDropDown))
.check(matches(withSpinnerText(containsString(modeString))));
这不是那篇文章的公认答案,但是由于公认的答案包含死参考链接,我觉得不得不不使用我不完全理解的答案。我希望其他人觉得这很有用。