Skip to content

Instantly share code, notes, and snippets.

@rfaelens
Created January 8, 2020 14:22
Show Gist options
  • Save rfaelens/2ae17142a8c8def5779fdcac781da2b5 to your computer and use it in GitHub Desktop.
Save rfaelens/2ae17142a8c8def5779fdcac781da2b5 to your computer and use it in GitHub Desktop.
# Author: Ruben Faelens, Copyright: 2020
# Licensed under MIT license
install_chromedriver <- function(version, baseURL="https://chromedriver.storage.googleapis.com") {
if (!grepl("/$", baseURL))
baseURL <- paste0(baseURL, "/")
if(missing(version)) {
tmp <- tempfile()
utils::download.file(paste0(baseURL, "LATEST_RELEASE"), tmp)
version <- readLines(tmp, warn=FALSE)
unlink(tmp)
}
owd <- setwd(tempdir())
on.exit(setwd(owd), add = TRUE)
if (.Platform$OS.type == "windows") {
zipfile <- "chromedriver_win32.zip"
utils::download.file(paste0(baseURL, version, "/", zipfile), zipfile, mode = "wb")
utils::unzip(zipfile)
exec <- "chromedriver.exe"
}
success <- FALSE
dirs <- sub("PhantomJS", "chromedriver", webdriver:::phantom_paths())
for (destdir in dirs) {
dir.create(destdir, showWarnings = FALSE)
success <- file.copy(exec, destdir, overwrite = TRUE)
if (success)
break
}
unlink(c(zipfile, exec), recursive = TRUE)
if (!success)
stop("Unable to install PhantomJS to any of these dirs: ",
paste(dirs, collapse = ", "))
message("chromedriver has been installed to ", normalizePath(destdir))
invisible()
}
chromeDriver_find <- function() {
dirs <- sub("PhantomJS", "chromedriver", webdriver:::phantom_paths())
execs <- file.path(dirs, "chromedriver.exe")
for (exec in execs) {
if(file.exists(exec))
return(exec)
}
stop("chromedriver.exe not found")
}
shinyDriver_switchToChromedriver <- function(app, loadTimeout=5000, headless=TRUE) {
browser()
env <- shinytest:::phantom_env
phantomProcess <- env$process
phantomWeb <- app$.__enclos_env__$private$web
phantomWeb$delete()
phantomProcess$kill()
phantomPort <- app$.__enclos_env__$private$phantomPort
p <- processx::process$new(chromeDriver_find(),
args=c(paste0("--port=", phantomPort),
"--verbose"),
stdout = phantomProcess$get_output_file(),
stderr = phantomProcess$get_error_file()
)
cat( paste( readLines( p$get_output_file() ), collapse="\n") )
env$process <- p
web <- webdriver::Session$new(port = phantomPort)
### Modify capabilities to make it a *headless* run!
if(headless) {
web$delete()
response <- web$.__enclos_env__$private$makeRequest(
"NEW SESSION",
list(
desiredCapabilities = list(
browserName = "phantomjs",
driverName = "ghostdriver",
chromeOptions = list("args"= list("--headless" ))
)
)
)
web$.__enclos_env__$private$sessionId = response$sessionId
web$.__enclos_env__$private$parameters = response$value
web$setTimeout(implicit = 0)
web$getWindow()$setSize(992, 744)
}
app$.__enclos_env__$private$web <- web
web$setTimeout(implicit = 0)
web$go(app$.__enclos_env__$private$getShinyUrl())
js_file <- system.file("js", "shiny-tracer.js", package = "shinytest")
js <- shinytest:::read_utf8(js_file)
web$executeScript(js)
load_ok <- web$waitFor(
'window.shinytest && window.shinytest.ready === true',
timeout = loadTimeout
)
if (!load_ok) {
stop(
"Shiny app did not load in ", loadTimeout, "ms.\n",
format(app$getDebugLog())
)
}
app$.__enclos_env__$private$state <- "running"
#app$.__enclos_env__$private$setupDebugging(debug)
app$.__enclos_env__$private$shinyWorkerId <- web$executeScript(
'return Shiny.shinyapp.config.workerId'
)
app$.__enclos_env__$private$shinyTestSnapshotBaseUrl <- web$executeScript(
'if (Shiny.shinyapp.getTestSnapshotBaseUrl)
return Shiny.shinyapp.getTestSnapshotBaseUrl({ fullUrl:true });
else
return null;'
)
invisible(app)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment