lundi 6 juin 2016

Android Espresso creates AmbiguousViewMatcherException but only after rotation

The problem

Android espresso returns AmbiguousViewMatcherException but only after rotation has occurred (Rotation supplied by Robotium because Espresso will not rotate my activity).

What I think is happening

So I thought about why this could be Hmm after rotation
What happens after rotation?
The old fragment is detached and destroyed etc and the new fragment is created but the fact that duplicate buttons are found only after rotation leads me to believe that maybe just maybe the old fragment is hanging around a little too long and maybe just maybe Espresso is collecting 2 buttons from 2 fragments, one button from the outgoing fragment and one from the new fragment
I have also tested it with no rotation commands but with rotation supplied by physically rotating the device with the same result

The testing code

solo=new Solo(getInstrumentation(), getActivity());
        myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        ViewInteraction boatNameInteraction = onView(withId(R.id.edit_txt_boat_name));
        boatNameInteraction.perform(closeSoftKeyboard());
        boatNameInteraction.perform(clearText(), typeText(extraBoat.getBoatName()));
        onView(withId(R.id.edit_txt_boat_make)).perform(clearText(), typeText(extraBoat.getBoatMake()));
        onView(withId(R.id.edit_txt_boat_model)).perform(clearText(), typeText(extraBoat.getBoatModel()));
        //Check that data is correct and click on Save button
        try
        {
            getInstrumentation().waitForIdleSync();
            getInstrumentation().waitForIdleSync();
            mPass ="7";
            writeAll();
        }
        catch (AssertionFailedError AfE)
        {
            mPass="0";
            writeAll();
            throw AfE;
        }
        mInput= String.format("%s, %s, %s",extraBoat.getBoatName(), extraBoat.getBoatMake(), extraBoat.getBoatModel());
        ViewInteraction clickSaveBoatInteraction = onView(withId(R.id.btn_save_boat));
        clickSaveBoatInteraction.perform(scrollTo());
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    SystemClock.sleep(1000);
    solo.setActivityOrientation(Solo.LANDSCAPE);
    SystemClock.sleep(1000);
    solo.setActivityOrientation(Solo.PORTRAIT);
    SystemClock.sleep(1000);
    clickSaveBoatInteraction.perform(closeSoftKeyboard(), click());

The error report

android.support.test.espresso.AmbiguousViewMatcherException: 'with id: com.blackstephen.line_wizard.sail_boat.view:id/btn_save_boat' matches multiple views in the hierarchy. Problem views are marked with '****MATCHES****' below.


+--------->Button{id=2131624108, res-name=btn_save_boat, visibility=VISIBLE, width=204, height=72, 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=240.0, y=8.0, text=Save, input-type=0, ime-target=false, has-links=false} ****MATCHES****


+--------->Button{id=2131624108, res-name=btn_save_boat, visibility=VISIBLE, width=204, height=72, 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=240.0, y=8.0, text=Save, input-type=0, ime-target=false, has-links=false} ****MATCHES****

Aucun commentaire:

Enregistrer un commentaire