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.
Python code
19 linesimport 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
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
- Use `unittest.mock` to patch real screenshot functions in tests.
- 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
More from Automation & scripting
- Aggregate Log Errors Count by Hour in Python easy
- Automate Tweeting New Blog Posts in Python easy
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
Keep learning
Related tutorials and quizzes for this topic.