跳到正文
cccskills
免费在 GitHub 公开

snapshot

Render SwiftUI view snapshots for design comparison and visual iteration. Use for layout, styling, or state variants; use verify for navigation and live app behavior.

中文简介正在准备中。当前显示原文说明。

查看安装方法

包含的文件(1)

  • SKILL.md7.7 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

Render View Snapshots

Use swift-snapshot-testing to create a visual feedback loop when implementing UI changes. Render SwiftUI views to PNG after each code change, compare against design goals, fix mismatches, and repeat.

When to Use

  • Implementing UI from Figma designs or reference screenshots
  • Multi-step view redesigns where each step should match a target
  • Any SwiftUI work where you need to visually verify output
  • When design reference images are available for comparison

When NOT to Use

  • Logic-only changes with no visual impact
  • For an already-covered view, extend its existing snapshot tests instead of adding a parallel fixture

Core Flow

1. SETUP      Reuse existing snapshot coverage and dependencies; add temporary setup if needed
2. GOAL       Collect design reference images
3. SCAFFOLD   Create snapshot test rendering the target view
4. LOOP       For each implementation step:
                a. Edit code
                b. Run snapshot test (record mode)
                c. Read generated PNG
                d. Compare against design
                e. If mismatch -> fix and re-snapshot
                f. If match -> retain evidence and proceed to the next state
5. CLEANUP    Remove only task-owned temporary setup; retain reviewed evidence

Setup: Add Dependency

Check existing tests, Modules/Package.swift, and the resolved dependency version first. Reuse compatible setup. If absent, add a toolchain-compatible release temporarily to the owning test target.

In the dependencies array of Modules/Package.swift:

.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.19.6"),

In the relevant test target's dependencies array:

.product(name: "SnapshotTesting", package: "swift-snapshot-testing"),

Select the test target that can import the view: for example, PointOfSaleTests for POS or WooCommerceTests for app-target views. Confirm its dependencies in the package or project.

Remember the original state of Modules/Package.swift so you can revert exactly after cleanup.

Goal: Collect References

Choose a task artifact directory:

mkdir -p .agent-artifacts
TASK_ARTIFACTS="$(mktemp -d "$PWD/.agent-artifacts/snapshot.XXXXXX")"
printf 'Task artifacts: %s\n' "$TASK_ARTIFACTS"

Use this directory's absolute path for <task-artifacts> below, including in later shell calls. If the user provides design reference images, copy them into this directory. Read each reference image to understand the target state before writing code.

If no reference image, the goal is inferred from the user's description of desired UI behavior.

Scaffold: Create Snapshot Test

Create a test file in the appropriate Modules/Tests/*Tests/ directory:

import Testing
import SnapshotTesting
import SwiftUI
@testable import TargetModule

@MainActor
struct ViewNameSnapshotTests {
    @Test
    func test_default_state_snapshot() {
        // Given: use the required viewport and representative dependencies.
        let view = YourView(/* props for default state */)
        // When: choose a fresh directory for each iteration.
        let diagnostic = verifySnapshot(
            of: view, as: .image(layout: .fixed(width: 390, height: 844)),
            record: .all, snapshotDirectory: "<task-artifacts>/iteration-1"
        )
        // Then: preserve the library's recording or failure diagnostic.
        if let diagnostic { Issue.record("\(diagnostic)") }
    }
}

Important details for this project:

  • Use the resolved library's public API. verifySnapshot accepts snapshotDirectory; assertSnapshot does not.
  • Match the requested device viewport, size class, theme, text size, and view state. The dimensions above are illustrative.
  • Add fixtures for required alternate states. Increasing image height does not prove the content fits on the device.
  • Record mode writes images and reports a diagnostic. It is capture evidence, not a passing regression comparison.
  • Follow the owning view's design system and repository styling rules. Use /verify for navigation, live data, and hardware flows.

Run: Execute Snapshot Tests

Use the /simulator skill approach to discover a booted simulator UDID.

Run only the snapshot tests for fast feedback. Select the module scheme when it owns the fixture; use WooCommerce for app-target views:

