Skip to content

feat: (experimental) add screenshot testing commands for React Native Storybook #760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 15 commits into
base: next
Choose a base branch
from

Conversation

dannyhw
Copy link
Member

@dannyhw dannyhw commented Jun 20, 2025

Summary

Experimental screenshot testing package for React Native Storybook with high-performance visual regression testing and native ignore regions support.

image

What's Included (Experimental)

Screenshot Comparison

  • usesodiff-bin for fast image comparison

📊 Basic Visual Regression Testing Workflow

  • Generate Maestro tests from stories, capture screenshots, compare with baselines
  • Simple HTML reports with side-by-side comparison and diff visualization
  • Configurable tolerance levels and comparison modes
  • Support for common image formats (PNG, JPG, JPEG)

🎯 Ignore Regions

  • --ignore-regions "x,y,w,h" flag to skip system UI areas that cause false positives
  • Prototype native region exclusion without temporary file creation
  • detect-ignore-regions helper command to find coordinates from diff images
  • Support for multiple regions (semicolon-separated)

Commands (Experimental)

# Main workflow - screenshot generation and comparison
screenshot-stories [options]

# Standalone comparison of existing screenshots  
compare-screenshots [options]

# Generate Maestro test files from stories
gen-maestro [options]

# Interactive tool to find ignore region coordinates
detect-ignore-regions [options]

Usage Examples

# Set up initial baselines
npx @storybook/react-native-test screenshot-stories --update-baseline

# Run basic visual regression tests
npx @storybook/react-native-test screenshot-stories --html-report

# Experiment with ignore regions for system UI
npx @storybook/react-native-test detect-ignore-regions
npx @storybook/react-native-test screenshot-stories --ignore-regions "376,2512,428,24"

# Skip test execution, just compare existing screenshots
npx @storybook/react-native-test screenshot-stories --skip-test --html-report

Notes & Limitations

This is an experimental package to explore screenshot testing workflows for React Native Storybook:

  • Performance: Early results are promising but need broader testing across different environments
  • API Stability: Command-line interface may change based on feedback
  • Platform Support: Currently tested primarily on macOS, needs validation on other platforms
  • Edge Cases: Some comparison scenarios may need refinement
  • Integration: Maestro dependency and workflow may need adjustment

Feedback Needed

  • Performance: Does the speed improvement hold across different project sizes?
  • API Design: Are the command-line options intuitive and complete?
  • Ignore Regions: Is the coordinate-based approach user-friendly enough?
  • Integration: How well does this fit into existing CI/CD workflows?
  • Error Handling: Are error messages and edge cases handled appropriately?

Test Plan

  • Basic functionality: Commands run without errors and produce expected outputs
  • Ignore regions: Actually prevent false positives from system UI elements
  • HTML reports: Load correctly and navigation works as expected
  • File handling: Baselines, diffs, and cleanup work correctly
  • Performance: Significant speed improvement verified in test environment
  • Accuracy: Comparison results identical to previous implementation
  • Cross-platform: Needs testing on Linux and Windows
  • Scale testing: Validation with larger screenshot sets
  • CI integration: Testing in automated environments

This adds comprehensive screenshot testing capabilities to @storybook/react-native-test.

## New Commands

- `screenshot-stories`: Complete workflow to generate, run tests, and compare screenshots
- `gen-maestro`: Generate Maestro test files from Storybook stories
- `compare-screenshots`: Compare screenshots against baselines with diff generation

## Key Features

- Automated Maestro test generation from story index
- Visual regression testing with configurable tolerance
- Baseline management with update capabilities
- Comprehensive CLI options for customization
- Reusable utility functions for screenshot comparison
- Support for skipping individual workflow steps

## Workflow

1. Generate Maestro tests from stories
2. Run tests to capture screenshots
3. Compare against baseline images
4. Generate diff images for failures
5. Update baselines when changes are intentional

This enables automated visual testing for React Native Storybook components.
@dannyhw dannyhw changed the title feat: add screenshot testing commands for React Native Storybook feat: experimental add screenshot testing commands for React Native Storybook Jun 20, 2025
@dannyhw dannyhw changed the title feat: experimental add screenshot testing commands for React Native Storybook feat: (experimental) add screenshot testing commands for React Native Storybook Jun 20, 2025
@dannyhw dannyhw marked this pull request as draft June 20, 2025 23:50
dannyhw added 12 commits June 21, 2025 00:50
- Add modern Storybook-inspired design with CSS custom properties
- Implement full-height image display utilizing entire viewport
- Create unified header combining all controls (filters, diff toggle, navigation)
- Enable diff images to show by default for immediate comparison
- Add pagination with keyboard navigation (arrows, spacebar)
- Implement filtering by status (all, differences, missing baselines)
- Support toggling diff images on/off
- Add mobile responsive design
- Include monospace filename display and proper status badges
- Utilize Storybook color palette and typography system
- Remove diff image toggle - always show diffs when available
- Default filter to "Differences Only" for faster workflow
- Update hover states to use subtle background colors instead of border changes
- Unify navigation button hover color to match filter buttons (blue)
- Remove unused toggle functionality from JavaScript
- Streamline controls for cleaner, more focused interface
- Replace iOS home indicator detection with flexible custom ignore regions
- Add --ignore-regions flag supporting "x,y,w,h;x2,y2,w2,h2" format
- Implement detect-ignore-regions command for interactive diff analysis
- Add optimized two-pass comparison (fast normal, then masked on failure)
- Generate preview images showing ignore regions highlighted in red
- Support #F0F magenta diff pixel detection for accurate region extraction

Solves visual regression testing issues with system UI differences by
giving users complete control over ignored areas.
- Replace looks-same library with odiff-bin for dramatically improved performance
- Eliminate PNG masking overhead by using odiff's native ignoreRegions support
- Convert ignore region format (x,y,width,height → x1,y1,x2,y2) for odiff compatibility
- Remove expensive PNG read/modify/write/cleanup workflow
- Maintain identical comparison accuracy and functionality
- Reduce comparison time from ~15s to ~2s for 93 screenshots

Performance improvements:
- Native ignore regions eliminate temporary file creation
- 6-7x faster image comparison engine (OCaml-based)
- Parallel processing without masking bottlenecks
- Memory efficient comparison workflow
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.

1 participant