Skip to content

Commit

Permalink
Delete stop_binary, check that package is loaded before taking screen…
Browse files Browse the repository at this point in the history
…shot
  • Loading branch information
siddhesh195 committed Sep 19, 2024
1 parent 87e3324 commit 1b81662
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 deletions.
45 changes: 24 additions & 21 deletions R/z_pages.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,30 @@ animint2pages <- function(plot.list, github_repo, owner=NULL, commit_message = "
stop(sprintf("Please run `install.packages('%s')` before using this function", pkg))
}
}
chrome.session <- chromote::ChromoteSession$new()
res <- animint2dir(plot.list, open.browser = FALSE, ...)
#Find available port and start server
portNum <- servr::random_port()
normDir <- normalizePath(res$out.dir, winslash = "/", mustWork = TRUE)
start_servr(serverDirectory = normDir, port = portNum, tmpPath = normDir)
Sys.sleep(3)
url <- sprintf("http://localhost:%d", portNum)
chrome.session$Page$navigate(url)
screenshot_path <- file.path(res$out.dir, "Capture.PNG")
screenshot_full <- file.path(res$out.dir, "Capture_full.PNG")
Sys.sleep(3)
## Capture screenshot
chrome.session$screenshot(screenshot_full, selector = ".plot_content")
image_raw <- magick::image_read(screenshot_full)
image_trimmed <- magick::image_trim(image_raw)
magick::image_write(image_trimmed, screenshot_path)
unlink(screenshot_full)
chrome.session$close()
# Stop the server
stop_server(normDir)

if(requireNamespace("chromote") && requireNamespace("magick")) {
chrome.session <- chromote::ChromoteSession$new()
res <- animint2dir(plot.list, open.browser = FALSE, ...)
#Find available port and start server
portNum <- servr::random_port()
normDir <- normalizePath(res$out.dir, winslash = "/", mustWork = TRUE)
start_servr(serverDirectory = normDir, port = portNum, tmpPath = normDir)
Sys.sleep(3)
url <- sprintf("http://localhost:%d", portNum)
chrome.session$Page$navigate(url)
screenshot_path <- file.path(res$out.dir, "Capture.PNG")
screenshot_full <- file.path(res$out.dir, "Capture_full.PNG")
Sys.sleep(3)
## Capture screenshot
chrome.session$screenshot(screenshot_full, selector = ".plot_content")
image_raw <- magick::image_read(screenshot_full)
image_trimmed <- magick::image_trim(image_raw)
magick::image_write(image_trimmed, screenshot_path)
unlink(screenshot_full)
chrome.session$close()
# Stop the server
stop_server(normDir)
}
all_files <- Sys.glob(file.path(res$out.dir, "*"))
file_info <- file.info(all_files)
to_post <- all_files[!(file_info$size == 0 | grepl("~$", all_files))]
Expand Down
15 changes: 1 addition & 14 deletions tests/testthat/helper-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ tests_run <- function(dir = ".", filter = NULL) {
#' @seealso \link{tests_run}
#' @export
tests_exit <- function() {
stop_binary()
Sys.unsetenv("ANIMINT_BROWSER")
res <- stop_server(tmpPath = find_test_path())
invisible(all(res))
Expand All @@ -365,26 +364,14 @@ tests_exit <- function() {
#' @param port port number to _attempt_ to run server on.
#' @param code R code to execute in a child session
#' @return port number of the successful attempt
run_servr <- function(directory = ".", port = 4848,
code = "servr::httd(dir='%s', port=%d)") {
run_servr <- function(directory, port, code) {
start_servr(directory, port, code, tmpPath = find_test_path())
}

# --------------------------
# Functions that are used in multiple places
# --------------------------

stop_binary <- function() {
if (exists("pJS")) pJS$stop()
# these methods are really queries to the server
# thus, if it is already shut down, we get some arcane error message
e <- try({
remDr$closeWindow()
remDr$closeServer()
}, silent = TRUE)
TRUE
}

# find the path to animint's testthat directory
find_test_path <- function(dir = ".") {
dir <- normalizePath(dir, winslash = "/", mustWork = TRUE)
Expand Down

0 comments on commit 1b81662

Please sign in to comment.