roborazzi

roborazzi

JVM Android 集成测试的可视化解决方案

Roborazzi 是专为 JVM Android 集成测试开发的截图工具,支持 Robolectric Native Graphics (RNG)。该工具与 Robolectric 无缝集成,使测试能在 Hilt 环境中运行并与组件交互。通过捕获 Robolectric 的截图,Roborazzi 提供高效可靠的测试流程,解决了 Paparazzi 与 Robolectric 的兼容性问题。此外,Roborazzi 可轻松集成到 GitHub Actions 中,实现自动化截图比较和验证。

Roborazzi截图测试JVM测试Android开发RobolectricGithub开源项目
<!-- Generated by ./gradlew generateReadme. Do not edit this file. --> <div name="topic_top"> <!-- Generated by docs/topics/top.md. Do not edit this file. -->

Roborazzi

Make JVM Android Integration Test Visible

Roborazzi now supports Robolectric Native Graphics (RNG) and enables screenshot testing.📣

Why Choose Roborazzi?

Why is screenshot testing important?

Screenshot testing is key to validate your app's appearance and functionality. It efficiently detects visual issues and tests the app as users would use it, making it easier to spot problems. It's quicker than writing many assert statements, ensuring your app looks right and behaves correctly.

What are JVM tests and why test with JVM instead of on Android?

JVM tests, also known as local tests, are placed in the test/ directory and are run on a developer's PC or CI environment. On the other hand, device tests, also known as Instrumentation tests, are written in the androidTest/ directory and are run on real devices or emulators. Device testing can result in frequent failures due to the device environment, leading to false negatives. These failures are often hard to reproduce, making them tough to resolve.

Paparazzi and Roborazzi: A Comparison

Paparazzi is a great tool for visualizing displays within the JVM. However, it's incompatible with Robolectric, which also mocks the Android framework.

Roborazzi fills this gap. It integrates with Robolectric, allowing tests to run with Hilt and interact with components. Essentially, Roborazzi enhances Paparazzi's capabilities, providing a more efficient and reliable testing process by capturing screenshots with Robolectric.

Leveraging Roborazzi in Test Architecture: An Example

<img src="https://github.com/takahirom/roborazzi/assets/1386930/937a96a4-f637-4029-87e1-c1bb94abc8ae" width="320" />

Integrating Roborazzi into the Architecture: An Example from DroidKaigi 2023 App

In the DroidKaigi 2023 app, Roborazzi was introduced from the early stages of development as part of the architectural design. This integration allowed the team to verify changes throughout the development process. The specific architectural decisions and how they were implemented can be found README.

</div> <div name="topic_try_it_out"> <!-- Generated by docs/topics/try_it_out.md. Do not edit this file. -->

Try it out

Available on Maven Central.

Add Robolectric

This is an example of adding Robolectric to your project: https://github.com/takahirom/roborazzi-usage-examples/compare/b697...5c12

This library is dependent on Robolectric. Please see below to add Robolectric.

https://robolectric.org/getting-started/

Add Roborazzi

This is an example of adding Roborazzi to your project: https://github.com/takahirom/roborazzi-usage-examples/commit/3a02

To take screenshots, please use Robolectric 4.10 alpha 1 or later and please add @GraphicsMode(GraphicsMode.Mode.NATIVE) to your test class.

@GraphicsMode(GraphicsMode.Mode.NATIVE)
</div> <div name="topic_build_setup"> <!-- Generated by docs/topics/build_setup.md. Do not edit this file. -->

Build setup

Roborazzi is available on maven central.

This plugin simply creates Gradle tasks record, verify, compare and passes the configuration to the test.

build.gradle.kts

<table> <tr><td>plugins</td><td>buildscript</td></tr> <tr><td>

Define plugin in root build.gradle.kts

plugins { ... id("io.github.takahirom.roborazzi") version "[version]" apply false }

Apply plugin in module build.gradle.kts

plugins { ... id("io.github.takahirom.roborazzi") }
</td><td>

root build.gradle.kts

buildscript { dependencies { ... classpath("io.github.takahirom.roborazzi:roborazzi-gradle-plugin:[version]") } }

module build.gradle.kts

plugins { ... id("io.github.takahirom.roborazzi") }
</td></tr> </table> <details> <summary>build.gradle version</summary> <table> <tr><td>plugins</td><td>buildscript</td></tr> <tr><td>

Define plugin in root build.gradle

plugins { ... id "io.github.takahirom.roborazzi" version "[version]" apply false }

Apply plugin in module build.gradle

plugins { ... id 'io.github.takahirom.roborazzi' }
</td><td>

root build.gradle

buildscript { dependencies { ... classpath "io.github.takahirom.roborazzi:roborazzi-gradle-plugin:[version]" } }

module build.gradle

apply plugin: "io.github.takahirom.roborazzi"
</td></tr> </table> </details> <table> <tr> <td> Use Roborazzi task </td> <td> Use default unit test task </td> <td> Description </td> </tr> <tr> <td>

./gradlew recordRoborazziDebug

