0

This is my use case.

  1. Load a recycler view
  2. Click on an item that will show a dialog with a radio group
  3. Select a radio button (# 2) in my test
  4. Click OK
  5. Show the recycler view showing the updated text.

My unit test

@Test
public void onNonODeviceSwitchIsShown() throws Exception
{


onView(withText(getString(R.string.PushNotifications))).perform(click());
            onView(withId(R.id.recycler_view))
                .perform(RecyclerViewActions.actionOnItem(hasDescendant(withText(R.string.general_notification_tone)),
                    click()));
            UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

        // Search for correct button in the dialog.
        UiObject toneText = uiDevice.findObject(new UiSelector().text("Adara"));
        toneText.click();
        UiObject2 okButton = uiDevice.findObjects(By.clazz(Button.class)).get(1);
        okButton.clickAndWait(Until.newWindow(), 5000);
        onView(withId(R.id.recycler_view)).check(
            ViewAssertions.matches(allOf(
                withText(R.string.custom))));

}

It fails with the following message, which means it does not detect any radio buttons:

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: '(with string from resource id: <2131887061>)' doesn't match the selected view. Expected: (with string from resource id: <2131887061>) Got: "RecyclerView{id=2131365043, res-name=recycler_view, visibility=VISIBLE, width=1080, height=1584, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=true, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@bc6b8c6, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=9}"

This is the dialog which upon closing will show the recycler view.

Dialog containing Radio Buttons

This is the part of the recycler view after the dialog is closed.

Recycler View

This is the line that fails

onView(withId(R.id.recycler_view)).check(
                    ViewAssertions.matches(allOf(
                        withText(R.string.custom))));

When I check the layout inspector, I see the following set up

RelativeLayout
  |
  | Ab (id/title) TextView - "General"
  | Ab (id/summary) TextView - "Custom"

I don't know why the assertions are failing even though the Custom text does exist in the recycler view.

Kartik
  • 2,541
  • 2
  • 37
  • 59
  • This looks like a system dialog, have you tried this instead `UiObject mText = uiDevice.findObject(new UiSelector().text("None"));` `mText.click();` – vk.4884 Oct 03 '19 at 15:20
  • This won't support localisation, will it? – Kartik Oct 04 '19 at 21:01
  • No, this won't support localisation. However for RecyclerView you might need to give the cell position to target the assertions. Hope this answer helps you. https://stackoverflow.com/a/31475962/1597616 – vk.4884 Oct 07 '19 at 16:20
  • That just validates that the id matches, but it does not recognise the click and does not invoke the ViewAction event. – Kartik Oct 11 '19 at 10:31

0 Answers0