Skip to content

fix: deviceScaleFactor = 0 disables the metric override#209

Merged
gadenbuie merged 11 commits intomainfrom
fix/shinytest2-404
Mar 12, 2025
Merged

fix: deviceScaleFactor = 0 disables the metric override#209
gadenbuie merged 11 commits intomainfrom
fix/shinytest2-404

Conversation

@gadenbuie
Copy link
Copy Markdown
Member

@gadenbuie gadenbuie commented Mar 12, 2025

Fixes rstudio/shinytest2#404

In rstudio/shinytest2#350, we went from using deviceScaleFactor = 1 before screenshots to using deviceScaleFactor = 0 in the Emulation.setDeviceMetricsOverride command. As pointed out there, the CDP docs explain that this is equivalent to disabling deivce scaling.

The issue rstudio/shinytest2#350 resolved was a problem that was flagged for us on CRAN that was difficult to reproduce. IIRC, we eventually realized that we were getting different screenshot sizes on Mac when the Chrome browser window was being displayed on a Retina display vs a monitor (and by association, the failing manual CRAN tests were happening on a laptop display).

That said, playwright defaults to deviceScaleFactor = 1: https://github.com/microsoft/playwright/blob/3b9515e7efa07230684bef9704c601f509f8ad72/packages/playwright-core/src/server/chromium/crPage.ts#L964

Anyway, for consistently sized screenshots, we may end up in a situation where we need to query window.devicePixelRatio to find the current value (because deviceScaleFactor was set to 0).

In retrospect, the real issue behind rstudio/shinytest2#350 was that we were calling Emulation.setDeviceMetricsOverride and setting deviceScaleFactor = 1, but internally the screenshot method was still using pixel_ratio = 2 because we weren't tracking changes to pixel_ratio. So using deviceScaleFactor = 0 in shinytest2 was equivalent to not changing the pixel ratio.

We could consider not tracking pixel_ratio at all, but our current approach does save us a round trip call or two to the browser whenever we change the viewport size.

@gadenbuie gadenbuie marked this pull request as ready for review March 12, 2025 18:32
@gadenbuie gadenbuie requested a review from wch March 12, 2025 18:32
Copy link
Copy Markdown
Member Author

@gadenbuie gadenbuie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note that deviceScaleFactor basically overrides window.devicePixelRatio

pkgload::load_all()
#> ℹ Loading chromote

b <- ChromoteSession$new()
b$Runtime$evaluate("window.devicePixelRatio")$result$value
#> [1] 1

b$set_viewport_size(800, 800, zoom = 2)
b$Runtime$evaluate("window.devicePixelRatio")$result$value
#> [1] 2

b$set_viewport_size(800, 800, zoom = 1.25)
b$Runtime$evaluate("window.devicePixelRatio")$result$value
#> [1] 1.25

where deviceScaleFactor = 0 is equivalent to removing any overrides, in which case we set private$pixel_ratio = NULL as a signal that we need to check in with the browser to know the current value.

b$set_viewport_size(800, 800, zoom = 0)
b$Runtime$evaluate("window.devicePixelRatio")$result$value
#> [1] 1

@gadenbuie gadenbuie force-pushed the fix/shinytest2-404 branch from 3b67e0d to 20e213e Compare March 12, 2025 21:02
@gadenbuie
Copy link
Copy Markdown
Member Author

Merging so we get overnight tests in shinycoreci; the failing test is flaky and I'll follow up separately to address that.

@gadenbuie gadenbuie merged commit f112166 into main Mar 12, 2025
11 of 12 checks passed
@gadenbuie gadenbuie deleted the fix/shinytest2-404 branch March 12, 2025 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

expect_screenshot(): Failed to deserialize params.clip.scale, double value expected

2 participants