android – 尝试单击导航按钮时Espresso AmbiguousViewMatcherException

前端之家收集整理的这篇文章主要介绍了android – 尝试单击导航按钮时Espresso AmbiguousViewMatcherException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
测试我正在尝试:进行购买,记下标题,然后返回到主页活动,然后按“home_image”这是一个图像按钮.

我的问题是我无法导航home_image.我按它,我得到一个伪造的重复ID.我相信重复ID是指具有相同ID的不可见片段.为什么在测试开始时不是问题很奇怪.由于文本限制,抱歉不得不截断日志.
我想做的就是直接启动家庭活动,但这个套件只是做一件简单事情的痛苦.即使是滑动命令也会出现此错误!第一次通过所有这些按钮工作,我认为这是浓缩咖啡的一个错误,我需要一个解决方案.

谢谢你的想法!

mSectionsPagerAdapter = new SectionsPagerAdapter( getFragmentManager() );

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPagerExtended) findViewById( R.id.pager );
    mViewPager.setAdapter( mSectionsPagerAdapter );
    mViewPager.setSwipeEnabled( true );

    final ImageButton homeImageButton = (ImageButton) findViewById( R.id.home_image );
    final ImageButton exploreImageButton = (ImageButton) findViewById( R.id.explore_image );
    final ImageButton profileImageButton = (ImageButton) findViewById( R.id.profile_image );

    mViewPager.setOnPageChangeListener( new ViewPager.SimpleOnPageChangeListener()
    {
        @Override
        public void onPageSelected( int position )
        {
            switch ( position )
            {
                case 0:
                    homeImageButton.setImageResource( R.drawable.home_icon );
                    exploreImageButton.setImageResource( R.drawable.explore_icon_inactive );
                    profileImageButton.setImageResource( R.drawable.profile_icon_inactive );
                    break;
                case 1:
                    homeImageButton.setImageResource( R.drawable.home_icon_inactive );
                    exploreImageButton.setImageResource( R.drawable.explore_icon );
                    profileImageButton.setImageResource( R.drawable.profile_icon_inactive );
                    break;
                case 2:
                    homeImageButton.setImageResource( R.drawable.home_icon_inactive );
                    exploreImageButton.setImageResource( R.drawable.explore_icon_inactive );
                    profileImageButton.setImageResource( R.drawable.profile_icon );
                    break;
            }
        }
    } );

    homeImageButton.setOnClickListener( new View.OnClickListener()
    {
        @Override
        public void onClick( View v )
        {
            mViewPager.setCurrentItem( 0 );
        }
    } );

    exploreImageButton.setOnClickListener( new View.OnClickListener()
    {
        @Override
        public void onClick( View v )
        {
            mViewPager.setCurrentItem( 1 );
        }
    } );

    profileImageButton.setOnClickListener( new View.OnClickListener()
    {
        @Override
        public void onClick( View v )
        {
            mViewPager.setCurrentItem( 2 );
        }
    } );

//这里的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="default"
            android:id="@+id/home"
    >

<LinearLayout
        android:id="@+id/tab_bar"
        android:layout_width="match_parent" android:layout_height="48dp"
        android:background="@color/****_orange_light"
        android:gravity="center"
        >

    <ImageButton
            android:id="@+id/home_image"
            android:layout_width="0dp" android:layout_height="match_parent"
            android:src="@drawable/home_icon"
            android:layout_weight="1"
            android:background="#00000000"
            />
    <ImageButton
            android:id="@+id/explore_image"
            android:layout_width="0dp" android:layout_height="match_parent"
            android:src="@drawable/explore_icon_inactive"
            android:layout_weight="1"
            android:background="#00000000"
            />
    <ImageButton
            android:id="@+id/profile_image"
            android:layout_width="0dp" android:layout_height="match_parent"
            android:src="@drawable/profile_icon_inactive"
            android:layout_weight="1"
            android:background="#00000000"
            />

    <android.support.v7.app.MediaRouteButton
            android:id="@+id/media_route_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:mediaRouteTypes="user"
            android:visibility="gone"

//测试输出

Running tests
Test running started
android.support.test.espresso.AmbiguousViewMatcherException: 'with id: com.*******.main:id/home_image' matches multiple views in the hierarchy.
Problem views are marked with '****MATCHES****' below.

