How to Create a Mock Headless Browser Screenshot Stub in Python

This code provides a deterministic stub that simulates capturing webpage screenshots with a headless browser, returning formatted output without real browser dependencies.

Easy Python 3.9+ Aug 9, 2026 Automation & scripting 13 views 0 copies

Python code

19 lines
Python 3.9+
import subprocess
import sys

def mock_screenshot_webpage(url: str, width: int = 1280, height: int = 800) -> str:
    """Stub that simulates taking a screenshot of a webpage using headless browser."""
    # In real implementation, you would use playwright/selenium/headless chrome
    result = {
        "url": url,
        "dimensions": f"{width}x{height}",
        "mode": "headless",
        "success": True,
        "file": f"screenshot_{url.replace('://', '_').replace('/', '_')}.png"
    }
    return f"Captured: {result['url']} | Size: {result['dimensions']} | Mode: {result['mode']} | Status: {'OK' if result['success'] else 'FAILED'} | File: {result['file']}"

if __name__ == "__main__":
    # Demonstrate usage with deterministic output
    print(mock_screenshot_webpage("https://example.com", 1920, 1080))
    print(mock_screenshot_webpage("https://docs.python.org", 1440, 900))

Output

stdout
Captured: https://example.com | Size: 1920x1080 | Mode: headless | Status: OK | File: screenshot_https_example.com.png
Captured: https://docs.python.org | Size: 1440x900 | Mode: headless | Status: OK | File: screenshot_https_docs.python.org.png

How it works

The mock_screenshot_webpage function uses a dictionary to represent the captured metadata, isolating the logic from real browser interactions. The subprocess and sys imports are placeholders for future integration with tools like Playwright or Selenium. Building the output string with f-strings makes the stub flexible and easy to adapt. Running the function under __main__ demonstrates deterministic behavior that can be tested without network access. This pattern is ideal for development, testing, and CI pipelines where real browser automation is costly or unavailable.

Common mistakes

  • Forgetting that this is a stub — real screenshots require Playwright or Selenium.
  • Over-engineering the stub with unnecessary I/O operations instead of returning deterministic strings.
  • Not sanitizing the URL properly when constructing filenames, leading to invalid filesystem paths.

Variations

  1. Use `unittest.mock` to patch real screenshot functions in tests.
  2. Return a dictionary directly instead of a formatted string for easier programmatic consumption.

Real-world use cases

  • Faking screenshot capture in unit tests to verify pipeline logic without launching a browser.
  • Generating placeholder file names and metadata for documentation or mock UI mockups.
  • Simulating headless screenshot behavior in a CI environment to test reporting workflows.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Automation & scripting

Related tutorials and quizzes for this topic.