Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record the target id in a session #135

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ Config/testthat/edition: 3
Encoding: UTF-8
Language: en-US
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
SystemRequirements: Google Chrome or other Chromium-based browser.
chromium: chromium (rpm) or chromium-browser (deb)
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# chromote (development version)

* `ChromoteSession` now records the `targetId`. This eliminates one round-trip to the browser when viewing or closing a session, and will make it possible to re-start a closed session (#94).

* `ChromoteSession$screenshot()` gains an `options` argument that accepts a list of additional options to be passed to the Chrome Devtools Protocol's [`Page.captureScreenshot` method](https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-captureScreenshot). (#129)

* `ChromoteSession$screenshot()` will now infer the image format from the `filename` extension. Alternatively, you can specify the `format` in the list passed to `options`. (#130)
Expand Down
50 changes: 25 additions & 25 deletions R/chromote_session.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# This represents one _session_ in a Chromote object. Note that in the Chrome
# DevTools Protocol a session is a debugging interface connected to a
# _target_; a target is a browser window/tab, or an iframe. A single target
# can have more than one session connected to it.

#' ChromoteSession class
#'
#' @description
#' This represents one _session_ in a Chromote object. Note that in the Chrome
#' DevTools Protocol a session is a debugging session connected to a _target_,
#' which is a browser window/tab or an iframe.
#'
#' A single target can potentially have more than one session connected to it,
#' but this is not currently supported by chromote.
#'
#' @export
#' @param timeout_ Number of seconds for \pkg{chromote} to wait for a Chrome
#' DevTools Protocol response. If `timeout_` is [`rlang::missing_arg()`] and
Expand Down Expand Up @@ -76,9 +80,11 @@ ChromoteSession <- R6Class(
wait_ = FALSE
)$
then(function(value) {
private$target_id <- value$targetId
parent$Target$attachToTarget(value$targetId, flatten = TRUE, wait_ = FALSE)
})
} else {
private$target_id <- targetId
p <- parent$Target$attachToTarget(targetId, flatten = TRUE, wait_ = FALSE)
}

Expand Down Expand Up @@ -157,11 +163,9 @@ ChromoteSession <- R6Class(
#' if (interactive()) b$view()
#' ```
view = function() {
tid <- self$Target$getTargetInfo()$targetInfo$targetId

# A data frame of targets, one row per target.
info <- fromJSON(self$parent$url("/json"))
path <- info$devtoolsFrontendUrl[info$id == tid]
path <- info$devtoolsFrontendUrl[info$id == private$target_id]
if (length(path) == 0) {
stop("Target info not found.")
}
Expand All @@ -188,15 +192,12 @@ ChromoteSession <- R6Class(
#' when the `ChromoteSession` is closed. Otherwise, block until the
#' `ChromoteSession` has closed.
close = function(wait_ = TRUE) {
p <- self$Target$getTargetInfo(wait_ = FALSE)
p <- p$then(function(target) {
tid <- target$targetInfo$targetId
# Even if this session calls Target.closeTarget, the response from
# the browser is sent without a sessionId. In order to wait for the
# correct browser response, we need to invoke this from the parent's
# browser-level methods.
self$parent$protocol$Target$closeTarget(tid, wait_ = FALSE)
})
# Even if this session calls Target.closeTarget, the response from
# the browser is sent without a sessionId. In order to wait for the
# correct browser response, we need to invoke this from the parent's
# browser-level methods.
p <- self$parent$protocol$Target$closeTarget(private$target_id, wait_ = FALSE)

p <- p$then(function(value) {
if (isTRUE(value$success)) {
self$mark_closed()
Expand Down Expand Up @@ -421,18 +422,16 @@ ChromoteSession <- R6Class(

#' @description
#' Retrieve the session id
#'
#' ## Examples
#'
#' ```r
#' b <- ChromoteSession$new()
#' b$get_session_id()
#' #> [1] "05764F1D439F4292497A21C6526575DA"
#' ```
get_session_id = function() {
private$session_id
},

#' @description
#' Retrieve the target id
get_target_id = function() {
private$target_id
},

#' @description
#' Wait for a Chromote Session to finish. This method will block the R
#' session until the provided promise resolves. The loop from
Expand Down Expand Up @@ -555,6 +554,7 @@ ChromoteSession <- R6Class(

private = list(
session_id = NULL,
target_id = NULL,
is_active_ = NULL,
event_manager = NULL,
pixel_ratio = NULL,
Expand Down
23 changes: 15 additions & 8 deletions man/ChromoteSession.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading