Skip to main content

AI-powered UI testing framework with natural language visual validation

Project description

LayoutLens: AI-Enabled UI Test System

Python 3.8+ License: MIT Build Status

Write visual UI tests using natural language to validate web layouts, accessibility compliance, and user interface consistency across devices. LayoutLens combines computer vision AI with automated screenshot testing to provide comprehensive UI validation.

🎯 Key Features

  • Natural Language Testing: Write UI tests in plain English
  • Multi-Viewport Testing: Automatically test responsive designs across devices
  • Accessibility Validation: Built-in WCAG compliance checking
  • Screenshot Comparison: Visual regression testing with AI-powered analysis
  • Form Validation Testing: Comprehensive form interaction and validation testing
  • CI/CD Integration: Easy integration with existing development workflows

📊 Benchmark Results

Our comprehensive benchmark suite demonstrates LayoutLens effectiveness across diverse UI patterns:

Test Suite Coverage

Benchmark File Description Size Elements Tested
ecommerce_product.html E-commerce product page 13.6KB Product galleries, pricing, responsive layout
dashboard.html Analytics dashboard 16.7KB Data grids, charts, complex layouts
contact_form.html Complex form validation 20.5KB Form elements, validation, accessibility
accessibility_showcase.html WCAG compliance demo 31.9KB Screen readers, keyboard nav, contrast
css_grid_showcase.html Advanced CSS layouts 27.6KB CSS Grid, Flexbox, responsive breakpoints
mobile_first_responsive.html Mobile-first design 32.1KB Progressive enhancement, touch interactions

Performance Results

Query Generation Performance:

  • 100% success rate across all benchmark files (4/4 files processed)
  • 246 total queries generated across benchmark suite
  • 61.5 queries average per file (range: 33-94 queries)
  • <100ms average generation time per page

AI Analysis Accuracy:

  • 100% OpenAI integration success rate (8/8 tests passed)
  • Real-time analysis of layout, accessibility, and semantic markup
  • Intelligent responses with specific reasoning for each query
  • Context-aware analysis using HTML structure and content

Content Structure Recognition:

📊 Verified Element Detection Results:
File                     | Size   | Forms | Images | Links | Headings | Inputs | Buttons | Queries
ecommerce_product.html   | 13.3KB |   0   |   5    |   5   |    3     |   1    |    2    |   34
contact_form.html        | 20.0KB |   1   |   0    |   2   |    1     |   13   |    1    |   33
accessibility_showcase   | 31.1KB |   1   |   1    |   7   |   10     |   10   |    5    |   85
css_grid_showcase.html   | 26.9KB |   0   |   0    |   31  |   16     |   0    |    2    |   94

Sample AI Analysis Results:

  • Semantic Markup: "No. The navigation links are wrapped in a <div class="nav"> instead of <nav> element"
  • Accessibility: "Yes, the content is properly marked up as a h1 element with correct heading hierarchy"
  • Interactive Elements: "Partial. HTML includes interactive elements with CSS focus styles defined"

Real-World Test Scenarios

✅ E-commerce Testing

  • Product image galleries and thumbnails
  • Pricing displays and discount calculations
  • Mobile-responsive product layouts
  • Add-to-cart functionality validation

✅ Dashboard Analytics

  • Complex data table structures
  • Chart and graph layout validation
  • Multi-column responsive grids
  • Interactive dashboard components

✅ Form Validation

  • Progressive form enhancement
  • Real-time validation feedback
  • Accessibility compliance (WCAG 2.1 AA)
  • Mobile-friendly form interactions

✅ Responsive Design

  • Mobile-first progressive enhancement
  • Breakpoint testing across 6+ screen sizes
  • Touch target size validation
  • Viewport meta tag optimization

Sample Test Queries Generated

Accessibility Tests:
  - "Are all form elements properly labeled and accessible?"
  - "Is the color contrast sufficient for readability?"
  - "Do all images have appropriate alt text?"

Layout Tests:  
  - "Is the page layout responsive across different screen sizes?"
  - "Are interactive elements easily clickable on mobile devices?"
  - "Is the heading hierarchy logical and well-structured?"

Visual Tests:
  - "Does the navigation menu collapse properly on mobile?"
  - "Are the product images displayed in the correct aspect ratio?"
  - "Is the form validation feedback clearly visible?"

🚀 Quick Start

Installation

pip install layoutlens
playwright install chromium  # Required for screenshots

Basic Usage

from layoutlens import LayoutLens

# Initialize the testing framework
tester = LayoutLens()

