Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用android espresso访问自定义expandablelist中的子项大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图访问我的自定义expandablelist中的孩子.该列表如下所示:

Custom ExpandableList

我可以成功访问组:

Espresso.onView(allOf(withId(R.id.groupRowTextView1),withText(startsWith("Candida")))).perform(click());

当我尝试访问子EditText时:

Espresso.onData(is(instanceOf(Customexpandablelistadapter.class)))
            .inAdapterView(withId(R.id.aversionsListView))
            .onChildView(allOf(withId(R.id.childEditText),isDisplayed()))
            .atPosition(1)
            .perform(click());

我收到此错误

11-30 16:47:45.733    2479-2492/com.foodaversions.people I/TestRunner﹕ ----- begin exception -----
11-30 16:47:45.733    2479-2492/com.foodaversions.people I/TestRunner﹕ com.google.android.apps.common.tesTing.ui.espresso.PerformException: Error performing 'load adapter data' on view 'with id: is <2131099682>'.
        at com.google.android.apps.common.tesTing.ui.espresso.PerformException$Builder.build(PerformException.java:67)
        at com.google.android.apps.common.tesTing.ui.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:57)
        at com.google.android.apps.common.tesTing.ui.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:40)
        at com.google.android.apps.common.tesTing.ui.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:159)
        at com.google.android.apps.common.tesTing.ui.espresso.ViewInteraction.doperform(ViewInteraction.java:90)
        at com.google.android.apps.common.tesTing.ui.espresso.ViewInteraction.perform(ViewInteraction.java:73)
        at com.google.android.apps.common.tesTing.ui.espresso.DataInteraction.load(DataInteraction.java:135)
        at com.google.android.apps.common.tesTing.ui.espresso.DataInteraction.perform(DataInteraction.java:112)
        at com.foodaversions.people.ModifyAversionsTest.SELEctGlutenIntolerance(ModifyAversionsTest.java:49)
        at com.foodaversions.people.ModifyAversionsTest.test(ModifyAversionsTest.java:20)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.test.instrumentationTESTCase.runMethod(instrumentationTESTCase.java:214)
        at android.test.instrumentationTESTCase.runTest(instrumentationTESTCase.java:199)
        at android.test.ActivityinstrumentationTESTCase2.runTest(ActivityinstrumentationTESTCase2.java:192)
        at junit.framework.TESTCase.runBare(TESTCase.java:134)
        at junit.framework.TestResult$1.protect(TestResult.java:115)
        at junit.framework.TestResult.runProtected(TestResult.java:133)
        at junit.framework.TestResult.run(TestResult.java:118)
        at junit.framework.TESTCase.run(TESTCase.java:124)
        at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
        at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
        at android.test.instrumentationTestRunner.onStart(instrumentationTestRunner.java:555)
        at com.google.android.apps.common.tesTing.testrunner.GoogleinstrumentationTestRunner.onStart(GoogleinstrumentationTestRunner.java:167)
        at android.app.instrumentation$instrumentationThread.run(instrumentation.java:1584)
 Caused by: java.lang.RuntimeException: No data found matching: is an instance of com.foodaversions.people.adapters.Customexpandablelistadapter contained values: <[Data: GroupStruct{id=6,name='Alcohol Intolerance',sourceTable=AVERSION_NAME,modifierEnum=EDITABLE,childData=
ChildStruct{id=-1,name='<Add Ingredient>',sourceTable=null,joinTable=null,modifierEnum=ADD_TEXT_VIEW}
}
(class: class com.foodaversions.people.data.ExpandableGroupdata$GroupStruct) token: 0,Data: GroupStruct{id=8,name='Candida',childData=
ChildStruct{id=13,name='Sugar',sourceTable=AVERSION,modifierEnum=deletE}
ChildStruct{id=-1,modifierEnum=ADD_TEXT_VIEW}
}
(class: class com.foodaversions.people.data.ExpandableGroupdata$GroupStruct) token: 1,Data: ChildStruct{id=13,modifierEnum=deletE} (class: class com.foodaversions.people.data.ChildStruct) token: 2,Data: ChildStruct{id=-1,modifierEnum=ADD_TEXT_VIEW} (class: class com.foodaversions.people.data.ChildStruct) token: 3,Data: GroupStruct{id=3,name='Dairy Intolerance',modifierEnum=ADD_TEXT_VIEW}
}
(class: class com.foodaversions.people.data.ExpandableGroupdata$GroupStruct) token: 4,Data: GroupStruct{id=10,name='Egg Allergy',modifierEnum
11-30 16:47:45.733    2479-2492/com.foodaversions.people I/TestRunner﹕ ----- end exception -----

使用onData扩展组

我无法让孩子点击工作,我发现更容易弄清楚如何首先扩展列表:

Espresso.onData(allOf(is(instanceOf(ExpandableGroupdata.GroupStruct.class)),withGroupName("Candida")))
            .inAdapterView(withId(R.id.aversionsListView))
            .perform(click());

我认为它可能有助于点击子视图的解决方案.

感谢@Anna提供解决此问题所需的方向:

// aversionsListView is the id of the ExpandableListView containing the expandable list
    Espresso.onData(allOf(is(instanceOf(ChildStruct.class)),withChildName("<Add Ingredient>")))
            .inAdapterView(withId(R.id.aversionsListView))
            .check(matches(isDisplayed()))
            .perform(click());

匹配器:

public static Matcher<Object> withChildName(String Name) {
    checkNotNull(Name);
    return withChildName(equalTo(Name));
}

public static Matcher<Object> withChildName(final Matcher<String> Name) {
    checkNotNull(Name);
    // ChildStruct is the Class returned by Baseexpandablelistadapter.getChild()
    return new BoundedMatcher<Object,ChildStruct>(ChildStruct.class){

        @Override
        public Boolean matchesSafely (final ChildStruct childStruct){
            return name.matches(childStruct.Name);
        }

        @Override
        public void describeTo (Description description){
            name.describeTo(description);
        }
    } ;
}

解决方法

使用适配器视图有几点.
如果它是从适配器加载数据,则无法使用简单视图,就像Espresso不会等到数据加载一样.
如果你要使用

Espresso.onData(is(instanceOf(Customexpandablelistadapter.class))) .inAdapterView(withId(R.id.aversionsListView))

然后它将返回AdapterListView.如果要单击列表的第二项,则.atPosition(1)可以使用.如果R.id.childEditText是列表元素的类型,则必须将其与自定义匹配器匹配.你不能一起使用.onChildView(allOf(withId(R.id.childEditText),isDisplayed())).atPosition(1).

我建议写一个自定义匹配器.请查看this.
自定义匹配器正在帮助获取列表项,并在开头匹配它.您可以尝试编写类似这样的内容.

onData(allOf(is(instanceOf(<List item class for adapter>.class)),withListItemcheck(check_value))).check(matches(isDisplayed()));

你的匹配器可以像这样:

public static Matcher<Object> withListItemcheck(final <Type> check_value) {
    checkNotNull(check_value);
    return new BoundedMatcher<Object,<List_Item_class>>(<List_Item_class>.class) {
        private String m_message = "";

        @Override
        public void describeTo(Description d) {
            d.appendText(m_messagE);
        }

        @Override
        public Boolean matchesSafely(<List_Item_class> listItem) {
            m_message = "Expected " + listItem+ " and got ";
            if (listItem== null) {
                m_message +=  "empty";
                return false;
            }
            return <assertion to check `check_value` is corresponding to `listItem` or not>;
        }
    };
}

大佬总结

以上是大佬教程为你收集整理的使用android espresso访问自定义expandablelist中的子项全部内容,希望文章能够帮你解决使用android espresso访问自定义expandablelist中的子项所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。