Intelligent screenshot comparison using Computer Vision and Machine Learning
Instead of simple pixel-by-pixel comparison, this tool uses SSIM (Structural Similarity Index Measure) - a computer vision algorithm that mimics how humans perceive visual differences.
if pixel[x,y] != pixel[x,y]: FAIL
β Too strict - fails on minor rendering differences
β Can't distinguish significant vs insignificant changes
β Breaks on anti-aliasing, font rendering, etc.
similarity = analyze_structure(img1, img2)
β
Understands visual patterns like humans
β
Ignores insignificant differences
β
Focuses on structural changes
β
Returns similarity score (0-100%)
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install packages
pip install -r requirements.txt
# Install browsers (tries all, installs what works)
playwright install chromium firefox webkitNote: The tool automatically uses whichever browser is available (Chromium β Firefox β WebKit), so you don't need all three!
# Test real websites (requires internet)
python example_usage.pyβ
Smart Browser Detection - Automatically uses Chromium, Firefox, or WebKit (whichever works)
β
AI-Powered Comparison - SSIM algorithm mimics human visual perception
β
Visual Diff Generation - Highlights exactly what changed
β
Baseline Management - Automatic baseline creation and updates
β
Organized File Structure - All screenshots in results/ folder
β
JSON Reports - Detailed test results with timestamps
β
Configurable Thresholds - Set custom similarity requirements
ai-visual-testing/
βββ visual_AI_tester.py # Main tool
βββ example_usage.py # Demo with external websites
βββ demo_pages/ # Simple demo using data URLs
βββ requirements.txt # Dependencies
βββ baselines/ # Baseline images (auto-created)
βββ results/ # Test results, diffs, and screenshots
βββ README.md # This file
from visual_ai_test import VisualAITester
# Initialize
tester = VisualAITester()
# Run test
result = tester.run_test(
url="https://example.com",
test_name="homepage",
threshold=0.95 # 95% similarity required
)
# Generate report
tester.generate_report()# Strict comparison (99% similar required)
tester.run_test(url="...", test_name="checkout", threshold=0.99)
# Lenient comparison (90% similar is okay)
tester.run_test(url="...", test_name="feed", threshold=0.90)test_cases = [
{"name": "homepage", "url": "https://example.com", "threshold": 0.95},
{"name": "about", "url": "https://example.com/about", "threshold": 0.95},
{"name": "contact", "url": "https://example.com/contact", "threshold": 0.95}
]
for test in test_cases:
tester.run_test(**test)
tester.generate_report()πΈ Capture screenshot
πΎ Save as baseline
π Status: BASELINE_CREATED
πΈ Capture new screenshot
π€ AI analyzes both images using SSIM
π Calculate similarity score (0-100%)
β
Pass if score β₯ threshold
β Fail if score < threshold
π¨ Generate visual diff (if failed)
π Find differences using computer vision
π― Detect significant changes (ignoring noise)
π¦ Draw red boxes around changes
πΌοΈ Create side-by-side comparison:
[Baseline] [Current] [Differences Highlighted]
SSIM = Structural Similarity Index Measure
The algorithm analyses three components:
- Luminance: How bright are the images?
- Contrast: How much variation is there?
- Structure: How similar are the patterns?
Similarity Scores:
1.00(100%): Perfect match0.99(99%): Virtually identical0.95(95%): Very similar (default threshold)0.90(90%): Similar but noticeable differences< 0.85(85%): Significant visual changes
Problem: net::ERR_ABORTED or browser crashes
Solution: The tool automatically tries multiple browsers. If all fail:
# Reinstall browsers
playwright install chromium firefox webkit
# Or install just one that works
playwright install firefoxProblem: Firewall/antivirus blocking
Solution:
- Temporarily disable firewall/antivirus
- Or use
headless=Falseto see browser window
Problem: pip install fails
Solution:
# Update pip
python -m pip install --upgrade pip
# Install with specific versions
pip install playwright==1.40.0{
"timestamp": "2025-10-22T12:00:00",
"total_tests": 3,
"passed": 2,
"failed": 1,
"baselines_created": 0,
"results": [
{
"test_name": "homepage",
"status": "PASSED",
"similarity": 0.9987,
"timestamp": "2025-10-22T12:00:01"
},
{
"test_name": "checkout",
"status": "FAILED",
"similarity": 0.8734,
"diff_image": "results/checkout_diff_20251022_120002.png",
"timestamp": "2025-10-22T12:00:02"
}
]
}Understanding SSIM:
Computer Vision Basics:
MIT License - feel free to use in your projects!
Emmanuel Kuye
Senior QA Engineer specializing in test automation and AI-powered testing
π§ Email: kuyeemmanuel@rocketmail.com
πΌ LinkedIn: emmanuel-kuye
π GitHub: emmanuel-qa
- scikit-image for SSIM implementation
- OpenCV for computer vision capabilities
- Playwright for browser automation
- The QA community for inspiration
- β Smart browser detection - Auto-fallback from Chromium β Firefox β WebKit
- β
Clean file structure - Screenshots now saved to
results/folder - β Better error messages - Clear guidance when browsers unavailable
- β Improved stability - Fixed browser crashes on macOS
- β Data URL support - Works completely offline with simple_demo.py
- Initial release with AI-powered visual regression testing