Skip to content

Conversation

@jeslat
Copy link
Contributor

@jeslat jeslat commented May 28, 2025

🥅 What's the goal?

Add support for screenshot testing

🚧 How do we do it?

  • Add new ScreenshotsRule test rule to capture and check screenshots.
  • Write the files with the extension included to avoid problems on the plugin side.
  • Add new annotation to ignore screenshots checks.

🧪 How can I test this?

  • I've added a new test in the sample project testing the activity UI
  • Diff image example when a test fails:

com telefonica loggerazzi ExampleInstrumentedTest_launch_activity


@Test
@IgnoreLogs
fun testLaunchActivity() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New test to check a basic screenshot

Comment on lines -150 to +151
lastFile?.renameTo(File(this, "$key.txt"))

lastFile?.renameTo(File(this, key))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm including the extension in the original file so no need to include it here

}

kotlin {
explicitApi()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to force the classes to define their visibility.

@@ -0,0 +1,10 @@
package com.telefonica.loggerazzi
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created a new annotation to ignore the screenshots checks and rename the logs one

@@ -0,0 +1,33 @@
package com.telefonica.loggerazzi
Copy link
Contributor Author

@jeslat jeslat Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy from dropshots, it allows you to customize the threshold

@@ -0,0 +1,72 @@
package com.telefonica.loggerazzi
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy from dropshots, it generates the diff image file

Comment on lines +56 to +78
@RequiresApi(Build.VERSION_CODES.O)
public fun compareScreenshot(
rule: ComposeTestRule,
name: String?,
) {
rule.waitForIdle()
val bitmap = rule.onRoot().captureToImage().asAndroidBitmap()
compareScreenshot(bitmap, name)
}

public fun compareScreenshot(
activity: Activity,
name: String?,
) {
val bitmap = Screenshot.capture(activity).bitmap
compareScreenshot(bitmap, name)
}

@Suppress("MemberVisibilityCanBePrivate")
public fun compareScreenshot(
bitmap: Bitmap,
name: String?
) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've copied the same API that Shot has

Comment on lines +83 to +87
if (InstrumentationRegistry.getArguments().getString("record") != "true" && !isTestIgnored) {
val goldenBitmap = getGoldenBitmap(resourceName)
compareImagesSize(bitmap, goldenBitmap, fileName, name)
compareImages(bitmap, goldenBitmap, fileName, resourceName)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are not recording the baseline or this test is ignored, we compare the screenshot.

@jeslat jeslat marked this pull request as ready for review June 5, 2025 07:45
@jeslat jeslat requested review from dpastor and yamal-alm June 5, 2025 07:45
Copy link

@yamal-alm yamal-alm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good 👍

Comment on lines +38 to +40
private val loggerazziDir = File(downloadDir, "loggerazzi-logs/${context.packageName}")
private val failuresDir = File(loggerazziDir, "failures")
private val recordedDir = File(loggerazziDir, "recorded")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I though we was going to stop using loggerazzi. I'm seeing it in many places (package, class names, etc), is there a renaming task in the roadmap or aren't we going to stop using that name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the rename is pending, I will do it after this

Copy link
Contributor

@dpastor dpastor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Probably in the renaming task we can change things like "LoggerazziRule" to "LogsRule", in general to make all naming more consistent with the "generic" or "multiple" snapshoting idea of the library, but everything else looks ok!

@jeslat jeslat merged commit 15abeac into main Jun 6, 2025
1 check passed
@jeslat jeslat deleted the compare-screenshots branch June 6, 2025 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants