Skip to content

Short and long screenshots have different widths despite set_window_size #171

Open

Description

@bersbersbers
library(shiny)
library(shinytest2)

make_app <- function(n) {
  shinyApp(
    ui = HTML(paste(rep("X", n), collapse="<br>")),
    server = function(input, output) {}
  )
}

AppDriver$new(make_app(n = 1))$set_window_size(100, 100)$get_screenshot("snap1.png")
AppDriver$new(make_app(n = 9))$set_window_size(100, 100)$get_screenshot("snap9.png")

Both snapshots should be 100 pixels wide, but snap9.png comes out 85 pixels wide. 15 pixels seems to be the width of a vertical scrollbar. This is an issue in chromote (see rstudio/chromote#84) which may require code changes in shinytest2.

Versions used: rstudio/chromote@865e24e, 91ff4e5
(in particular, it is independent of the fix to rstudio/chromote#81)

Activity

  1. changed the title [-]Short and long screenshots have different widths[/-] [+]Short and long screenshots have different widths despite `set_window_size`[/+] on Apr 6, 2022
  2. wch commented on Apr 6, 2022

    @wch
    Collaborator

    I don't agree that both screenshots should necessarily be 100 pixels wide. The way it works is: there is a viewport size; the browser renders the page for that size viewport, and then it takes a screenshot of the page. What exactly happens in that last step is, of course, tricky.

    When a (normal) browser views a tall page, it adds a scrollbar and makes the page size narrower to accommodate. (Note that the behavior might be different on a Mac with auto-hiding scrollbars.) So the current screenshot behavior takes an image of the page that is the same size as what the browser says it is.

    All that said, I don't think that the screenshot of the longer page should necessarily be narrower than 100 pixels either -- I'm just pointing out that the behavior is a choice, and that it's not that one is right and the other is wrong. I do think that a 100-pixel-wide screenshot is more convenient for many use cases.

    One more note: If any changes are made here, they should be tested on:

    • A Mac with auto-hiding scrollbars
    • A Windows or Linux machine, or aMac with visible scrollbars

    This is because auto-hiding scrollbars on a Mac work differently from the visible scrollbars -- the former get overlaid on the page and don't reduce the width.

  3. bersbersbers commented on Apr 6, 2022

    @bersbersbers
    ContributorAuthor

    @wch I fully agree. I had understood the presence of self$Emulation$setScrollbarsHidden(hidden = TRUE, wait_ = FALSE) in
    https://github.com/rstudio/chromote/blob/865e24e2270afab07e587909499c4e06f03bbedf/R/screenshot.R#L52 (which in other contexts does make the two viewports and snapshots 100 pixels wide, see rstudio/chromote#84 (comment)) as an indication that this choice was to make the two snapshots the same width - or am I overlooking another reason for calling setScrollbarsHidden? At least on Windows Chrome, the scrollbars do not seem to be part of the HTML element which is the one being captured by default.

  4. bersbersbers commented on Apr 8, 2022

    @bersbersbers
    ContributorAuthor

    I think the perfect solution would add some hide_scrollbars_for_full_width = FALSE argument to shinytest2s get_screenshot which, if TRUE, calls chrome$Emulation$setScrollbarsHidden(hidden = TRUE) before taking the snapshot. The ineffective call to self$Emulation$setScrollbarsHidden inside chromote's screenshot could potentially be removed (but see rstudio/chromote#84 for that).

  5. added this to the v0.2.0 milestone on Apr 25, 2022
  6. bersbersbers commented on May 17, 2022

    @bersbersbers
    ContributorAuthor

    Here's another thought related to snapshot sizes. While the above thoughts are about having some mimum size, this example is about exceeding that minimum size. In this snippet, I had somewhat expected that snap9.png is made wide enough to accomodate all X, leading to snapshot more than 100 pixels wide:

    library(shiny)
    library(shinytest2)
    
    make_app <- function(n) {
      shinyApp(
        ui = HTML(paste(rep("XXX", n), collapse="")),
        server = function(input, output) {}
      )
    }
    
    AppDriver$new(make_app(n = 1))$set_window_size(100, 100)$get_screenshot("snap1.png")
    AppDriver$new(make_app(n = 9))$set_window_size(100, 100)$get_screenshot("snap9.png")

    (I had expected that due to the use of captureBeyondViewport = TRUE in https://github.com/rstudio/chromote/blob/1884e3bd5a4854832b44387a3cb3144c2ff757af/R/screenshot.R#L103)

    But that is not the case - both snapshots are 100x35. I have played around with selectors a bit (HTML, body, ...) without much success so far. This might be due to Chrome's box model - I played around with this code:

    library(shiny)
    make_app <- function(w = 1, h = 1) {
      shinyApp(
        ui = HTML(paste(rep(paste(rep("XXX", w), collapse=""), h), collapse="<br>")),
        server = function(input, output) {}
      )
    }
    make_app(w = 99, h = 99)

    and while the height of the HTML (and other) nodes can easily exceed the window size (in Chrome Dev Tools), it seems that the width of it cannot... Highlighting the HTML node, $0.scrollWidth gives me the desired number, though. In addition, scrollWidth/Height seem to be always at least as large as the window, and larger if necessary. It's basically what I will be trying to use as a basis for screenshots.

    Update: This piece of code now does exactly what I want: screenshots have at least window size if the window is not filled, and if the page content is wider or higher than the page, then the screenshot is as well. No space is reserved for scrollbars.

    min_width <- 100
    min_height <- 100
    
    make_app <- function(nw = 1, nh = 1) {
      shiny::shinyApp(
        ui = shiny::HTML(paste(rep(paste(rep("XXX", nw), collapse=""), nh), collapse="<br>")),
        server = function(input, output) {}
      )
    }
    
    make_snap <- function(nw = 1, nh = 1) {
        app <- shinytest2::AppDriver$new(make_app(nw = nw, nh = nh))
        app$set_window_size(min_width, min_height)
        b <- app$get_chromote_session()
        rect_size <- b$Page$getLayoutMetrics()$cssContentSize
        rect_size <- pmax(as.numeric(rect_size), c(0, 0, min_width, min_height))
    
        filename <- paste0("/tmp/snap-", nw, "-", nh, ".png")
        unlink(filename)
        app$get_screenshot(filename, screenshot_args = list(cliprect = rect_size))
    }
    
    make_snap(1, 1)  # fits on screen
    make_snap(1, 9)  # higher
    make_snap(9, 1)  # wider
    make_snap(9, 9)  # higher and wider

    exiftool *.png | grep -E "File Name|Image Size"

    File Name                       : snap-1-1.png
    Image Size                      : 100x100
    File Name                       : snap-1-9.png
    Image Size                      : 100x187
    File Name                       : snap-9-1.png
    Image Size                      : 278x100
    File Name                       : snap-9-9.png
    Image Size                      : 278x187
    

    It would be great if such functionality were directly accessible through shinytest2, but the above is simple enough.

  7. modified the milestones: v0.2.0, v0.3.0 on Jun 8, 2022
  8. schloerke commented on Feb 10, 2023

    @schloerke
    Collaborator

    @bersbersbers

    The default selector for $get_screenshot() and $expect_screenshot() is now "scrollable_area" as of #325 . This PR also added support for selector="viewport" which takes an image of the current browsers viewport.

    Using your example above with the new selectors:

    make_app <- function(n) {
      shinyApp(
        ui = HTML(paste(rep("XXX", n), collapse="")),
        server = function(input, output) {}
      )
    }
    
    AppDriver$new(make_app(n = 1))$set_window_size(100, 100)$get_screenshot("snap1-new-default.png")
    AppDriver$new(make_app(n = 9))$set_window_size(100, 100)$get_screenshot("snap9-new-default.png")
    
    AppDriver$new(make_app(n = 1))$set_window_size(100, 100)$get_screenshot("snap1-viewport.png", selector = "viewport")
    AppDriver$new(make_app(n = 9))$set_window_size(100, 100)$get_screenshot("snap9-viewport.png", selector = "viewport")
    
    AppDriver$new(make_app(n = 1))$set_window_size(100, 100)$get_screenshot("snap1-prev-default.png", selector = "html")
    AppDriver$new(make_app(n = 9))$set_window_size(100, 100)$get_screenshot("snap9-prev-default.png", selector = "html")
    Selector n=1 n=9
    selector="scrollable_area"
    New default custom selector
    snap1-new-default snap9-new-default
    selector="viewport"
    New custom selector
    snap1-viewport snap9-viewport
    selector="html"
    Previous default selector
    snap1-prev-default snap9-prev-default

    @bersbersbers Would these two new custom selectors (and default behavior) would work for you?

  9. bersbersbers commented on Feb 11, 2023

    @bersbersbers
    ContributorAuthor

    It will take a while until I can try this myself, but since 5 out of 6 snapshots are 100px wide (which is what I originally requested) and the 6th seems to accomodate my request from #171 (comment), this looks like the perfect solution to me. Looks like I can remove that workaround at some point :) thanks!

  10. bersbersbers commented on Feb 11, 2023

    @bersbersbers
    ContributorAuthor

    (For the record, I haven't followed commits in a while, so I have no explanation why snap9-prev-default.png in #171 (comment) differs from the actual previous default snap9.png in my original post. But I am happy with it!)

  11. modified the milestones: v0.3.0, future on Nov 21, 2025
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions