Catalog / Puppeteer Testing & Debugging Cheatsheet
Puppeteer Testing & Debugging Cheatsheet
A practical guide to testing and debugging Puppeteer scripts, covering common techniques and tools for robust automation.
Debugging Fundamentals
Console Logging
Using Example:
|
To view console messages from within the browser, listen to the Example:
|
Debugging with Chrome DevTools
Launch Puppeteer with the Example:
Set |
Use Example:
|
Alternatively, use Example:
|
Error Handling
Wrap Puppeteer code in Example:
|
Listen to the Example:
|
Testing Techniques
Basic Assertions
Use assertion libraries like Example with
|
Visual Regression Testing
Capture screenshots and compare them against baseline images to detect visual changes. Example:
Consider using libraries like |
Using Jest with image snapshot
|
End-to-End Testing
Simulate user interactions to test complete workflows. Example:
|
Advanced Debugging
Tracing
Use tracing to analyze performance and identify bottlenecks. Example:
Open |
Network Interception
Monitor and modify network requests and responses. Example:
|
To abort requests based on URL:
|
EvaluateOnNewDocument
Inject scripts that run before any other script on the page. Example:
|
Test Framework Integration
Jest Integration
Example setup with Jest:
|
Mocha & Chai Integration
Example setup with Mocha and Chai:
|
Continuous Integration (CI)
Configure your CI environment to run Puppeteer tests in a headless environment. Ensure necessary dependencies are installed (e.g., Chrome or Chromium). Use environment variables to configure Puppeteer’s launch options for CI. |
Example using GitHub Actions:
|