使用通过xml:
从Activity视图启动的片段 <fragment
android:id="@+id/listconversations"
android:contentDescription="my list fragment"
android:name="com.example.ui.MyListFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="visible"
android:layout_below="@+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
它有一个SimpleCursorAdapter支持的项目行的自定义列表适配器:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:clickable="true"
android:paddingLeft="8dp"
android:paddingRight="4dp"
android:paddingTop="8dp"
android:paddingBottom="4dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ib_contact_details"
android:src="@drawable/green_arrow"
android:background="@color/transparent"
android:clickable="true"
android:layout_marginLeft="16dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Somebody Else Senior"
android:paddingBottom="4dp"
android:id="@+id/tv_marker_title"
android:textColor="@color/black"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/ib_contact_details"
android:layout_toStartOf="@+id/ib_contact_details" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="the conversation"
android:textColor="@color/black"
android:id="@+id/tv_message"
android:layout_below="@+id/tv_marker_title"
android:layout_alignLeft="@+id/tv_marker_title"
android:layout_alignStart="@+id/tv_marker_title"
android:layout_toLeftOf="@+id/ib_contact_details"
android:layout_toStartOf="@+id/ib_contact_details" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:gravity="center_horizontal"
android:textColor="@color/gray_btn_bg_pressed_color"
android:id="@+id/tv_timestamp"
android:layout_below="@+id/tv_message"
android:layout_toLeftOf="@+id/ib_contact_details"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_partnerserial"
android:textColor="@color/blue"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/ib_contact_details"
android:visibility="invisible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_imageurl"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/ib_contact_details"
android:visibility="invisible" />
</RelativeLayout>
尝试使用SimpleCursorAdapter和自定义列表视图行在ListFragment上运行以下espresso测试时:
onData(allOf(is(instanceOf(ListFragment.class)))).check(matches(isDisplayed()));
onData(anything())
.inAdapterView(withId(R.id.listconversations))
.atPosition(0)
.perform(click());
发生以下错误:
Running tests
测试运行已开始
android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'is assignable from class: class android.widget.AdapterView'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:70)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:185)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at android.support.test.espresso.DataInteraction.load(DataInteraction.java:151)
at android.support.test.espresso.DataInteraction.check(DataInteraction.java:141)
at com.example.TestMainActivity.shouldBeAbleToLaunchMainScreen(TestMainActivity.java:149)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at com.example.ActivityRule$1.evaluate(ActivityRule.java:101)
at org.junit.rules.RunRules.evaluate(RunRules.java:18)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:270)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)
Caused by: java.lang.RuntimeException: No data found matching: (is an instance of android.support.v4.app.ListFragment) contained values: <[Data: Row 0: {name:"Peter B.", lastlines:"just a message test", lastcontacted:"2015-11-12 12:14:47", image_url:"http://example.com/7618de72-2015-09-01 07:35:42-droid_test1.jpg", partnerserial:"261cc6", _id:76, } (class: android.content.ContentResolver$CursorWrapperInner) token: 0, Data: Row 1: {name:"Community Help", lastlines:"what's going on?", lastcontacted:"2015-10-06 17:58:20", image_url:"https://example.com/image.jpg", partnerserial:"bbb321de", _id:77, } ... token: 4]>
at android.support.test.espresso.action.AdapterDataLoaderAction.perform(AdapterDataLoaderAction.java:96)
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Finish
我也尝试过:
onData(allOf(is(instanceOf(SimpleCursorAdapter.class)))).check(matches(isDisplayed()));
但这也不起作用。
答案 0 :(得分:0)
我可以使用onView来处理以下内容:
onView(allOf(withId(R.id.ib_contact_details), hasSibling(withText("Blah Blah"))))
.perform(click());
答案 1 :(得分:0)
我使用的最简单的解决方案是:
onData(CursorMatchers.withRowString("NAME", "hbhj"))
.perform(click());
在这里, NAME:是列中的。
hbhj:名称是否显示在listview元素上。