# Test a single page with auto-generated queries
result = tester.test_page(
    "homepage.html",
    viewports=["mobile_portrait", "desktop"],
    auto_generate_queries=True
)

print(f"Success rate: {result.success_rate:.2%}")
print(f"Tests passed: {result.passed_tests}/{result.total_tests}")

CLI Usage

# Test with automatic query generation
layoutlens test homepage.html --viewports mobile,desktop

# Test with custom queries
layoutlens test homepage.html --query "Is the navigation menu properly aligned?"

# Run full test suite
layoutlens suite tests/ui_tests.yaml

Advanced Features

# Compare two page versions
comparison = tester.compare_pages(
    "before_redesign.html",
    "after_redesign.html",
    query="Are the layouts visually consistent?"
)

# Create and run test suites
suite = tester.create_test_suite(
    name="Homepage Tests",
    description="Comprehensive homepage validation",
    test_cases=[
        {
            "name": "Mobile Homepage",
            "html_path": "homepage.html",
            "queries": ["Is the menu collapsed on mobile?"],
            "viewports": ["mobile_portrait"]
        }
    ]
)

results = tester.run_test_suite(suite)

🧪 Running Benchmarks

Test LayoutLens with our comprehensive benchmark suite:

# Clone the repository
git clone https://github.com/matmulai/layoutlens.git
cd layoutlens

# Set up environment
export OPENAI_API_KEY="your-key-here"
pip install -e .

# Run individual benchmarks
layoutlens test benchmarks/ecommerce_product.html
layoutlens test benchmarks/accessibility_showcase.html --viewports mobile,tablet,desktop

# Generate comprehensive benchmark report
python scripts/benchmark/run_full_evaluation.py

📋 Framework Architecture

The repository includes both legacy components and the modern LayoutLens framework:

Modern Framework (layoutlens/):

  • core.py: Enhanced LayoutLens class with user-friendly API
  • config.py: Comprehensive configuration management
  • cli.py: Command-line interface for easy integration

Testing Infrastructure (scripts/):

  • testing/page_tester.py: Main testing orchestrator
  • testing/screenshot_manager.py: Multi-viewport screenshot capture
  • testing/query_generator.py: Intelligent test query generation
  • benchmark/benchmark_generator.py: Automated benchmark data creation

Benchmark Suite (benchmarks/):

  • 6 comprehensive HTML test pages covering real-world scenarios
  • CSV datasets for batch testing and comparison
  • README with detailed testing guidelines

🔧 Configuration

LayoutLens supports flexible configuration via YAML files or environment variables:

# layoutlens_config.yaml
llm:
  model: "gpt-4o-mini"
  api_key: "${OPENAI_API_KEY}"

viewports:
  mobile_portrait:
    width: 375
    height: 667
    device_scale_factor: 2
    is_mobile: true
  
  desktop:
    width: 1920
    height: 1080
    device_scale_factor: 1
    is_mobile: false

testing:
  parallel_execution: true
  auto_generate_queries: true
  screenshot_format: "png"

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

# Clone and set up development environment
git clone https://github.com/matmulai/layoutlens.git
cd layoutlens
python -m venv venv
source venv/bin/activate
pip install -e .
pip install -r requirements-dev.txt

# Run tests
make test

# Run linting
make lint

# Run full development checks
make full-check

📄 License

LayoutLens is released under the MIT License.

🙏 Acknowledgments

📧 Support


LayoutLens: Making UI testing as simple as describing what you see.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

layoutlens-1.0.0.tar.gz (136.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

layoutlens-1.0.0-py3-none-any.whl (53.5 kB view details)

Uploaded Python 3

File details

Details for the file layoutlens-1.0.0.tar.gz.

File metadata

  • Download URL: layoutlens-1.0.0.tar.gz
  • Upload date:
  • Size: 136.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for layoutlens-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1184633e42e5de79eb8a73fde416b599b6b039f6359ec9ce5702503cec415728
MD5 bc8abe32502c7be94882ce9049fa1a1d
BLAKE2b-256 19f1a9072dfaa91a7f3190159cf22df5a91bda12e7b9f370295bd8c4dc7bb519

See more details on using hashes here.

File details

Details for the file layoutlens-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: layoutlens-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 53.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for layoutlens-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee816b0a276dd7fe9709269955b9e18b39e99c7fab18bd37f6dfc79d261deb77
MD5 4d5576d3f1fb948e18a2dc49ebdca999
BLAKE2b-256 c902e8fae74d57e2ae651d26b511ad89245c8ea5d9eab8097199f39d4a2b9672

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page