Search code examples
javaandroidbarista

Get access to View object during testing


I'm using Barista to test my Android app. At a certain point in my activity's lifecycle, a QR code is displayed on screen. I'm able to successfully test that the image appears on screen:

BaristaVisibilityAssertions.assertDisplayed(R.id.qrCode);

Is there a way to get access to the actual ImageView object? I want to inspect its Bitmap and verify that the QR code's contents are correct.


Solution

  • It suffices to get access to the activity object as, from there, you can call findViewById. Assuming you have a BaristaRule<MyActivity>:

    public class MyActivityTest {
        @Rule
        public BaristaRule<MyActivity> baristaRule = BaristaRule.create(MyActivity.class);
    
        ...
    }
    

    you can get the activity by baristaRule.getActivityTestRule().getActivity().