View Hierarchy:
+>DecorView{id=-1,visibility=VISIBLE,width=1080,height=1776,has-focus=true,has-focusable=true,has-window-focus=true,is-clickable=false,is-enabled=true,is-focused=false,is-focusable=false,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}
|
+->LinearLayout{id=-1,child-count=2}
|
+-->ViewStub{id=16909070,visibility=GONE,width=0,height=0,has-focus=false,has-focusable=false,is-layout-requested=true,y=0.0}
|
+-->FrameLayout{id=-1,height=1701,y=75.0,child-count=1}
|
+--->FitWindowsLinearLayout{id=2131492941,res-name=action_bar_root,child-count=2}
|
+---->ViewStubCompat{id=2131492942,res-name=action_mode_bar_stub,y=0.0}
|
+---->NativeActionModeAwareLayout{id=16908290,res-name=content,child-count=1}
|
+----->RelativeLayout{id=2131492921,res-name=home,child-count=2}
|
+------>LinearLayout{id=2131493015,res-name=tab_bar,height=144,child-count=4}
|
+------->ImageButton{id=2131493016,res-name=home_image,width=360,is-clickable=true,is-focusable=true,y=0.0} ****MATCHES****
|
+------->ImageButton{id=2131493017,res-name=explore_image,x=360.0,y=0.0}
|
+------->ImageButton{id=2131493018,res-name=profile_image,x=720.0,y=0.0}
|
+------->MediaRouteButton{id=2131493019,res-name=media_route_button,desc=Cast,is-enabled=false,y=0.0}
|
+------>ViewPagerExtended{id=2131493020,res-name=pager,height=1557,y=144.0,child-count=3}
|
+------->LinearLayout{id=2131493215,res-name=fragment_member_home,x=-1080.0,child-count=3}
|
+-------->PagerSlidingTabStrip{id=2131493216,res-name=member_home_tabs,child-count=1}
|
+--------->LinearLayout{id=-1,child-count=2}
|
+---------->TextView{id=-1,text=MY CLASSES,input-type=0,ime-target=false,has-links=false}
|
+---------->TextView{id=-1,text=MY PATTERNS,has-links=false}
|
+-------->View{id=-1,height=3,y=0.0}
|
+-------->ViewPagerExtended{id=2131493217,res-name=member_home_view_pager,height=1554,y=3.0,child-count=2}
|
+--------->RelativeLayout{id=-1,child-count=2}
|
+---------->SwipeRefreshLayout{id=2131493222,res-name=swipe_container,child-count=2}
|
+----------->CircleImageView{id=-1,width=140,height=140,x=470.0,y=-140.0}
|
+----------->ListView{id=2131493178,res-name=courseGridView,child-count=1}
|
+------------>LinearLayout{id=-1,height=905,child-count=1}
|
+------------->RelativeLayout{id=2131493121,res-name=courseGridCell,child-count=6}
|
+-------------->FrameLayout{id=2131493122,res-name=courseImageFrame,height=613,child-count=2}
|
+--------------->DynamicNetworkImageView{id=2131493123,res-name=courseImage,y=0.0}
|
+--------------->ImageView{id=2131493124,res-name=playButtonImage,width=168,height=169,x=456.0,y=222.0}
|
+-------------->FontTextView{id=2131493125,res-name=titleText,width=936,height=166,x=72.0,y=613.0,text=40 Techniques Every Sewer Should Know,has-links=false}
|
+-------------->FontTextView{id=2131493126,res-name=instructorText,width=376,height=105,y=779.0,text=with Gail Yellen,has-links=false}
|
+-------------->View{id=2131493127,res-name=horizontalLine1,y=884.0}
|
+-------------->View{id=2131493128,res-name=footer_divider,y=884.0}
|
+-------------->View{id=-1,height=18,y=887.0}
|
+---------->SwipeRefreshLayout{id=2131493218,res-name=swipe_refresh_empty_container,y=0.0}
|
+----------->ScrollView{id=-1,child-count=1}
|
+------------>LinearLayout{id=2131493219,res-name=not_enrolled_ui,child-count=4}
|
+------------->ImageView{id=2131493016,y=0.0} ****MATCHES****
|
+------------->TextView{id=-1,text=Welcome to *****. Looks like you aren't enrolled in any classes yet.,has-links=false}
|
+------------->Button{id=2131493220,res-name=explore_****_link,text=Explore Classes,has-links=false}
|
+------------->IconButton{id=2131493221,res-name=commercial_button,text=Play Commercial,has-links=false}
|
+--------->FrameLayout{id=-1,x=1080.0,child-count=1}
|
+---------->RecyclerView{id=2131493227,res-name=recycler_view,child-count=0}
|
+------->LinearLayout{id=-1,child-count=1}
|
+-------->ListView{id=2131493190,res-name=categoryGridView,is-focused=true,child-count=3}
|
+--------->RelativeLayout{id=-1,height=504,child-count=3}
|
+---------->TextView{id=2131493267,res-name=explore_text,height=168,text=Explore,has-links=false}
|
+---------->LinearLayout{id=2131493268,res-name=linear_layout,y=168.0,child-count=2}
|
+----------->Button{id=2131493269,res-name=on_sale_button,width=513,x=18.0,text=On Sale,has-links=false}
|
+----------->Button{id=2131493270,res-name=free_button,x=549.0,text=Free,has-links=false}
|
+---------->TextView{id=2131493271,res-name=category_text,y=336.0,text=Categories,has-links=false}
|
+--------->LinearLayout{id=-1,height=540,y=504.0,child-count=2}
|

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:579)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:82)
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 com.****.****.main.tests.purchase.BasePurchaseTest.testHelperSeeIfTitleInLibrary(BasePurchaseTest.java:91)
at com.****.****.main.tests.purchase.PurchaseTests.testPurchaseIsInGallery(PurchaseTests.java:184)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
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 android.support.test.internal.runner.junit3.DelegatingTestResult.runProtected(DelegatingTestResult.java:90)
at junit.framework.TestResult.run(TestResult.java:118)
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:49)
at junit.framework.TestCase.run(TestCase.java:124)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:63)
at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)
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:1701)

解决方法

这不是Espresso中的错误.视图层次结构清楚地显示了具有相同ID的两个视图.这可能是由于视图位于ViewPager内部,而ViewPager是一个AdapterView.因此可以预期多个实例.

解决此问题,请在匹配视图时更具体.仅仅通过ID无济于事.由于主页按钮的其他ImageView不可见,因为它的父级是GONE,您可以简单地匹配:

onView(allOf(withId(..),withEffectiveVisibility(VISIBLE))).perform(click());
原文链接:https://www.f2er.com/android/316870.html

猜你在找的Android相关文章