Skip to content

Commit

Permalink
add scrolling functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ashbythorpe committed Dec 14, 2024
1 parent 2c8b914 commit c5392ad
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
74 changes: 74 additions & 0 deletions R/global_actions.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,80 @@ reload <- function(timeout = NULL, session = NULL) {
#' @export
refresh <- reload

#' Scroll along the page
#'
#' @description
#' `scroll_to()` scrolls the page to the specified coordinates.
#'
#' `scroll_by()` scrolls the page by the specified amount.
#'
#' @param top The vertical scroll position/offset, in pixels.
#' @param left The horizontal scroll position/offset, in pixels.
#' @param session A `selenider_session` object. If not specified, the global
#' session object (the result of [get_session()]) is used.
#'
#' @family global actions
#'
#' @examplesIf selenider::selenider_available()
#' session <- selenider_session()
#'
#' open_url("https://r-project.org")
#'
#' scroll_to(100, 100)
#'
#' # Scrolls an additional 100 pixels down
#' scroll_by(100)
#'
#' @export
scroll_to <- function(top = 0, left = 0, session = NULL) {
check_number_whole(top)
check_number_whole(left)
check_class(session, "selenider_session", allow_null = TRUE)

if (is.null(session)) {
session <- get_session(.env = caller_env())
}

driver <- session$driver

execute_js_expr_internal(
paste0("window.scrollTo(", left, ",", top, ");"),
session = session$session,
driver = driver
)
}

#' @rdname scroll_to
#'
#' @export
scroll_by <- function(top = 0, left = 0, session = NULL) {
check_number_whole(top)
check_number_whole(left)
check_class(session, "selenider_session", allow_null = TRUE)

if (is.null(session)) {
session <- get_session(.env = caller_env())
}

driver <- session$driver

if (session$session == "chromote") {
driver$Input$dispatchMouseEvent(
type = "mouseWheel",
x = 0,
y = 0,
deltaX = left,
deltaY = top,
)
} else {
execute_js_expr_internal(
paste0("window.scrollBy(", left, ",", top, ");"),
session = session$session,
driver = driver
)
}
}

#' Take a screenshot of the current page
#'
#' Take a screenshot of the current session state, saving this image to a file.
Expand Down
13 changes: 13 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,19 @@ uses_chromote <- function(x) {
inherits(x, "ChromoteSession")
}

execute_js_expr_internal <- function(x, session, driver) {
if (session == "chromote") {
driver$Runtime$evaluate(
expression = x,
returnByValue = TRUE
)
} else if (session == "selenium") {
driver$execute_script(x)
} else {
driver$executeScript(x)
}
}

execute_js_fn_on <- function(fn, x, session, driver) {
if (session == "chromote") {
script <- paste0("function() { return (", fn, ")(this) }")
Expand Down

0 comments on commit c5392ad

Please sign in to comment.