Skip to content

Screenshot dimensions #9363

Closed

Description

@tschaub

Clear and concise description of the problem

I'm using the Playwright provider and trying to use screenshots for rendering tests with the following vite.config.js test config:

{
  browser: {
    ui: true,
    provider: playwright({
      launchOptions: {
        args: ['--enable-gpu', '--force-device-scale-factor=1'],
      },
    }),
    enabled: true,
    instances: [{browser: 'chromium'}],
    viewport: {width: 512, height: 512},
  },
}

In my tests, I capture a screenshot with await expect(page.elementLocator(target)).toMatchScreenshot(); and the target element has CSS dimensions of 512x512. The problem is that the vitest iframe with the Browser UI has an arbitrary width depending on the --tester-transform scale that is applied, and the screenshot dimensions are based on this scaled element's dimensions instead of dimensions that I control.

I can get tests to pass by running them in headless mode or by setting browser.ui to false. Is there an existing way to configure things so that screenshots captured using headed tests with browser.ui set to true will have a fixed or known dimension?

Suggested solution

I'm not sure what a good solution would be. Maybe another option to disable scaling of the tester-ui element?

{
  viewport: {width: 512, height},
  scaleViewport: false, // this would make it so tester-ui doesn't have a scale transform applied
}

Alternative

No response

Additional context

# npm ls vitest
├─┬ @vitest/browser-playwright@4.0.16
│ ├─┬ @vitest/browser@4.0.16
│ │ └── vitest@4.0.16 deduped
│ └── vitest@4.0.16 deduped
└── vitest@4.0.16

Validations

Activity

  1. added theissue type on Dec 30, 2025
  2. philholden commented on Jan 14, 2026

    @philholden

    Similar issue. With the UI enabled I get sub-pixel issues with SVGs. The dividers on the three pane layout do not snap to pixels. The columns have percentage widths. This means the iframe is nearly always rendered on a sub-pixel e.g. doing getBoundingClientRect().x on the parent of the iframe I get: 676.3203125

    It would be helpful if the dividers snapped to pixels. If too hard shift the iframe wrapper div back to the nearest pixel boundary using relative positioning e.g. {position: relative, left: -0.3203125} (needs to be updated on resize of screen or dragging of left divider).

    Third (easiest) option move the browser to the first panel.

  3. hon2a commented on Jan 26, 2026

    @hon2a

    I'm facing the same issue even when running tests with ui: false and/or headless: true, see #9124 (comment). I haven't been able to find a configuration that'd let me take screenshots of arbitrarily large viewport without scaling them down.

  4. veykos commented on Feb 6, 2026

    @veykos

    I have the same issue - using

    await expect(someLocator).toMatchScreenshot('some-name');
    

    in headless mode takes the proper size screenshots - for example my viewport is 1280 x 720, the element is taking 1280x88 and the screenshot is 1280x88.
    Turning off headless mode the screenshot size is 1966×136px.

    This makes it so test are almost not runnable in UI mode except with expect.soft which still throws warnings but at least I can check if the actual test is passing.

  5. larsswanstrommyh commented on Mar 11, 2026

    @larsswanstrommyh

    Me too having this problem. All tests green in with headless, all tests red with UI (or vice verse depending on how the reference screenshots were taken).

    Reference screenshot, 333×17px, (headless: true)
    Image

    Actual screenshot , 403×20px, (headless: false)
    Image

    Error: expect(element).toMatchScreenshot()
    
    Screenshot does not match the stored reference.
    Expected image dimensions to be 333×17px, but received 403×20px.
    
  6. craigwalkeruk commented on Mar 16, 2026

    @craigwalkeruk

    I have the same issue, even when working locally on my retina display and switching to a montior the scaling changes due to -tester-transform moving the goal posts.

    I'm now working in a docker image to for consistentcy but it kind defeats the pourpose of having the UI and the diff slider etc if it the scale will break the test on diferent machines.

    I've created a test project with my setup: https://github.com/craigwalkeruk/green-surf-co-angular/tree/8cd8efe9d8a85476107d5c3e5656854705dec375

    You can see I'm working around other challenages if your curiouis with this command

    Hope it helps

  7. locked and limited conversation to collaborators on May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions