rstudio/chromote

Short and long screenshots have different widths despite `setDeviceMetricsOverride`

Closed this issue · 2 comments

This may be the chromote-only version of rstudio/shinytest2#171 (using chromote 865e24e).

b <- chromote::ChromoteSession$new()
b$Emulation$setDeviceMetricsOverride(width=1920, height=1080, deviceScaleFactor=1, mobile=FALSE)

b$Page$navigate("https://www.google.com/search?q=abc")
Sys.sleep(3)
b$screenshot("snap_long.png")

# b$Page$navigate("https://google.com/404")
# Sys.sleep(3)
# b$screenshot("snap_short.png")

Both snapshots should be 1920 pixels wide, but snap_short.png (for whatever reason - not the longer one!) comes out 1890 pixels wide. [Update: The shorter 404 one's dimenions are due to the HTML element having a 15-pixel-wide padding on both sides, which is not included in screenshots, and can be fixed by passing region="padding" to b$screenshot. See below for an updated example.]

I thought I had a solution for this while working on other issues (involving 05586e3 and/or b$Emulation$setScrollbarsHidden(TRUE) near b$Emulation$setDeviceMetricsOverride, but I cannot currently reproduce that. (see below)

This is what I was looking for:

library(chromote)
b <- ChromoteSession$new()
b$Emulation$setDeviceMetricsOverride(width = 1920, height = 1080, deviceScaleFactor = 1, mobile = FALSE)
b$Page$navigate("https://github.com/rstudio/chromote")
Sys.sleep(3)
b$screenshot("snap.png")

This snapshot comes out at width 1905 and may be the closest case to rstudio/shinytest2#171, and it is the case which is actually fixed by b$Emulation$setScrollbarsHidden(TRUE), somewhat regardless of where one puts it.

library(chromote)
b <- ChromoteSession$new()
# b$Emulation$setScrollbarsHidden(TRUE) # works
b$Emulation$setDeviceMetricsOverride(width = 1920, height = 1080, deviceScaleFactor = 1, mobile = FALSE)
# b$Emulation$setScrollbarsHidden(TRUE) # works
b$Page$navigate("https://github.com/rstudio/chromote")
b$Emulation$setScrollbarsHidden(TRUE) # works
Sys.sleep(3)
b$screenshot("snap.png")

This snapshot is 1920 pixels wide, as expected. Interestingly, screenshot() does call setScrollbarsHidden already, but that call seems to be ineffective for some reason:

p <- self$Emulation$setScrollbarsHidden(hidden = TRUE, wait_ = FALSE)$

Closing, since it seems like you figured out a solution.