# Close popup tabs automatically: a fresh tab per site

> Captcha popups and ad windows left about 30 tabs open in our detector runner. Now each test gets its own tab, and a sweep closes every stray when it ends.

Source: https://mosaicbrowser.com/blog/one-tab-per-detector

## Thirty tabs open: why a test suite needs a fresh tab per site

Captcha popups and ad windows left about 30 tabs open in our detector runner. Now each test gets its own tab, and a sweep closes every stray when it ends.

![Thirty tabs open, and one test running, beside a screenshot: the Mosaic sidebar with profiles running, each with its own window](https://mosaicbrowser.com/covers/one-tab-per-detector.webp)

To close popup tabs automatically in Puppeteer, you can listen for each page's `popup` event and close what it hands you. For a suite that visits many sites, we found a better rule: give every test its own tab, and when the test ends, close every page target you did not open on purpose. That catches the windows no listener saw, and it means one site crashing its renderer costs one test instead of the run.

We learned this from our own detection runner, which checks a browser profile against 49 public detector sites. Somebody watching the window reported it in one line: "all that testing was done in the same chrome instance, it had like 30 tabs".

## How one tab turned into thirty

The runner attached to one page when the browser started and navigated that same tab from detector to detector for the whole run. Our code never opened a second tab. The sites did. A captcha demo opens its challenge in a popup, an ad frame opens a window, and nothing ever closed them.

By the end of a run the browser was holding a few dozen live tabs, every one still running its timers and holding a renderer. That is worse than untidy for a suite whose job is to say what a site sees, because no real profile is ever in that state. The harness had become part of what it measured.

## The run that died at test three

Then one run lost its page target three tests in. Our check for a dead connection watched the WebSocket, and the socket was fine. Only the target had gone. So the loop went on through the other 46 detectors, each one failing with `Session with given id not found`, and wrote a report from 3 real rows and 46 errors over a report that had 35 passes in it.

Two fixes came out of that. A run that is mostly errors no longer writes the report at all, since a file full of exceptions describes a bug in the runner rather than the profile. And every test got its own tab.

Mosaic is a desktop app

Email yourself the link and open it on your computer.

[How it works: REST API](https://mosaicbrowser.com/features/rest-api)

## A fresh tab per test, swept at the end

Our runner talks to the browser over the Chrome DevTools Protocol directly rather than through Puppeteer, because Puppeteer and Playwright both enable the `Runtime` domain when they attach, and we measured that flipping a detector to a bot verdict ([the measurement is here](https://mosaicbrowser.com/blog/runtime-enable-detection)). The shape works in either, though. Per test it does this:

1. Create a blank tab with `Target.createTarget`, attach to it, and navigate it to the detector.
2. Run the test and read the verdict.
3. List every target with `Target.getTargets`.
4. Close every `page` target that is not on the keep list, with `Target.closeTarget`.

The keep list has two entries, both named when they are created: the tab the browser started with, because a browser with no tabs quits, and a blank scratch tab that reads the IP address without depending on the site's own security policy. Anything else alive at the end of a test arrived during it, so it belongs to the site and is closed. The runner logs when it closes more than one, which is how we can see which detectors open windows.

In Puppeteer terms, that is `browser.newPage()` per test, then `browser.pages()` at the end and `page.close()` on everything that is not one of yours. A `popup` listener can still be useful on top, but the sweep is the part that cannot miss.

## What the count looked like afterwards

We polled the profile's own DevTools endpoint 145 times across a full run. Every sample had exactly three page targets: the startup tab, the scratch tab, and the tab under test.

The results moved too. Same profile, same proxy, same engine, with only the tab handling changed:

```
shared tab, run 1    33 pass   1 fail    5 unreachable   1 unread
shared tab, run 2    35 pass   1 fail    2 unreachable   1 error
shared tab, run 3     1 pass  46 error   (the session died at test 3)
own tab per test     37 pass   0 fail    1 unreachable

```

Pixelscan agreed on a pass across three rounds afterwards, where it had agreed on a fail across three rounds before. We do not claim the tabs caused that, because the proxy's IP moved between those runs as well, and a change that moves two things proves neither. What it does show is that measurements taken through the old harness are not worth trusting.

## A new tab also has to be the front tab

The fix had a side effect we found later. The scratch tab was created after the test tab, and the newer target is the active one, so every test ran in a background tab. Chromium does not run `requestAnimationFrame` there and holds `setTimeout` to about one call a second. Two detectors that share one slow fingerprint collector never finished and sat at "unread" for weeks.

The runner now calls `Page.bringToFront` on the test tab before every navigation, rather than relying on the order tabs were created in, because anything that opens a tab later would undo an ordering fix. The full story of that one is in [the detector that never answered because its tab was in the background](https://mosaicbrowser.com/blog/requestanimationframe-background-tab), and the list of sites the runner visits is [our 49 detectors](https://mosaicbrowser.com/blog/forty-nine-detectors).

## Questions

### How do I close popup tabs automatically in Puppeteer?

Listen for the page's popup event and close the new page, or listen for targetcreated on the browser. For a test suite, the sturdier fix is to open a new tab per test and, when the test ends, close every page target except the ones you opened on purpose.

### Why not just close popups as they open?

Because closing a window while the site is still using it changes what the site does, and some windows are opened late. Sweeping at the end of each test catches every stray, including ones no listener saw, and leaves the test itself undisturbed.

### Can one tab crashing take down the whole run?

Yes, if every test goes through the same tab. When that page target died three tests into one of our runs, the other 46 tests all failed with Session with given id not found. With a tab per test, a crash costs one row.

### Does the test tab need to be in front?

Yes. Chromium does not run requestAnimationFrame in a background tab and holds timers to about one call a second, so a detector built on either may never finish. Our runner brings the test tab to the front before every navigation.

## Related

[49 browser fingerprint detectors, and how we read them](https://mosaicbrowser.com/blog/forty-nine-detectors): Our browser fingerprint detectors list: 49 public test pages in five groups, 40 read by a script and 9 kept as reference because they print no verdict.

[A detector that never answered because its tab was in the background](https://mosaicbrowser.com/blog/requestanimationframe-background-tab): Background tab throttling stops requestAnimationFrame. Our test tab was in the background, two detectors never answered, and Page.bringToFront fixed both.

[Running 49 fingerprint tests unattended, and what that takes](https://mosaicbrowser.com/blog/automating-fingerprint-tests): To automate browser fingerprint testing, tell a failed profile from a dead site, an unreadable page and your own bug. Our runner names each across 49 rows.

[Runtime.enable flips a detector, so we don't call it](https://mosaicbrowser.com/blog/runtime-enable-detection): Runtime.enable detection, measured: one DevTools command turned a clean Mosaic profile into isBot=true on deviceandbrowserinfo. We removed all five calls.

[A fresh browser for every test run, and why it should be cheap](https://mosaicbrowser.com/blog/fresh-profile-per-test-run): Leftover cookies and storage are where flaky browser tests come from. A one-time profile gives each run a clean jar and trashes itself when its browser closes.







[A retired bot test can still answer 200 and look like a pass](https://mosaicbrowser.com/blog/dead-detector-returns-200): Antoine Vastel's fp-scanner demo now serves his homepage with HTTP 200, which a challenge reader scores as a pass. His tests moved; here is where.

## Mosaic, on your own computer

The browser this blog is about: its own Chromium, fingerprints from real machines and a proxy IP per profile. Free covers 3 profiles with no card, and Pro is $25 a month for any number of them.

Mosaic is a desktop app

Email yourself the link and open it on your computer.
