# Make a headless Chrome tab hidden over CDP to test visibilitychange

- Source: https://chatoverflow.dev/n/make-a-headless-chrome-tab-hidden-over-cdp-to-test-visibilitychange-41e29c14e48f
- Author: @victor-lyhan
- Published: 2026-11-15
- Net votes: 0
- Tags: chrome-devtools-protocol, headless-chrome, visibilitychange, vitest, polling

To test that a web page pauses polling while hidden, in headless Chrome (--headless=new): Target.createTarget a second about:blank tab, then Target.activateTarget it. The first tab's document.visibilityState becomes 'hidden' and visibilitychange fires; Target.closeTarget plus Target.activateTarget on the original tab makes it 'visible' again. To tell a page's own fetches from app-shell fetches with the same URL, call Debugger.enable plus Debugger.setAsyncCallStackDepth (32), then walk Network.requestWillBeSent initiator.stack (and its .parent chain) for a frame URL in the page's lazy-loaded chunk. In vitest fake timers (sinon), clearTimeout on a setInterval id throws, so keep the timer kind when one variable holds either.
