Check if a dialog is displayed with Espresso

后端 未结 6 593
孤街浪徒
孤街浪徒 2020-12-04 14:07

I\'m trying to write some tests with the new android-test-kit (Espresso). But I can\'t find any information on how to check if a dialog is displayed and per

6条回答
  •  温柔的废话
    2020-12-04 14:54

    If you have an AlertDialog like that:

    You can check if the components are displayed:

    int titleId = mActivityTestRule.getActivity().getResources()
            .getIdentifier( "alertTitle", "id", "android" );
    
    onView(withId(titleId))
            .inRoot(isDialog())
            .check(matches(withText(R.string.my_title)))
            .check(matches(isDisplayed()));
    
    onView(withId(android.R.id.text1))
            .inRoot(isDialog())
            .check(matches(withText(R.string.my_message)))
            .check(matches(isDisplayed()));
    
    onView(withId(android.R.id.button2))
            .inRoot(isDialog())
            .check(matches(withText(android.R.string.no)))
            .check(matches(isDisplayed()));
    
    onView(withId(android.R.id.button3))
            .inRoot(isDialog())
            .check(matches(withText(android.R.string.yes)))
            .check(matches(isDisplayed()));
    

    and perform an action:

    onView(withId(android.R.id.button3)).perform(click());
    

提交回复
热议问题