tests/testthat/test-app-bookmark.R

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
    }
  })
}

shiny_app <- shinyApp(ui, server, enableBookmarking = "url")

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

  # 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 <- 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": {
  #
  #   }
  # }
})

Try the shinytest2 package in your browser

Any scripts or data that you put into this service are public.

shinytest2 documentation built on Jan. 10, 2026, 1:07 a.m.