Playwright Advanced Features – Screenshot, Video Recording & Network Interception
Playwright provides many powerful features that help testers debug issues, capture application behavior, and monitor network traffic. These advanced capabilities make Playwright a very powerful automation framework.
In this tutorial, we will learn how to capture screenshots, record test videos, and intercept network requests using Playwright with Python.
Taking Screenshot in Playwright
Screenshots are extremely useful for debugging failed test cases and documenting test results. Playwright allows capturing screenshots of the entire page or specific elements.
Capture Full Page Screenshot
page.screenshot(path="homepage.png")
This command captures the current browser page and saves the image as homepage.png.
Capture Screenshot of Specific Element
page.locator("#login-form").screenshot(path="login_form.png")
This captures a screenshot of the login form element only.
Video Recording in Playwright
Playwright allows recording test execution videos which are very useful for debugging automation failures.
Video recording can be enabled while creating a browser context.
browser = p.chromium.launch()
context = browser.new_context(
record_video_dir="videos/"
)
page = context.new_page()
page.goto("https://example.com")
This configuration automatically records the test execution and saves the video files inside the videos directory.
Network Interception
Modern web applications communicate with backend servers through APIs. Playwright allows testers to intercept, monitor, or modify network requests during test execution.
This helps validate API responses and simulate different backend behaviors.
Example: Intercept Network Requests
def handle_route(route):
print("Intercepted request:", route.request.url)
route.continue_()
page.route("**/*", handle_route)
page.goto("https://example.com")
This script intercepts all network requests and prints the request URL.
Example: Mock API Response
page.route("**/api/users", lambda route: route.fulfill(
status=200,
body='{"name":"Test User"}'
))
This allows testers to simulate API responses without calling the actual backend service.
Playwright Advanced Features – Quick Summary
The following table provides a quick reference for advanced Playwright capabilities used for debugging and network monitoring.
| Feature | Playwright Method | Description | Example |
|---|---|---|---|
| Full Page Screenshot | screenshot() | Capture screenshot of entire page | page.screenshot(path="page.png") |
| Element Screenshot | locator().screenshot() | Capture screenshot of specific element | page.locator("#form").screenshot() |
| Video Recording | record_video_dir | Records browser execution video | browser.new_context(record_video_dir="videos") |
| Intercept Requests | page.route() | Capture network requests | page.route("**/*", handler) |
| Mock API Response | route.fulfill() | Return custom API response | route.fulfill(status=200) |
| Continue Request | route.continue_() | Allow request to proceed | route.continue_() |
Conclusion
Advanced Playwright features such as screenshots, video recording, and network interception provide powerful debugging and testing capabilities for automation engineers.
These features help testers analyze failures, monitor network activity, and simulate backend responses without depending on external systems.
In the next tutorial, we will learn how to integrate Playwright with Pytest and build a scalable automation framework.
No comments:
Post a Comment