When using Playwright with JavaScript, the messages starting with console.log [Interceptor] Web Console: are typically generated by the browser's console output being intercepted and logged by Playwright. These messages are often related to browser console events (e.g., console.log, console.error, etc.) that Playwright captures by default during test execution.
To disable or filter out these messages, you can take one of the following approaches:
1. Disable Console Message Interception
By default, Playwright listens to console messages in the browser and logs them to the terminal. You can disable this behavior by ignoring the console event on the page. Here's how you can do it:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
// Disable console message logging by overriding the console event handler
page.removeAllListeners('console');
// Your test code here
await page.goto('https://example.com');
await browser.close();
})();
By calling page.removeAllListeners('console'), you prevent Playwright from logging any console messages to the terminal.
2. Filter Specific Console Messages
If you want to keep some console messages but filter out the ones starting with [Interceptor] Web Console:, you can add a custom console event handler to selectively ignore those messages. Here's an example:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
// Add a custom console event handler to filter messages
page.on('console', (msg) => {
const text = msg.text();
// Ignore messages that start with '[Interceptor] Web Console:'
if (!text.startsWith('[Interceptor] Web Console:')) {
console.log(`Console message: ${text}`);
}
});
// Your test code here
await page.goto('https://example.com');
await browser.close();
})();
In this approach, you only log console messages that do not match the pattern you're trying to suppress.
3. Disable Playwright Debugging Output
If the [Interceptor] Web Console: messages are part of Playwright's internal debugging or verbose logging, you can reduce the verbosity of Playwright's output by setting the DEBUG environment variable to exclude Playwright's internal logs. For example:
# Run your tests with the DEBUG environment variable set to exclude Playwright logs
DEBUG=-pw:* node your-test-script.js
Alternatively, you can disable Playwright's debug logging entirely by ensuring the DEBUG environment variable is not set to pw:* or similar values.
4. Redirect Playwright Logs to a File
If you want to keep the logs but prevent them from cluttering your terminal, you can redirect Playwright's output to a file. For example:
node your-test-script.js > playwright-logs.txt 2>&1
This approach doesn't disable the logs but moves them out of your terminal, making it easier to focus on the test output.
5. Check for Third-Party Interceptors
If the [Interceptor] Web Console: messages are not coming from Playwright but from a third-party library or browser extension (e.g., a network interceptor or debugging tool), you may need to disable or configure that tool. For example:
- If you're using a browser extension, launch Playwright with a clean browser profile by using a new browser context without extensions.
- If the interceptor is part of your application code, you may need to modify the application to disable it during testing (e.g., by setting an environment variable or configuration flag).
To launch Playwright with a clean browser profile, use:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({ args: ['--disable-extensions'] });
const context = await browser.newContext();
const page = await context.newPage();
// Your test code here
await page.goto('https://example.com');
await browser.close();
})();
Summary
- Use
page.removeAllListeners('console') to disable all console message logging.
- Use a custom
page.on('console', ...) handler to filter out specific messages.
- Adjust the
DEBUG environment variable to reduce Playwright's verbose logging.
- Redirect logs to a file if you want to keep them but avoid terminal clutter.
- Check for third-party interceptors or extensions and disable them if necessary.
Choose the approach that best fits your needs, depending on whether you want to disable all console messages, filter specific ones, or address a third-party issue.