how to make playwright clicks show red
- User
how to make playwright clicks show red
- Assistant
how to make playwright clicks show red
If you want to visually highlight clicks performed by Playwright during a test by showing a red circle or some red indication on the clicked element (or at the click position) in the browser, Playwright doesn’t have a built-in option for this, but you can achieve it by injecting some JavaScript code into the page.
Here is a common approach:
You can add a snippet that attaches a click listener to the page that creates a red circle animation at the click location.
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
// Load some page
await page.goto('https://example.com');
// Inject custom click-highlighting script
await page.addInitScript(() => {
document.addEventListener('click', (event) => {
// Create the red circle
const circle = document.createElement('div');
circle.style.position = 'fixed';
circle.style.left = `${event.clientX - 15}px`;
circle.style.top = `${event.clientY - 15}px`;
circle.style.width = '30px';
circle.style.height = '30px';
circle.style.border = '3px solid red';
circle.style.borderRadius = '50%';
circle.style.pointerEvents = 'none';
circle.style.zIndex = '9999';
circle.style.transition = 'opacity 0.5s ease-out';
circle.style.opacity = '1';
document.body.appendChild(circle);
// Animate fade out then remove
setTimeout(() => {
circle.style.opacity = '0';
setTimeout(() => {
circle.remove();
}, 500);
}, 300);
}, true);
});
// Now when you do clicks, you will see a red circle highlight
await page.click('h1'); // Example click
// Wait a bit to see the effect
await page.waitForTimeout(1000);
await browser.close();
})();
page.addInitScript injects a script that runs in the browser context before any page scripts run.true).If you want the border of the clicked element to flash red instead of a circle at the pointer:
await page.addInitScript(() => {
document.addEventListener('click', (event) => {
const el = event.target;
const originalBorder = el.style.border;
el.style.border = '3px solid red';
setTimeout(() => {
el.style.border = originalBorder;
}, 500);
}, true);
});
This way you can visually debug where Playwright is clicking by adding a red visual indication.
If you want something more advanced you can create visuals with CSS animations or overlays. But this simple snippet works well for quick debugging.
Not yet included in a published release.