diff --git a/NEWS.md b/NEWS.md index a9e6a910..9af53dfb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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) @@ -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 diff --git a/tests/testthat/apps/bookmark/app.R b/tests/testthat/apps/bookmark/app.R new file mode 100644 index 00000000..30cf8fa1 --- /dev/null +++ b/tests/testthat/apps/bookmark/app.R @@ -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") diff --git a/tests/testthat/apps/bookmark/tests/testthat.R b/tests/testthat/apps/bookmark/tests/testthat.R new file mode 100644 index 00000000..7d25b5b9 --- /dev/null +++ b/tests/testthat/apps/bookmark/tests/testthat.R @@ -0,0 +1 @@ +shinytest2::test_app() diff --git a/tests/testthat/apps/bookmark/tests/testthat/_snaps/bookmark/001.json b/tests/testthat/apps/bookmark/tests/testthat/_snaps/bookmark/001.json new file mode 100644 index 00000000..1d0ff37d --- /dev/null +++ b/tests/testthat/apps/bookmark/tests/testthat/_snaps/bookmark/001.json @@ -0,0 +1,13 @@ +{ + "input": { + "._bookmark_": 0, + "caps": true, + "txt": "abcd" + }, + "output": { + "out": "ABCD" + }, + "export": { + + } +} diff --git a/tests/testthat/apps/bookmark/tests/testthat/_snaps/bookmark/001_.png b/tests/testthat/apps/bookmark/tests/testthat/_snaps/bookmark/001_.png new file mode 100644 index 00000000..7be77abb Binary files /dev/null and b/tests/testthat/apps/bookmark/tests/testthat/_snaps/bookmark/001_.png differ diff --git a/tests/testthat/apps/bookmark/tests/testthat/setup-shinytest2.R b/tests/testthat/apps/bookmark/tests/testthat/setup-shinytest2.R new file mode 100644 index 00000000..be65b4f0 --- /dev/null +++ b/tests/testthat/apps/bookmark/tests/testthat/setup-shinytest2.R @@ -0,0 +1,2 @@ +# Load application support files into testing environment +shinytest2::load_app_env() diff --git a/tests/testthat/apps/bookmark/tests/testthat/test-bookmark.R b/tests/testthat/apps/bookmark/tests/testthat/test-bookmark.R new file mode 100644 index 00000000..68c3dec6 --- /dev/null +++ b/tests/testthat/apps/bookmark/tests/testthat/test-bookmark.R @@ -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": { + # + # } + # } +}) diff --git a/vignettes/zzz-faq.Rmd b/vignettes/zzz-faq.Rmd index 3b6912ba..1c382a25 100644 --- a/vignettes/zzz-faq.Rmd +++ b/vignettes/zzz-faq.Rmd @@ -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": { + } +} +```