browser-playwright: failure screenshot fails with strict mode violation when there are 2 `<body>` elements
Describe the bug
Even though it might seem weird at first, we use a library that for calculation purposes has a hidden <body> element inside its shadowRoot.
When a browser-mode test fails, Vitest screenshots the page through locator('body'). Playwright's CSS engine pierces open shadow roots, so any element inside a shadow root makes the locator match two elements. The strict-mode check then throws, and the failure screenshot is lost with an error message.
[vitest] Failed to take a screenshot [Error: locator.screenshot: Error: strict mode violation: locator('body') resolved to 2 elements: 1) <body>…</body> aka locator('body').first() 2) <body></body> aka locator('div body')
Expected: the screenshot targets the document's own body, for example with locator('body').first() and the screenshot is successfully taken.
Reproduction
https://github.com/SomaH123/vitest-screenshot-repro
System Info
System:
OS: Linux 6.18 Manjaro Linux
CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11850H @ 2.50GHz
Memory: 9.26 GB / 31.07 GB
Container: Yes
Shell: 5.3.9 - /bin/bash
Binaries:
Node: 24.19.0 - /home/somah/.nvm/versions/node/v24.19.0/bin/node
npm: 12.0.2 - /home/somah/.nvm/versions/node/v24.19.0/bin/npm
pnpm: 10.33.0 - /usr/bin/pnpm
Browsers:
Brave Browser: 147.1.89.145
Chromium: 148.0.7778.96
Firefox: 150.0.1
Firefox Developer Edition: 150.0.1
npmPackages:
@vitejs/plugin-vue: ^6.0.9 => 6.0.9
@vitest/browser-playwright: ^5.0.1 => 5.0.1
@vitest/coverage-istanbul: ^5.0.1 => 5.0.1
playwright: ^1.63.0 => 1.63.0
vite: ^8.3.1 => 8.3.1
vitest: ^5.0.1 => 5.0.1
vitest-browser-vue: ^3.1.0 => 3.1.0Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Source: vitest-dev/vitest