Skip to content

Take screenshots beyond viewport without resizing - #83

Merged
schloerke merged 1 commit into
rstudio:mainfrom
bersbersbers:snapshot_without_resize
Apr 15, 2022
Merged

schloerke merged 1 commit into
rstudio:mainfrom
bersbersbers:snapshot_without_resize

Conversation

@bersbersbers

Copy link
Copy Markdown
Contributor

This closes #81 and may also help with rstudio/shinytest2#136

@wch

wch commented Apr 6, 2022

Copy link
Copy Markdown
Collaborator

Interesting -- this should really simplify the screenshotting process. I believe that the captureBeyondViewport argument didn't exist when this code was originally written.

A couple things to watch out for:

  • What happens if the page is smaller than the viewport?
  • What happens when scrollbars are present?

@wch wch closed this Apr 6, 2022
@schloerke

Copy link
Copy Markdown
Collaborator

@wch Why was this PR closed?

@wch

wch commented Apr 6, 2022

Copy link
Copy Markdown
Collaborator

Oh oops, I must have accidentally clicked that.

@wch wch reopened this Apr 6, 2022
@bersbersbers

bersbersbers commented Apr 6, 2022 •

Copy link
Copy Markdown
Contributor Author

I believe that the captureBeyondViewport argument didn't exist when this code was originally written.

Yes, this landed in Chrome 89, and the code I deleted refers to Chrome 72 and 75.

  • What happens if the page is smaller than the viewport?

Do you mean vertically, horizontally, both?

  • What happens when scrollbars are present?

I filed (in principle, unrelated) issues as the current implementation has problems in exactly these cases, see rstudio/shinytest2#171 and #84

@wch

wch commented Apr 6, 2022

Copy link
Copy Markdown
Collaborator
  • What happens if the page is smaller than the viewport?

Do you mean vertically, horizontally, both?

I was thinking vertically (which would be far more common), although I suppose it's good to know what happens if it's smaller horizontally as well.

The reason I ask is because the existing behavior would take a screenshot that is the same size of the page, but I think that the new behavior would be to take a screenshot that is larger than the page. (Just guessing; I haven't tried this out myself.)

@bersbersbers

bersbersbers commented Apr 6, 2022 •

Copy link
Copy Markdown
Contributor Author

The reason I ask is because the existing behavior would take a screenshot that is the same size of the page, but I think that the new behavior would be to take a screenshot that is larger than the page. (Just guessing; I haven't tried this out myself.)

Understood. I would have to try that out myself, too: I never use it without app$set_window_size or b$Emulation$setDeviceMetricsOverride, but you may be right that the session's (somewhat odd) default dimensions might come into play then:

initialize = function(
parent = default_chromote_object(),
width = 992,
height = 1323,

@bersbersbers

bersbersbers commented Apr 7, 2022 •

Copy link
Copy Markdown
Contributor Author

A few more points in addition:

@bersbersbers

bersbersbers commented Apr 7, 2022 •

Copy link
Copy Markdown
Contributor Author

All theoretical considerations aside, a short test reveals that chromote already renders "small" (short, narrow) web sites according the dimensions of the initial session (probably because the HTML element fills the available viewport), and that remains true with this PR:

git clone https://github.com/bersbersbers/chromote/
cd chromote

export URL="https://cran.r-project.org/bin/"
export BRANCH="main"
# export BRANCH="snapshot_without_resize"

git checkout $BRANCH
Rscript -e 'devtools::install(); b <- chromote::ChromoteSession$new(); b$Page$navigate(Sys.getenv("URL")); Sys.sleep(1); b$screenshot("snap.png")'
exiftool snap.png | grep -E "Width|Height"
Image Width                     : 992
Image Height                    : 1323

I have not tested how shinytest2 may react to this change, but to me, this looks alright.

@wch

wch commented Apr 7, 2022

Copy link
Copy Markdown
Collaborator

All theoretical considerations aside, a short test reveals that chromote already renders "small" (short, narrow) web sites according the dimensions of the initial session (probably because the HTML element fills the available viewport), and that remains true with this PR:

The page you were using for testing is this one: https://cran.r-project.org/bin/
When I look at it in the web browser, the <body> takes the full height of the viewport. However, other pages don't work this way. For example, this shiny app: https://gallery.shinyapps.io/001-hello/?showcase=0

With this app, the body does not fill the height of the viewport. And currently, screenshots of that page result in a PNG that is 463 pixels tall.

library(chromote)
b <- chromote::ChromoteSession$new()
b$Page$navigate("https://gallery.shinyapps.io/001-hello/?showcase=0")
# Wait for a few seconds
b$screenshot()

@wch

wch commented Apr 7, 2022 •

Copy link
Copy Markdown
Collaborator

At least the width of current snapshots (992) is already larger than the minimum....

It is possible (though uncommon) for a web page to have the width set to a value other than the viewport width. For example, if you have <body style="width: 500px">, then the body width would be 500 pixels.

One thing that could be done is to use the clip parameter to clip the screenshot to the size of the body (if the body is smaller than the viewport).

The goal of the screenshot method is to get an image of the web page; if we are willing to capture areas that are outside of the viewport, then we should be willing to trim the image to match the web page.

@wch

wch commented Apr 7, 2022

Copy link
Copy Markdown
Collaborator

Now that we've been looking at the code more closely, it appears that the is.null(cliprect) code path may already clip the screenshot to the correct dimensions, although this needs to be tested.

@schloerke

Copy link
Copy Markdown
Collaborator

Been using this PR for the day and have not run into issues. I'd like to merge it as it solves a few issues in {shinytest2}

@schloerke

Copy link
Copy Markdown
Collaborator

@bersbersbers Has this PR resolved the pixel diffs being produced in {shinytest2}?

@schloerke

Copy link
Copy Markdown
Collaborator

Merging before feedback. My local usage as been good!

@schloerke schloerke changed the title Take screenshots beyond viewport without resizing (Closes #81) Take screenshots beyond viewport without resizing Apr 15, 2022
@schloerke
schloerke merged commit eaf3875 into rstudio:main Apr 15, 2022
schloerke added a commit that referenced this pull request Apr 15, 2022
* main:
  Take screenshots beyond viewport without resizing (#83)
  Quote browser path if not using Windows (#80)
  Do not `shQuote()` the browser path (#78)
  `$view()` should use local `Chrome` browser if available (#73)
  Add a reasonable `{chromote}` wall `timeout_` if Protocol `timeout` exists (#69)
  Remove `ChromoteSession$get_parent()` and lock `$protocol` (#76)
@bersbersbers

Copy link
Copy Markdown
Contributor Author

@bersbersbers Has this PR resolved the pixel diffs being produced in {shinytest2}?

Sorry for the late reply, I was OOO for a bit. With chromote v0.1.0 from CRAN and a current shinytest2 from GitHub, I have not seen this issue any more, no. A lot of other packages have been updated in the meantime, so I am not able to pinpoint it to this specific PR, but it seems likely to me that it did in fact resolve it.

@schloerke

Copy link
Copy Markdown
Collaborator

@bersbersbers Yay! Glad to hear

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.

Screenshots are white beyond the window size when using setDeviceMetricsOverride

3 participants