set -o pipefail
TEST_RUNNER_SNAPSHOT_ARTIFACTS='<task-artifacts>/mismatch' \
xcodebuild test \
  -workspace WooCommerce.xcworkspace \
  -scheme '<scheme>' \
  -derivedDataPath "$PWD/DerivedData" \
  -destination 'platform=iOS Simulator,id=<SIMULATOR_UDID>' \
  -only-testing:'<TestTarget>/<SnapshotTestClass>' \
  CODE_SIGNING_ALLOWED=NO 2>&1 | tee '<task-artifacts>/snapshot.log'

Prefer module-level test targets when they own the view and its dependencies.

Check the pipeline exit status and full log. Record mode can fail after writing a PNG; confirm that the expected new image exists and inspect it. Other failures require diagnosis. Use the new iteration directory and image paths reported by the library. An older PNG is not evidence of the current render. Keep one log per iteration and report unrelated build or test failures.

For a regression comparison, keep the recorded snapshotDirectory and change record: .all to record: .never. Run the unchanged view first, then any intentional mismatch. Use a separate log filename for each run; comparison failures must retain their images under the task directory.

Compare: Read and Assess

After each run:

  1. Read the generated PNG using the Read tool
  2. If a design reference exists, read it side by side
  3. Identify specific mismatches: layout, spacing, missing elements, text wrapping, colors
  4. Copy PNGs to working directory with step labels for history:
cp '<reported-PNG-path>' '<task-artifacts>/step1.png'

Fix: Iterate on Mismatches

Common issues caught by snapshots:

  • Text wrapping — compare long and localized text at the required widths before changing layout
  • Navigation controls — render the intended size class and navigation context
  • Content clipped — verify scrolling and safe areas at the real viewport
  • Spacing mismatches — check the owning design tokens against the reference
  • Wrong section order — compare VStack children order with design

When fixing: edit the code and re-run snapshots to verify. Complete the loop when each requested state has a new, inspected image. Report the states reviewed and remaining runtime gaps.

Cleanup: Remove Temporary Setup

Restore only dependency and lockfile edits introduced by this task, and remove only its temporary fixture and generated source-tree images. Preserve pre-existing edits, tests, dependencies, and snapshot baselines. Keep reviewed captures in the task artifact directory for the required retention period. Inspect git diff and git status before committing; task artifacts stay out of commits.

Quick Reference

ActionCommand/Tool
Add dependencyEdit Modules/Package.swift
Run snapshotsxcodebuild test -only-testing:<Target>/<Class>
Find PNGsPaths reported by the library under the selected iteration directory
View snapshotRead tool on the PNG path
CompareRead both snapshot PNG and design reference
Fix + re-runEdit code, re-run xcodebuild, commit once verified
Clean upRestore only task-owned setup; retain reviewed evidence

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

Log in to a test store on an iOS simulator. Use live-store DEBUG login by default, WireMock when mocks are requested, and the specified store for "my site".

中文简介正在准备中。当前显示原文说明。

woocommerce/woocommerce-ios3582026年10月22日 更新

bootstrap

免费

Install dependencies and build tools for WooCommerce iOS

中文简介正在准备中。当前显示原文说明。

woocommerce/woocommerce-ios3582026年10月22日 更新

debug

免费

Debug a failing test or build error in WooCommerce iOS

中文简介正在准备中。当前显示原文说明。

woocommerce/woocommerce-ios3582026年10月22日 更新

lint

免费

Run SwiftLint on the WooCommerce iOS project

中文简介正在准备中。当前显示原文说明。

woocommerce/woocommerce-ios3582026年10月22日 更新

mocks

免费

Start or stop the WireMock API mock server for UI testing and E2E verification.

中文简介正在准备中。当前显示原文说明。

woocommerce/woocommerce-ios3582026年10月22日 更新

pr

免费

Create a pull request following WooCommerce iOS conventions

中文简介正在准备中。当前显示原文说明。

woocommerce/woocommerce-ios3582026年10月22日 更新

查看 woocommerce 的全部技能

报告此技能的问题