Skip to content

Screenshots are white beyond the window size when using setDeviceMetricsOverride #81

Closed

Description

@bersbersbers

Originally posted as rstudio/shinytest2#161, but independent of shinytest2.

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()
Details

screenshot

Activity

  1. changed the title [-]Screenshots are white beyond the window size[/-] [+]Screenshots are white beyond the window size when using `setDeviceMetricsOverride`[/+] on Apr 6, 2022
  2. bersbersbers commented on Apr 6, 2022

    @bersbersbers
    ContributorAuthor

    Looks like this exact problem has been seen before, although in a different context:

    chromote/R/screenshot.R

    Lines 72 to 80 in 865e24e

    # Make viewport the same size as content -- seems to be necessary
    # on Chrome 75 for Mac, though it wasn't necessary for 72. Without
    # this, the screenshot will be the full height, but everything
    # outside the viewport area will be blank white.
    self$Emulation$setVisibleSize(
    width = overall_width,
    height = overall_height,
    wait_ = FALSE
    )

    This worked for me:

    diff --git a/R/screenshot.R b/R/screenshot.R
    index cd5af87..fedece1 100644
    --- a/R/screenshot.R
    +++ b/R/screenshot.R
    @@ -42,7 +42,6 @@ chromote_session_screenshot <- function(
     
       # These vars are used to store information gathered from one step to use
       # in a later step.
    -  visual_viewport <- NULL  # Initial viewport dimensions
       image_data      <- NULL
       overall_width   <- NULL
       overall_height  <- NULL
    @@ -51,11 +50,6 @@ chromote_session_screenshot <- function(
       # Setup stuff for both selector and cliprect code paths.
       p <- self$Emulation$setScrollbarsHidden(hidden = TRUE, wait_ = FALSE)$
         then(function(value) {
    -      self$Page$getLayoutMetrics(wait_ = FALSE)
    -    })$
    -    then(function(value) {
    -      visual_viewport <<- value$visualViewport
    -
           self$DOM$getDocument(wait_ = FALSE)
         })$
         then(function(value) {
    @@ -69,16 +63,6 @@ chromote_session_screenshot <- function(
           overall_width  <<- value$model$width
           overall_height <<- value$model$height
     
    -      # Make viewport the same size as content -- seems to be necessary
    -      # on Chrome 75 for Mac, though it wasn't necessary for 72. Without
    -      # this, the screenshot will be the full height, but everything
    -      # outside the viewport area will be blank white.
    -      self$Emulation$setVisibleSize(
    -        width = overall_width,
    -        height = overall_height,
    -        wait_ = FALSE
    -      )
    -
           promise(function(resolve, reject) {
             # Wait `delay` seconds for resize to complete. For complicated apps this may need to be longer.
             ## TODO: Can we wait for an event instead?
    @@ -116,6 +100,7 @@ chromote_session_screenshot <- function(
                 scale = scale / private$pixel_ratio
               ),
               fromSurface = TRUE,
    +          captureBeyondViewport = TRUE,
               wait_ = FALSE
             )
           })$
    @@ -136,6 +121,7 @@ chromote_session_screenshot <- function(
                 scale = scale / private$pixel_ratio
               ),
               fromSurface = TRUE,
    +          captureBeyondViewport = TRUE,
               wait_ = FALSE
             )
           })$
    @@ -146,13 +132,6 @@ chromote_session_screenshot <- function(
     
       p <- p$
         then(function(value) {
    -      # Restore original viewport size
    -      self$Emulation$setVisibleSize(
    -        width = visual_viewport$clientWidth,
    -        height = visual_viewport$clientHeight,
    -        wait_ = FALSE
    -      )
    -
           # Un-hide scrollbars
           self$Emulation$setScrollbarsHidden(hidden = FALSE, wait_ = FALSE)
         })$
    • There's some related discussion in https://bugs.chromium.org/p/chromium/issues/detail?id=1198576 which I did not fully read.

    • I now get a 1905x28962 screenshot, which is almost what I expected: I'd like it to be 1920 in width, so I'm looking for a way to turn the Chrome instance to full-screen mode.

      b$Browser$setWindowBounds(1, bounds = list(windowState = "fullscreen"))
      b$Browser$setWindowBounds(2, bounds = list(windowState = "fullscreen"))

      does not change the width of the snapshot, although

      b$Browser$getWindowBounds(1)
      b$Browser$getWindowBounds(2)

      confirms that values are set. (But maybe that effort is misguided anyway, as Chrome does not have any left/right borders in non-full-screen mode. It might be related to the scrollbars taking space despite being invisible.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions