'"Unresolved reference: PopupBackgroundView"

I have a problem. After the espresso test recorded, found an error shown "Unresolved reference: PopupBackgroundView"

[Espresson Test code]:

val materialTextView = onData(anything()).inAdapterView(childAtPosition(withClassName(is("android.widget.PopupWindow$PopupBackgroundView")), 0)).atPosition(4)
materialTextView.perform(click())
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0-alpha03'
androidTestImplementation 'androidx.test:rules:1.4.0-alpha05'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.0-alpha03'


Solution 1:[1]

The issue here is that the class name that you are seeing is an internal, platform-level class that may vary on different OS builds.

For instance a skinned build made by a manufacturer (think Samsung, Sony, Huawei etc.) might have a modified version of the PopupWindow class that is styled differently or have some extra attributes for configuration associated with it, and could be called something like com.manufacturer.widget.PopupWindow$CustomPopupBackgroundView.

So there wouldn't be a match if you developed the Espresso test on a Pixel device using the stock Android class name, and then ran it on a device with this modified UI class; an instance of the class wouldn't exist in the view hierarchy, and the test could fail dependent on the device it was being run on.

For that reason, platform or system-level UI classes shouldn't be directly referenced in Espresso tests. Instead you can use a root matcher such as isPlatformPopup() when you need Espresso to tap on auto-complete text or other system-owned UI components. That way it'll always find the component that you're looking for regardless which device you run the test on.

So try this code that replaces the withClassName search for the isPlatFormPopup() method instead:

val materialTextView = onData(anything())
    .inRoot(isPlatformPopup()) // this line is the key part
    .atPosition(4)

materialTextView.perform(click())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MattMatt