Skip to content
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
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

* Add support for _not_ recording the screen size when recording a test (#223)


## Bug / Improvements

* Fix set of bugs found by @daattali including test files should be opened in the IDE after recording and test and replace missing images in the website (#199)
Expand All @@ -39,6 +38,8 @@

* Added support for `AppDriver$stop(timeout=)`. The default timeout when sending `SIGINT`, `SIGTERM`, and `SIGKILL` signals to the `{shiny}` process is now 500ms. However, if `{covr}` is running, the default timeout is 20,000 ms to allow time for the report to generate. (#259)

* Add example and FAQ entry on how to test bookmarking (#262)


# shinytest2 0.1.1

Expand Down
21 changes: 21 additions & 0 deletions tests/testthat/apps/bookmark/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
library(shiny)

ui <- function(request) {
fluidPage(
textInput("txt", "Enter text"),
checkboxInput("caps", "Capitalize"),
verbatimTextOutput("out"),
bookmarkButton()
)
}
server <- function(input, output, session) {
output$out <- renderText({
if (input$caps) {
toupper(input$txt)
} else {
input$txt
}
})
}

shinyApp(ui, server, enableBookmarking = "url")
1 change: 1 addition & 0 deletions tests/testthat/apps/bookmark/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shinytest2::test_app()
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"input": {
"._bookmark_": 0,
"caps": true,
"txt": "abcd"
},
"output": {
"out": "ABCD"
},
"export": {

}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Load application support files into testing environment
shinytest2::load_app_env()
28 changes: 28 additions & 0 deletions tests/testthat/apps/bookmark/tests/testthat/test-bookmark.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
library(shinytest2)

test_that("Bookmark works", {
# Start local app in the background in test mode
bg_app <- shinytest2::AppDriver$new()

# Capture the background app's URL and add appropriate query parameters
bookmark_url <- paste0(bg_app$get_url(), "?_inputs_&txt=%22abcd%22&caps=true")
# Open the bookmark URL in a new AppDriver object
app <- shinytest2::AppDriver$new(bookmark_url)

# Run your tests on the bookmarked `app`
app$expect_values()
## File: _snaps/bookmark/001.json
# {
# "input": {
# "._bookmark_": 0,
# "caps": true,
# "txt": "abcd"
# },
# "output": {
# "out": "ABCD"
# },
# "export": {
#
# }
# }
})
35 changes: 35 additions & 0 deletions vignettes/zzz-faq.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,38 @@ Input values can face the same issues, and they can also be modified with `shiny
For code completeness, you should call `app$stop()` at the end of your tests to shut down the background Shiny process and current Chromote Session.

If your R session is starting to feel more sluggish, you may want to call `app$stop()` manually. Or if you are programmatically creating many instances of `AppDriver`, you may want to call `app$stop()` when the specific test is complete via `withr::defer(app$stop())` right after initialization.

## How can I open the test to see if bookmarks are working?

Testing Shiny bookmarks requires the browser to be loaded with specific query parameters. Luckily, you can use `{shinytest2}` to help you achieve this by running a local `AppDriver` and another `AppDriver` that uses a url with your specific query parameters! ([Original code](https://github.com/rstudio/shinytest2/issues/219#issuecomment-1149162328), [Working example](https://github.com/rstudio/shinytest2/blob/v0.2.0/tests/testthat/apps/download))

File: `tests/testthat//test-bookmark.R`
```{r}
test_that("Bookmark works", {
# Start local app in the background in test mode
bg_app <- shinytest2::AppDriver$new("path/to/shiny/app")
# Capture the background app's URL and add appropriate query parameters
bookmark_url <- paste0(bg_app$get_url(), "?_inputs_&n=10")
# Open the bookmark URL in a new AppDriver object
app <- shinytest2::AppDriver$new(bookmark_url)

# Run your tests on the bookmarked `app`
app$expect_values()
})
```

File: `tests/testthat/_snaps/bookmark/001.json`
```json
{
"input": {
"._bookmark_": 0,
"caps": true,
"txt": "abcd"
},
"output": {
"out": "ABCD"
},
"export": {
}
}
```