</td><td>

./gradlew testDebugUnitTest after adding roborazzi.test.record=true to your gradle.properties file.

or

./gradlew testDebugUnitTest -Proborazzi.test.record=true

</td><td>

Record a screenshot
Default output directory is build/outputs/roborazzi
You can check a report under build/reports/roborazzi/index.html

</td> </tr> <tr> <td>

./gradlew compareRoborazziDebug

</td><td>

./gradlew testDebugUnitTest after adding roborazzi.test.compare=true to your gradle.properties file.

or

./gradlew testDebugUnitTest -Proborazzi.test.compare=true

</td><td>

Review changes made to an image. This action will compare the current image with the saved one, generating a comparison image labeled as [original]_compare.png. It also produces a JSON file containing the diff information, which can be found under build/test-results/roborazzi.

</td> </tr> <tr> <td>

./gradlew verifyRoborazziDebug

</td><td>

./gradlew testDebugUnitTest after adding roborazzi.test.verify=true to your gradle.properties file.

or

./gradlew testDebugUnitTest -Proborazzi.test.verify=true

</td><td>

Validate changes made to an image. If there is any difference between the current image and the saved one, the test will fail.

</td> </tr> <tr> <td>

./gradlew verifyAndRecordRoborazziDebug

</td><td>

./gradlew testDebugUnitTest after adding roborazzi.test.verify=true and roborazzi.test.record=true to your gradle.properties file.

or

./gradlew testDebugUnitTest -Proborazzi.test.verify=true -Proborazzi.test.record=true

</td><td>

This task will first verify the images and, if differences are detected, it will record a new baseline.

</td> </tr> <tr> <td>

./gradlew clearRoborazziDebug

</td><td>

This is not a test task.

</td><td>

Note: This is an experimental task. This task will clear the saved images. This task also deletes the cached images. Please be careful when using this task.

</td> </tr> </table>

The comparison image, saved as [original]_compare.png, is shown below:

image

You can check the test report in build/reports/roborazzi/index.html

<img width="400" alt="image" src="https://github.com/takahirom/roborazzi/assets/1386930/7834a436-1927-438d-8656-61f583ae3f48" />

This uses JetNew from Compose Samples. You can check the pull request introducing Roborazzi to the compose-samples here.

Add dependencies

DescriptionDependencies
Core functionstestImplementation("io.github.takahirom.roborazzi:roborazzi:[version]")
Jetpack ComposetestImplementation("io.github.takahirom.roborazzi:roborazzi-compose:[version]")
JUnit rulestestImplementation("io.github.takahirom.roborazzi:roborazzi-junit-rule:[version]")
</div> <div name="topic_how_to_use"> <!-- Generated by docs/topics/how_to_use.md. Do not edit this file. -->

How to use

Take a screenshot manually

You can take a screenshot by calling captureRoboImage().

app/src/test/java/../ManualTest.kt

import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.test.core.app.ActivityScenario import androidx.test.espresso.Espresso.onView import androidx.test.ext.junit.runners.AndroidJUnit4 import org.junit.Test import org.junit.runner.RunWith import org.robolectric.annotation.GraphicsMode // All you need to do is use the captureRoboImage function in the test! import com.github.takahirom.roborazzi.captureRoboImage // Tips: You can use Robolectric while using AndroidJUnit4 @RunWith(AndroidJUnit4::class) // Enable Robolectric Native Graphics (RNG) @GraphicsMode(GraphicsMode.Mode.NATIVE) class ManualTest { @get:Rule val composeTestRule = createAndroidComposeRule<MainActivity>() @Test fun captureRoboImageSample() { // Tips: You can use Robolectric with Espresso API // launch ActivityScenario.launch(MainActivity::class.java) // Capture screen onView(ViewMatchers.isRoot()) // If you don't specify a screenshot file name, Roborazzi will automatically use the method name as the file name for you. // The format of the file name will be as follows: // build/outputs/roborazzi/com_..._ManualTest_captureRoboImageSample.png .captureRoboImage() // Capture Jetpack Compose Node composeTestRule.onNodeWithTag("AddBoxButton") .onParent() .captureRoboImage("build/compose.png") } }

Roborazzi supports the following APIs.

<table> <tr><td>Capture</td><td>Code</td></tr> <tr><td> ✅ Jetpack Compose's onNode() </td><td>
composeTestRule.onNodeWithTag("AddBoxButton") .captureRoboImage()
</td></tr> <tr><td> ✅ Espresso's onView() </td><td>
onView(ViewMatchers.isRoot()) .captureRoboImage()
onView(withId(R.id.button_first)) .captureRoboImage()
</td></tr> <tr><td> ✅ View </td><td>
val view: View = composeTestRule.activity.findViewById<View>(R.id.button_second) view.captureRoboImage()
</td></tr> <tr><td> ✅ Jetpack Compose lambda </td><td>
captureRoboImage() { Text("Hello Compose!") }
</td></tr> <tr><td> Experimental🧪<br/> ✅ Captures the entire screen, including dialogs </td><td>
captureScreenRoboImage()
</td></tr> <tr><td> ✅ Bitmap </td><td>
val bitmap: Bitmap = createBitmap(100, 100, Bitmap.Config.ARGB_8888) .apply { applyCanvas { drawColor(android.graphics.Color.YELLOW) } } bitmap.captureRoboImage()
</td></tr> </table>

Device configuration

You can configure the device by using the @Config annotation and RobolectricDeviceQualifiers.

<table> <tr><td>Configuration</td><td>Code</td></tr> <tr><td> ✅ Predefined device configuration </td><td>

You can change the device configuration by adding @Config to the class or method.

@RunWith(AndroidJUnit4::class) @GraphicsMode(GraphicsMode.Mode.NATIVE) @Config(qualifiers = RobolectricDeviceQualifiers.Pixel5) class RoborazziTest {
@Test @Config(qualifiers = RobolectricDeviceQualifiers.Pixel5) fun test() {
</td></tr> <tr><td> ✅ Night mode </td><td>
@Config(qualifiers = "+night")
</td></tr> <tr><td> ✅ Locale </td><td>
@Config(qualifiers = "+ja")
</td></tr> <tr><td> ✅ Screen size </td><td>
@Config(qualifiers = RobolectricDeviceQualifiers.MediumTablet)
</td></tr> </table>

Integrate to your GitHub Actions

It is easy to integrate Roborazzi to your GitHub Actions.

Add a job to store screenshots

name: store screenshots on: push env: GRADLE_OPTS: "-Dorg.gradle.jvmargs=-Xmx6g -Dorg.gradle.daemon=false -Dkotlin.incremental=false" jobs: test: runs-on: macos-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-java@v3.9.0 with: distribution: 'zulu' java-version: 19 - name: Gradle cache uses: gradle/gradle-build-action@v2 - name: test run: | # Create screenshots ./gradlew app:recordRoborazziDebug --stacktrace # Upload screenshots to GitHub Actions Artifacts - uses: actions/upload-artifact@v3 with: name: screenshots path: app/build/outputs/roborazzi retention-days: 30

Add a job to verify screenshots

name: verify test on: push env: GRADLE_OPTS: "-Dorg.gradle.jvmargs=-Xmx6g -Dorg.gradle.daemon=false -Dkotlin.incremental=false" jobs: test: runs-on: macos-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-java@v3.9.0 with: distribution: 'zulu' java-version: 19 - name: Gradle cache uses: gradle/gradle-build-action@v2 # Download screenshots from main branch - uses: dawidd6/action-download-artifact@v2 with: name: screenshots path: app/build/outputs/roborazzi workflow: test.yaml branch: main - name: verify test id: verify-test run: | # If there is a difference between the screenshots, the test will fail. ./gradlew app:verifyRoborazziDebug --stacktrace - uses: actions/upload-artifact@v3 if: ${{ always() }} with: name: screenshot-diff path: app/build/outputs/roborazzi retention-days: 30 - uses: actions/upload-artifact@v3 if: ${{ always() }} with: name: screenshot-diff-reports path: app/build/reports retention-days: 30 - uses: actions/upload-artifact@v3 if: ${{ always() }} with: name: screenshot-diff-test-results path: app/build/test-results retention-days: 30

Advanced workflow Sample: Compare Snapshot Results on Pull Requests

For those who are looking for a more advanced example, we have prepared a sample repository that demonstrates how to use Roborazzi to compare snapshot results on GitHub pull requests. This sample showcases the integration of Roborazzi with GitHub Actions workflows, making it easy to visualize and review the differences between snapshots directly in the pull request comments.

Check out the roborazzi-compare-on-github-comment-sample repository to see this powerful feature in action and learn how to implement it in your own projects.

Example of the comment

<img src="https://user-images.githubusercontent.com/1386930/236480693-80483cde-53fe-4c04-ba1f-2352e14b5f15.png" width="600" />

RoborazziRule (Optional)

RoborazziRule is a JUnit rule for Roborazzi. RoborazziRule is optional. You can use captureRoboImage() without this rule.

RoborazziRule has two features.

  1. Provide context such as RoborazziOptions and outputDirectoryPath etc for captureRoboImage().
  2. Capture screenshots for each test when specifying RoborazziRule.options.captureType.

For example, The following

编辑推荐精选

Keevx

Keevx

AI数字人视频创作平台

Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。

即梦AI

即梦AI

一站式AI创作平台

提供 AI 驱动的图片、视频生成及数字人等功能,助力创意创作

扣子-AI办公

扣子-AI办公

AI办公助手,复杂任务高效处理

AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!

TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

热门AI工具生产力协作转型TraeAI IDE
蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI助手AI工具AI写作工具AI辅助写作蛙蛙写作学术助手办公助手营销助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

聊天机器人AI助手热门AI工具AI对话
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

热门AI工具AI办公办公工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

模型训练热门AI工具内容创作智能问答AI开发讯飞星火大模型多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

下拉加载更多