You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Short and long screenshots have different widths despite set_window_size #171
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.
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
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.
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).
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:
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:
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.
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.
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!
(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!)
Activity
[-]Short and long screenshots have different widths[/-][+]Short and long screenshots have different widths despite `set_window_size`[/+]wch commentedon Apr 6, 2022
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:
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.
bersbersbers commentedon Apr 6, 2022
@wch I fully agree. I had understood the presence of
self$Emulation$setScrollbarsHidden(hidden = TRUE, wait_ = FALSE)inhttps://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 theHTMLelement which is the one being captured by default.bersbersbers commentedon Apr 8, 2022
I think the perfect solution would add some
hide_scrollbars_for_full_width = FALSEargument toshinytest2sget_screenshotwhich, ifTRUE, callschrome$Emulation$setScrollbarsHidden(hidden = TRUE)before taking the snapshot. The ineffective call toself$Emulation$setScrollbarsHiddeninsidechromote'sscreenshotcould potentially be removed (but see rstudio/chromote#84 for that).bersbersbers commentedon May 17, 2022
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.pngis made wide enough to accomodate allX, leading to snapshot more than 100 pixels wide:(I had expected that due to the use of
captureBeyondViewport = TRUEin 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: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 theHTMLnode,$0.scrollWidthgives 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.
exiftool *.png | grep -E "File Name|Image Size"It would be great if such functionality were directly accessible through shinytest2, but the above is simple enough.
schloerke commentedon Feb 10, 2023
@bersbersbers
The default
selectorfor$get_screenshot()and$expect_screenshot()is now"scrollable_area"as of #325 . This PR also added support forselector="viewport"which takes an image of the current browsers viewport.Using your example above with the new selectors:
n=1n=9selector="scrollable_area"New default custom selector
selector="viewport"New custom selector
selector="html"Previous default selector
@bersbersbers Would these two new custom selectors (and default behavior) would work for you?
bersbersbers commentedon Feb 11, 2023
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!
bersbersbers commentedon Feb 11, 2023
(For the record, I haven't followed commits in a while, so I have no explanation why
snap9-prev-default.pngin #171 (comment) differs from the actual previous defaultsnap9.pngin my original post. But I am happy with it!)