Shopify / android-testify

Add screenshots to your Android tests
https://testify.dev
MIT License
231 stars 23 forks source link

BottomSheetDialogFragment not included in screenshots? #243

Closed amanzan closed 2 years ago

amanzan commented 2 years ago

Describe the bug Trying to record the baseline screenshots for an app with a BottomSheetDialogFragment opened. The screenshot doesn't include the BottomSheetDialogFragment, only the everything that is behind it

This issue relates to:

To Reproduce Steps to reproduce the behavior:

  1. App with a BottomSheetDialogFragment, open the dialog
  2. Create a test and click on 'Record baseline' on Android Studio
  3. Check the taken screenshot and the BottomSheetDialogFragment is not there

Expected behavior The opened BottomSheetDialogFragment should be included in the screenshot

Screenshots App with BottomSheetDialogFragment opened (disregard bottom nav menu): Screenshot_1631722353

Recorded baseline screenshot: ModalsTest_fixedSizeScreenshotCheck

Desktop (please complete the following information):

Target Android Device (please complete the following information):

DanielJette commented 2 years ago

@amanzan Thank you for the bug report.

Testify isn't currently able to capture the bottom sheet fragment on top of another fragment/activity since its internal capture mechanism is based on the view hierarchy. Basically, the bottom sheet isn't in the same view hierarchy, so it's not getting captured.

Supporting this case would be a nice addition to the next version of Testify and so I've tagged this to the 1.2 milestone.

As a workaround for testing bottom sheet fragments, I recommend that you write your test so that you focus on testing the contents of the fragment in isolation, instead of actually as being rendered in a bottom sheet. You can do this by loading the Fragment directly into a host activity and using Testify to capture that, or you can try using setScreenshotViewProvider to instruct Testify to capture a different view hierarchy.

e.g. https://github.com/Shopify/android-testify/blob/master/Sample/src/androidTest/java/com/shopify/testify/sample/ScreenshotRuleExampleTests.kt#L251-L267

amanzan commented 2 years ago

@DanielJette would it be possible to extend somehow ScreenshotRule, so that there's a new assertSame function which accepts currentBitmap as parameter? This way, I could take a screenshot of the entire screen and pass it

DanielJette commented 2 years ago

Hi @amanzan Unfortunately there's not currently a way to extend the ScreenshotRule in a meaningful way to allow for customization of the currentBitmap. However, there are plans to significantly expand the extensibility and customizability of the screenshot rule. This is tentatively scheduled to be included in the 1.2.0 release.

I have a work-in-progress branch here https://github.com/Shopify/android-testify/compare/227-extensible that demonstrates one way that Testify can allow for custom capture methods.

amanzan commented 2 years ago

Cool, looking forward to it 👌 Thanks!

jennatauro commented 2 years ago
Hi @DanielJette would this issue also be applicable to DropdownMenu from jetpack compose? I'm trying to write a screenshot test for the "expanded" (opened) version of the dropdown and the menu doesn't appear in the screenshot: expected actual
Screenshot_1644879440 DropdownScreenshotTest_expanded

I wasn't sure if this was due to a timing issue with when the screenshot is taken or if it's because the dropdown is rendered in a different view hierarchy.

DanielJette commented 2 years ago

@jennatauro I added a DropdownMenu test to the Sample app and I can confirm that I'm seeing the same error as you are.

Here is the Layout Inspector output:

image

It appears that the Dropdown is rendered in a different layer than the rest of the composition. I suspect this is likely implemented via a new Window by the compose renderer and this Window is not being captured.

You can find my changes to the Sample here https://github.com/Shopify/android-testify/pull/262

As of yet, I don't know how to resolve this issue, but it might be possible to direct Testify to capture the popup Window. Due to the limitations of Testify, it is not likely possible to capture the entire screen without also capturing the System UI. There is an issue requesting full-screen capture here.

amanzan commented 2 years ago

Hi @DanielJette, I see there is a 1.2.0-beta1 version available. Would this be possible to do in that version then? If so, would you mind guiding me how to approach it?

DanielJette commented 2 years ago

@amanzan Unfortunately the 1.2.0-beta1 version has not been published and there are no plans to publish this version to the Shopify repository. Future updates will be published under the dev.testify namespace (https://search.maven.org/artifact/dev.testify/plugin).

The full-screen functionality you are looking for will be published in the testify-fullscreen library which you can find here: https://github.com/ndtp/android-testify/tree/main/Ext/Fullscreen

We're still working through a lot of details regarding the move to the new repository, so stay tuned for updates. I would anticipate this library being published in Q3 2022.

DanielJette commented 2 years ago

Moved to https://github.com/ndtp/android-testify/issues/87