MaterialButtonToggleGroup Toggles Not Visible During Android Espresso Tests

13 Views Asked by At

I'm working on an Android application that uses Material Design components extensively. The app runs perfectly fine under normal conditions, with all UI elements including MaterialButtonToggleGroup toggles for Ctrl, Alt/Option, and Command buttons being visible and functional. However, when running Espresso tests aimed at UI testing, these toggle buttons do not appear in the mocked environment, leading to failed tests due to invisibility. These components are not found by Espresso during testing, leading to NoMatchingViewException.

Error in Question:

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: view.getId() is <2131296415/com.example.comp0016_group23_app:id/commandButton>

XML Layout: Here's a simplified version of the layout that includes the MaterialButtonToggleGroup:

<com.google.android.material.button.MaterialButtonToggleGroup
    android:id="@+id/ctrlToggleButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/inputField"
    android:layout_marginStart="5dp"
    android:layout_marginEnd="5dp">

    <Button
        android:id="@+id/ctrlButton"
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ctrl"
        android:textSize="6pt"/>
</com.google.android.material.button.MaterialButtonToggleGroup>

This is a snippet from the Espresso test where the visibility check fails:

@Test
public void testIsCtrlButtonDisplayed() {
    onView(withParent(withId(R.id.ctrlToggleButton))).check(matches(isDisplayed()));
}

I tried to find the IDs of the toggle buttons in the original fragment in another way, crashes anyway.

0

There are 0 best solutions below