Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
^renv$
^renv\.lock$
^\renv$
^\renv\.lock$
^.*\.Rproj$
^\.Rproj\.user$
^Makefile$
Expand All @@ -7,3 +11,4 @@
^vignettes$
^pkgdown$
^\.github$
^\.renvignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
.Rhistory
.RData
docs
.Rprofile
/renv
/renv.lock
2 changes: 2 additions & 0 deletions .renvignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tests/testthat/example_test_dirs/mixed-toplevel/tests/testa.r
tests/testthat/example_test_dirs/nested/tests/shinytest/whatever.R
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
shinytest 1.4.0.9002
===============

* `ShinyDriver$getAllValues()`, `ShinyDriver$snapshot()`, and
`ShinyDriver$snapshotDownload()` give clear errors messages if the Shiny
app is no longer running (e.g. because you've trigged a `stopApp()`) (#192).

* `ShinyDriver$snapshotDownload()` gives a clear error message if the
`fileInput()` does not exist (#191)

* New `Widget$click()` method to click buttons (#325).

* New `ShinyDriver$waitForShiny()` that waits until Shiny is done computing
Expand Down
30 changes: 20 additions & 10 deletions R/app-get-all-values.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
# Note: This queries the server
sd_getAllValues <- function(self, private, input, output, export) {
"!DEBUG sd_getAllValues"
url <- private$getTestSnapshotUrl(input, output, export, format = "rds")

sd_getAllValues <- function(self, private, input, output, export,
exclude=NULL, stop_on_error=TRUE) {
self$logEvent("Getting all values")
tmpfile <- tempfile("shinytest_values", fileext = ".rds")
req <- httr::GET(url)
if (req$status_code != 200) {
stop("Unable to fetch all values from server. Is target app running with options(shiny.testmode=TRUE?)")
}
"!DEBUG sd_getAllValues"

writeBin(req$content, tmpfile)
url <- private$getTestSnapshotUrl(input, output, export, format = "rds")
req <- httr_get(url, stop_on_error)

tmpfile <- tempfile()
on.exit(unlink(tmpfile))
writeBin(req$content, tmpfile)
rObj <- readRDS(tmpfile)

# Remove any items specified in ignore
if(length(exclude)>0)
{
dropItems <- function(l, i) l[! names(l) %in% i]

rObj$input <- dropItems(rObj$input, exclude)
rObj$output <- dropItems(rObj$output, exclude)
rObj$export <- dropItems(rObj$export, exclude)
}

readRDS(tmpfile)
rObj
}
37 changes: 28 additions & 9 deletions R/shiny-driver.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,14 @@ ShinyDriver <- R6Class(
#' @param input,output,export Either `TRUE` to return all
#' input/output/exported values, or a character vector of specific
#' controls.
getAllValues = function(input = TRUE, output = TRUE, export = TRUE)
sd_getAllValues(self, private, input, output, export),
#' @exclude Names of objects to exclude from input, output, and export.
#' @param stop_on_error When `TRUE` (the default) error messages from the
#' shiny app are displayed and execution halts. When `FALSE` the HTTP
#' error code and message are included in the returned list.
getAllValues = function(input = TRUE, output = TRUE, export = TRUE,
exclude=NULL, stop_on_error=TRUE)
sd_getAllValues(self, private, input, output, export, exclude,
stop_on_error),

#' @description
#' Sends the specified keys to specific HTML element. Shortcut for
Expand Down Expand Up @@ -351,10 +357,17 @@ ShinyDriver <- R6Class(
#' @param filename Filename to use
#' @param screenshot Take a screenshot? Overrides value set by
#' `$snapshotInit()`
#' @exclude Names of objects to exclude from `input`, `output`, and `export.`
#' @param stop_on_error When `TRUE` (the default) error messages from the
#' shiny app are displayed and execution halts. When `FALSE` the HTTP
#' error code and message are included in the snapshot.
snapshot = function(items = NULL,
filename = NULL,
screenshot = NULL)
sd_snapshot(self, private, items, filename, screenshot),
screenshot = NULL,
exclude = NULL,
stop_on_error = TRUE)
sd_snapshot(self, private, items, filename, screenshot, exclude,
stop_on_error),


#' @description Deprecated
Expand All @@ -364,11 +377,17 @@ ShinyDriver <- R6Class(
},

#' @description
#' Download a snapshot. Generally, you should not call this function
#' yourself; it will be generated by [recordTest()] as needed.
#' @param id,filename Internal use only
snapshotDownload = function(id, filename = NULL)
sd_snapshotDownload(self, private, id, filename),
#' Snapshot a file download action. Generally, you should not call this
#' function yourself; it will be generated by [recordTest()] as needed.
#' @param id Output id of [shiny::downloadButton()]/[shiny::downloadLink()]
#' @param filename File name to save file to. The default, `NULL`,
#' generates an ascending sequence of names: `001.download`,
#' `002.download`, etc.
#' @param stop_on_error When `TRUE` (the default) error messages from the
#' shiny app are displayed and execution halts. When `FALSE` the HTTP
#' error code and message are stored in the download file.
snapshotDownload = function(id, filename = NULL, stop_on_error=TRUE)
sd_snapshotDownload(self, private, id, filename, stop_on_error),

#' @description Directory where app is located
getAppDir = function() {
Expand Down
39 changes: 29 additions & 10 deletions R/snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ sd_snapshotInit <- function(self, private, path, screenshot) {
private$snapshotScreenshot <- screenshot
}

sd_snapshot <- function(self, private, items, filename, screenshot)
sd_snapshot <- function(self, private, items, filename, screenshot, exclude,
stop_on_error=TRUE)
{
if (!is.list(items) && !is.null(items))
stop("'items' must be NULL or a list.")
Expand Down Expand Up @@ -50,7 +51,27 @@ sd_snapshot <- function(self, private, items, filename, screenshot)
# Take snapshot -------------------------------------------------------------
self$logEvent("Taking snapshot")
url <- private$getTestSnapshotUrl(items$input, items$output, items$export)
req <- httr::GET(url)
req <- httr_get(url, stop_on_error)

# Convert to text, then replace base64-encoded images with hashes of them.
content <- raw_to_utf8(req$content)
content <- hash_snapshot_image_data(content)

# Remove any items specified in ignore

@warnes warnes Aug 11, 2020 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementing the exclude arguments only involves this code block and adding the omit argument to the sd_snapshot and app$snapshot function definitions and handoff.

if(length(exclude)>0)
{
rObj <- jsonlite::fromJSON(txt=content)

dropItems <- function(l, i) l[! names(l) %in% i]

rObj$input <- dropItems(rObj$input, exclude)
rObj$output <- dropItems(rObj$output, exclude)
rObj$export <- dropItems(rObj$export, exclude)

content <- jsonlite::toJSON(rObj)
}

content <- jsonlite::prettify(content, indent = 2)

# For first snapshot, create -current snapshot dir.
if (private$snapshotCount == 1) {
Expand All @@ -60,10 +81,6 @@ sd_snapshot <- function(self, private, items, filename, screenshot)
dir.create(current_dir, recursive = TRUE)
}

# Convert to text, then replace base64-encoded images with hashes of them.
content <- raw_to_utf8(req$content)
content <- hash_snapshot_image_data(content)
content <- jsonlite::prettify(content, indent = 2)
write_utf8(content, file.path(current_dir, filename))

if (screenshot) {
Expand All @@ -73,10 +90,10 @@ sd_snapshot <- function(self, private, items, filename, screenshot)
}

# Invisibly return JSON content as a string
invisible(raw_to_utf8(req$content))
invisible(content)
}

sd_snapshotDownload <- function(self, private, id, filename) {
sd_snapshotDownload <- function(self, private, id, filename, stop_on_error=TRUE) {

current_dir <- paste0(self$getSnapshotDir(), "-current")

Expand All @@ -88,8 +105,10 @@ sd_snapshotDownload <- function(self, private, id, filename) {

# Find the URL to download from (the href of the <a> tag)
url <- self$findElement(paste0("#", id))$getAttribute("href")

req <- httr::GET(url)
if (identical(url, "")) {
stop("Download from '#", id, "' failed")
}
req <- httr_get(url, stop_on_error)

# For first snapshot, create -current snapshot dir.
if (private$snapshotCount == 1) {
Expand Down
30 changes: 30 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,33 @@ png_res_header_data <- as.raw(c(
on_ci <- function() {
isTRUE(as.logical(Sys.getenv("CI")))
}

httr_get <- function(url, stop_on_error=TRUE) {
pieces <- httr::parse_url(url)

if (!pingr::is_up(pieces$hostname, pieces$port)) {
stop("Shiny app is no longer running")
}

req <- httr::GET(url)
status <- httr::status_code(req)
if (status == 200) {
return(req)
}

if(stop_on_error)
{
cat("Query failed (", status, ")----------------------\n", sep = "")
cat(httr::content(req, "text"), "\n")
cat("----------------------------------------\n")
stop("Unable request data from server. HTTP Code", req$status_code, "\n",
httr::content(req, "text"))
} else {
new_content <- list(
status_code=req$status_code,
html=httr::content(req, "text")
)
return(jsonlite::toJSON(new_content))
}

}
40 changes: 34 additions & 6 deletions man/ShinyDriver.Rd

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

8 changes: 8 additions & 0 deletions tests/testthat/apps/stopApp/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
library(shiny)
ui <- fluidPage(
actionButton("quit", "quit")
)
server <- function(input, output, session) {
observeEvent(input$quit, stopApp())
}
shinyApp(ui, server)
37 changes: 37 additions & 0 deletions tests/testthat/recorded_tests/app-exclude/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
library(shiny)


ui <- fluidPage(
checkboxInput("chkbx", "Display Graph?", FALSE),
uiOutput("dynamic_output")
)

server <- function(input, output) {
output$dynamic_output <- renderUI({
if(!input$chkbx) {
return(NULL)
}
tagList(
sliderInput(inputId = "bins", label = "Number of bins:", min = 1, max = 50, value = 30),
plotOutput(outputId = "distPlot")
)
})

observe({
str(reactiveValuesToList(input))
})

# will not execute until `input$bins` is available
# `input$bins` is not available until `chkbx` is checked
output$distPlot <- renderPlot({
Sys.sleep(5)

x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}

shinyApp(ui = ui, server = server)
Loading