Docs
Storybook Docs

视觉测试

Visual Tests 可捕获 UI 外观中的错误。它们的工作原理是截取每个故事的屏幕截图并将其与以前的版本进行比较以识别视觉变化。这非常适合验证布局、颜色、大小、对比度以及 UI 的任何其他视觉方面。

¥Visual tests catch bugs in UI appearance. They work by taking screenshots of every story and comparing them to previous versions to identify visual changes. This is ideal for verifying layout, color, size, contrast, and any other visual aspect of your UI.

Storybook 使用 Chromatic(Storybook 团队制作的云服务)原生支持跨浏览器视觉测试。当你启用可视化测试时,每个故事都会自动变成一个测试。这会直接在 Storybook 中为你提供有关 UI 错误的即时反馈。

¥Storybook supports cross-browser visual testing natively using Chromatic, a cloud service made by the Storybook team. When you enable visual testing, every story is automatically turned into a test. This gives you instant feedback on UI bugs directly in Storybook.

安装

¥Install the addon

通过安装 @chromatic-com/storybook(Storybook 维护者的官方插件)为你的项目添加视觉测试:

¥Add visual tests to your project by installing @chromatic-com/storybook, the official addon by Storybook maintainers:

npx storybook@latest add @chromatic-com/storybook

需要 Storybook 7.6 或更高版本。阅读 迁移指南 升级你的项目。

¥Storybook 7.6 or higher is required. Read the migration guide to upgrade your project.

启用可视化测试

¥Enable visual tests

当你启动 Storybook 时,你将看到一个新的 Visual Tests 插件面板,你可以在其中运行测试并查看结果。

¥When you start Storybook, you'll see a new addon panel for Visual Tests where you can run tests and view results.

Visual Tests addon enabled

要启用可视化测试,请注册 Chromatic 并创建一个项目。这将使你能够访问一组云浏览器。

¥To enable visual testing, sign up for Chromatic and create a project. This will give you access to a fleet of cloud browsers.

Visual Tests addon project selection

从你的项目列表中选择一个项目以完成设置。如果你是第一次设置插件,则会自动为你添加配置文件和必要的项目标识符。

¥Select a project from your project list to finish setup. If you're setting up the addon for the first time, the configuration files and necessary project identifiers will be added for you automatically.

配置

¥Configure

插件默认包含涵盖大多数用例的配置选项。你还可以通过 ./chromatic.config.json 文件微调插件配置以满足你的项目要求。以下是可用选项及其使用示例。

¥The addon includes configuration options covering most use cases by default. You can also fine-tune the addon configuration to match your project's requirements via the ./chromatic.config.json file. Below are the available options and examples of how to use them.

选项描述
projectId自动配置。设置项目标识符
"projectId": "Project:64cbcde96f99841e8b007d75" 的值
buildScriptName可选。定义自定义 Storybook 构建脚本
"buildScriptName": "deploy-storybook"
debug可选。将详细的调试信息输出到控制台。
"debug": true
zip可选。推荐用于大型项目。配置插件以将你的 Storybook 作为 zip 文件部署到 Chromatic。
"zip": true
// ./chromatic.config.json
{
  "buildScriptName": "deploy-storybook",
  "debug": true,
  "projectId": "Project:64cbcde96f99841e8b007d75",
  "zip": true
}

运行可视化测试

¥Run visual tests

单击 Storybook 侧栏中的 ▶️ 播放按钮以运行视觉测试。这会将你的故事发送到云端以拍摄快照并检测视觉变化。

¥Click the ▶️ Play button in the Storybook sidebar to run visual tests. This will send your stories to the cloud to take snapshots and detect visual changes.

Storybook running visual tests with the addon

审查变更

¥Review changes

如果你的故事中有视觉变化,它们将在侧边栏中高亮。单击故事并转到 Visual Tests 插件面板以查看哪些像素发生了变化。

¥If there are visual changes in your stories, they will be 🟡 highlighted in the sidebar. Click the story and go to the Visual Tests addon panel to see which pixels changed.

如果更改是故意的,✅ 在本地接受它们作为基线。如果更改不是故意的,请修复故事并使用 ▶️ 播放按钮重新运行测试。

¥If the changes are intentional, ✅ accept them as baselines locally. If the changes aren't intentional, fix the story and rerun the tests using the ▶️ Play button.

Confirm UI changes in Storybook

当你完成接受插件中的更改作为基线时,你就可以将代码推送到远程存储库了。这会将基线同步到云端,以供任何签出你的分支的人使用。

¥When you finish accepting changes as baselines in the addon, you're ready to push the code to your remote repository. This will sync baselines to the cloud for anyone who checks out your branch.

Accept UI changes in Storybook

使用 CI 自动化

¥Automate with CI

插件设计为与 CI 一起使用。我们建议使用插件在开发过程中检查更改,然后在准备合并时在 CI 中运行可视化测试。

¥The addon is designed to be used in tandem with CI. We recommend using the addon to check for changes during development and then running visual tests in CI as you get ready to merge.

你在插件中接受为基线的更改将自动在 CI 中被接受为基线,因此你无需重复检查。

¥Changes you accept as baselines in the addon will get auto-accepted as baselines in CI so you don’t have to review twice.

  1. 在你的 CI 工作流程中添加一个步骤以运行 Chromatic。

    ¥Add a step to your CI workflow to run Chromatic.

  2. 配置你的 CI 以包含环境变量,以便使用 Chromatic(项目令牌)进行身份验证。

    ¥Configure your CI to include environment variables to authenticate with Chromatic (project token).

PR 检查

¥PR checks

在 CI 中成功设置 Chromatic 后,你的拉取/合并请求将带有 UI 测试检查标记。徽章会通知你需要团队验证的测试错误或 UI 更改。在你的 Git 提供程序中进行必要的检查,以防止意外的 UI 错误被合并。

¥Once you successfully set up Chromatic in CI, your pull/merge requests will be badged with a UI Tests check. The badge notifies you of test errors or UI changes that need to be verified by your team. Make the check required in your Git provider to prevent accidental UI bugs from being merged.

PR badge for visual tests


可视化测试和快照测试有什么区别?

¥What’s the difference between visual tests and snapshot tests?

快照测试 将每个故事的渲染标记与已知基线进行比较。这意味着测试比较 HTML 块,而不是用户实际看到的内容。这反过来会导致误报增加,因为代码更改并不总是会导致组件中的视觉变化。

¥Snapshot tests compare the rendered markup of every story against known baselines. This means the test compares blobs of HTML and not what the user actually sees. Which in turn, can lead to an increase in false positives as code changes don’t always yield visual changes in the component.

Visual Tests 将每个故事的渲染像素与已知基线进行比较。因为你正在测试用户实际体验的相同内容,所以你的测试将更加丰富且易于维护。

¥Visual tests compare the rendered pixels of every story against known baselines. Because you're testing the same thing your users actually experience, your tests will be richer and easier to maintain.

了解其他 UI 测试

¥Learn about other UI tests