### Install shinytest2 Source: https://github.com/rstudio/shinytest2/blob/main/README.md Installation commands for the released version from CRAN or the development version from GitHub. ```r # Install the released version from CRAN install.packages("shinytest2") # Or the development version from GitHub: # install.packages("devtools") devtools::install_github("rstudio/shinytest2") ``` -------------------------------- ### Setup Testing Infrastructure Source: https://context7.com/rstudio/shinytest2/llms.txt Initialize the necessary files and configurations for shinytest2 in a project. ```r # Full setup (creates runner, setup file, gitignore entries) shinytest2::use_shinytest2() # Setup for a specific app directory shinytest2::use_shinytest2(app_dir = "inst/shinyapp") # Selective setup shinytest2::use_shinytest2( runner = TRUE, # Create tests/testthat.R setup = TRUE, # Create tests/testthat/setup-shinytest2.R ignore = TRUE, # Add *.new.png to .gitignore package = TRUE # Add shinytest2 to DESCRIPTION Suggests ) # Create a new test file shinytest2::use_shinytest2_test() shinytest2::use_shinytest2_test(open = TRUE) # Open in editor ``` -------------------------------- ### Example Workflow: Test app w/ DESCRIPTION Source: https://github.com/rstudio/shinytest2/blob/main/actions/test-app/README.md This is an example GitHub Actions workflow for testing a Shiny application that uses a DESCRIPTION file for dependency management. It sets up R, dependencies, and runs the test-app action. ```yaml # Workflow derived from https://github.com/rstudio/shinytest2/tree/main/actions/test-app/example-test-app-description.yaml # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: branches: [main, master] pull_request: branches: [main, master] name: Test app w/ DESCRIPTION jobs: test-app: runs-on: ${{ matrix.config.os }} name: ${{ matrix.config.os }} (${{ matrix.config.r }}) strategy: fail-fast: false matrix: config: - {os: ubuntu-latest, r: release} env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} R_KEEP_PKG_SOURCE: yes steps: - uses: actions/checkout@v2 - uses: r-lib/actions/setup-pandoc@v2 - uses: r-lib/actions/setup-r@v2 with: r-version: ${{ matrix.config.r }} http-user-agent: ${{ matrix.config.http-user-agent }} use-public-rspm: true - uses: r-lib/actions/setup-r-dependencies@v2 with: extra-packages: shinytest2 - uses: rstudio/shinytest2/actions/test-app@actions/v1 with: app-dir: "." ``` -------------------------------- ### Install test-app-description Workflow Source: https://github.com/rstudio/shinytest2/blob/main/actions/test-app/README.md Use this R code snippet with usethis to install a GitHub Actions workflow for testing Shiny apps with DESCRIPTION file dependencies. ```r usethis::use_github_action( url = "https://github.com/rstudio/shinytest2/raw/main/actions/test-app/example-test-app-descrption.yaml", save_as = "test-app-description.yaml" ) ``` -------------------------------- ### Install GitHub Action Workflow Source: https://github.com/rstudio/shinytest2/blob/main/actions/test-app/README.md Use this command to add the test-app-package workflow to your repository. ```r usethis::use_github_action( url = "https://github.com/rstudio/shinytest2/raw/main/actions/test-app/example-test-app-package.yaml", save_as = "test-app-package.yaml" ) ``` -------------------------------- ### Install shinytest2 dependencies Source: https://github.com/rstudio/shinytest2/blob/main/cran-comments.md Run this command to install necessary dependencies for running tests in shinytest2. ```r shinytest::installDependencies() ``` -------------------------------- ### Example Workflow: Test app w/ {renv} Source: https://github.com/rstudio/shinytest2/blob/main/actions/test-app/README.md This is an example GitHub Actions workflow for testing a Shiny application that uses renv for dependency management. It includes steps for checking out code, setting up R, renv, and running the test-app action. ```yaml # Workflow derived from https://github.com/rstudio/shinytest2/tree/main/actions/test-app/example-test-app-renv.yaml # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: branches: [main, master] pull_request: branches: [main, master] name: Test app w/ {renv} jobs: test-app: runs-on: ${{ matrix.config.os }} name: ${{ matrix.config.os }} (${{ matrix.config.r }}) strategy: fail-fast: false matrix: config: - {os: ubuntu-latest, r: release} env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} R_KEEP_PKG_SOURCE: yes steps: - uses: actions/checkout@v2 - uses: r-lib/actions/setup-pandoc@v2 - uses: r-lib/actions/setup-r@v2 with: r-version: ${{ matrix.config.r }} http-user-agent: ${{ matrix.config.http-user-agent }} use-public-rspm: true - uses: r-lib/actions/setup-renv@v2 - uses: rstudio/shinytest2/actions/test-app@actions/v1 with: app-dir: "." ``` -------------------------------- ### Install test-app-renv Workflow Source: https://github.com/rstudio/shinytest2/blob/main/actions/test-app/README.md Use this R code snippet with usethis to install a GitHub Actions workflow for testing Shiny apps where dependencies are managed by renv. ```r usethis::use_github_action( url = "https://github.com/rstudio/shinytest2/raw/main/actions/test-app/example-test-app-renv.yaml", save_as = "test-app-renv.yaml" ) ``` -------------------------------- ### use_shinytest2 - Setup Testing Infrastructure Source: https://context7.com/rstudio/shinytest2/llms.txt Initializes shinytest2 testing configuration for a Shiny application. ```APIDOC ## use_shinytest2 ### Description Sets up the necessary files and configuration for shinytest2 in a project directory. ### Parameters #### Request Body - **app_dir** (string) - Optional - Directory of the Shiny app. - **runner** (boolean) - Optional - Create test runner. - **setup** (boolean) - Optional - Create setup file. ``` -------------------------------- ### Get and Use Platform Variants Source: https://context7.com/rstudio/shinytest2/llms.txt Retrieve the current platform variant string or customize it by toggling OS name and R version inclusion. ```r # Get current platform variant variant <- platform_variant() # Returns: "mac-4.3" or "linux-4.2" or "windows-4.3" # Use in AppDriver app <- AppDriver$new( "path/to/app", variant = platform_variant() ) # Custom variant components platform_variant(os_name = TRUE, r_version = FALSE) # Returns: "mac" platform_variant(os_name = FALSE, r_version = TRUE) # Returns: "4.3" ``` -------------------------------- ### Test Shiny Application States and Interactions Source: https://context7.com/rstudio/shinytest2/llms.txt Examples of using AppDriver to verify initial states, handle form submissions, process file uploads, and validate error messages in a Shiny application. ```R library(shinytest2) library(testthat) test_that("Initial app state is correct", { app <- AppDriver$new( test_path("apps/myapp"), name = "myapp", variant = platform_variant() ) # Verify initial values app$expect_values() app$stop() }) ``` ```R test_that("User can submit form", { app <- AppDriver$new(test_path("apps/myapp")) # Fill out form app$set_inputs( name = "Jane Doe", email = "jane@example.com", department = "Engineering" ) # Submit form app$click(input = "submit") # Wait for confirmation app$wait_for_value(output = "confirmation") # Verify success message confirmation_text <- app$get_text("#confirmation") expect_match(confirmation_text, "Successfully submitted") # Snapshot final state app$expect_values(name = "after_submit") app$stop() }) ``` ```R test_that("File upload processes correctly", { app <- AppDriver$new(test_path("apps/myapp")) # Upload test file app$upload_file(data_file = test_path("data/sample.csv")) # Wait for processing app$wait_for_idle(timeout = 10000) # Verify data was loaded row_count <- app$get_value(output = "row_count") expect_equal(row_count, "100 rows") app$stop() }) ``` ```R test_that("Error handling works", { app <- AppDriver$new(test_path("apps/myapp")) # Trigger validation error app$set_inputs(email = "not-an-email") app$click(input = "submit") # Check error is displayed error_text <- app$get_text(".validation-error") expect_match(error_text, "valid email") app$stop() }) ``` -------------------------------- ### AppDriver - Create and Control a Shiny App Test Driver Source: https://context7.com/rstudio/shinytest2/llms.txt The AppDriver R6 class is the primary interface for testing Shiny applications. It starts a Shiny app in a background R process and connects a headless Chrome browser to simulate user interactions and capture application state. ```APIDOC ## AppDriver - Create and Control a Shiny App Test Driver ### Description The `AppDriver` R6 class is the primary interface for testing Shiny applications. It starts a Shiny app in a background R process and connects a headless Chrome browser to simulate user interactions and capture application state. ### Method `AppDriver$new(...) ### Parameters #### Path Parameters - **app_dir** (string) - Required - Path to the Shiny app directory. - **name** (string) - Optional - Prefix for snapshot files. - **variant** (any) - Optional - Platform-specific snapshots. - **seed** (numeric) - Optional - Random seed for reproducibility. - **load_timeout** (numeric) - Optional - Max time to wait for app load (ms). - **timeout** (numeric) - Optional - Default timeout for operations (ms). - **height** (numeric) - Optional - Browser window height. - **width** (numeric) - Optional - Browser window width. - **view** (boolean) - Optional - Open browser for debugging. - **check_names** (boolean) - Optional - Warn about duplicate input/output IDs. ### Request Example ```R library(shinytest2) # Create an AppDriver from a Shiny app directory app <- AppDriver$new( app_dir = "path/to/app", name = "myapp", variant = platform_variant(), seed = 12345, load_timeout = 15000, timeout = 4000, height = 800, width = 1200, view = FALSE, check_names = TRUE ) # Create from a Shiny app object directly library(shiny) shiny_app <- shinyApp( ui = fluidPage( numericInput("n", "N", value = 10), textOutput("result") ), server = function(input, output) { output$result <- renderText({ input$n * 2 }) } ) app <- AppDriver$new(shiny_app) # Create from a function (useful for package testing) app <- AppDriver$new(function() { library(mypackage) mypackage::run_app() }) # Always stop the app when done app$stop() ``` ### Response #### Success Response (200) - **AppDriver object** - An instance of the AppDriver R6 class. #### Response Example ```R # AppDriver object is returned upon successful creation ``` ``` -------------------------------- ### Retrieve App State Values Source: https://context7.com/rstudio/shinytest2/llms.txt Get current values of inputs, outputs, and exported test values from a running Shiny application. Can retrieve all values, specific categories, or individual named values. ```R library(shiny) shiny_app <- shinyApp( ui = fluidPage( numericInput("A", "A", 3), numericInput("B", "B", 4), verbatimTextOutput("C") ), server = function(input, output) { c_squared <- reactive({ input$A^2 + input$B^2 }) output$C <- renderText({ sqrt(c_squared()) }) # Export internal reactive values for testing exportTestValues( c_squared = { c_squared() } ) } ) app <- AppDriver$new(shiny_app) # Get all values (inputs, outputs, exports) all_values <- app$get_values() # Returns: list(input = list(A = 3, B = 4), # output = list(C = "5"), # export = list(c_squared = 25)) # Get specific categories input_values <- app$get_values(input = TRUE) output_values <- app$get_values(output = TRUE) export_values <- app$get_values(export = TRUE) # Get specific named values specific_values <- app$get_values(input = "A", output = "C") # Get a single value (shorthand) a_value <- app$get_value(input = "A") # Returns: 3 c_output <- app$get_value(output = "C") # Returns: "5" app$stop() ``` -------------------------------- ### Build and check package Source: https://github.com/rstudio/shinytest2/blob/main/CLAUDE.md Standard development workflow for building documentation, checking package integrity, and reloading code. ```r # Load package for development devtools::load_all() # Check package devtools::check() # Build documentation devtools::document() # Build website pkgdown::build_site() ``` -------------------------------- ### Test File Downloads with expect_download Source: https://context7.com/rstudio/shinytest2/llms.txt Use expect_download to test downloadButton/downloadLink outputs by downloading files and comparing them against snapshots. It supports explicit text comparison and custom snapshot names. ```r library(shinytest2) library(testthat) test_that("Download produces expected file", { app <- AppDriver$new("path/to/app") # Download and snapshot (auto-detects text vs binary) app$expect_download("download_csv") # Explicit text comparison app$expect_download( "download_report", compare = testthat::compare_file_text ) # Custom snapshot name app$expect_download("download_data", name = "exported_data.csv") app$stop() }) # Get download without snapshotting download_path <- app$get_download("download_csv") # Returns: "/tmp/RtmpXXX/rock.csv" # Save to specific location app$get_download("download_csv", filename = "output/my_data.csv") ``` -------------------------------- ### Record Interactive Tests Source: https://context7.com/rstudio/shinytest2/llms.txt Launch the interactive recorder to capture user actions and generate test scripts. ```r # Record tests for an app in the current directory shinytest2::record_test() # Record for a specific app shinytest2::record_test("path/to/app") # Record with specific options shinytest2::record_test( app = "inst/shinyapp", name = "mytest", seed = 12345, load_timeout = 30000, test_file = "test-custom-name.R", record_screen_size = TRUE, run_test = TRUE # Run test after recording ) # Don't run test automatically after recording shinytest2::record_test( "path/to/app", run_test = FALSE, open_test_file = TRUE ) ``` -------------------------------- ### Basic Usage of test-app Action Source: https://github.com/rstudio/shinytest2/blob/main/actions/test-app/README.md Use this step in your GitHub Actions workflow to test a single Shiny application. Specify the application directory using the 'app-dir' input. ```yaml - uses: rstudio/shinytest2/actions/test-app@actions/v1 with: app-dir: | dir/to/app ``` -------------------------------- ### expect_download - Test File Downloads Source: https://context7.com/rstudio/shinytest2/llms.txt Tests downloadButton/downloadLink outputs by downloading the file and comparing it against a saved snapshot. ```APIDOC ## expect_download ### Description Tests downloadButton/downloadLink outputs by downloading the file and comparing it against a saved snapshot. ### Parameters #### Query Parameters - **output_id** (character) - Required - The ID of the download button. - **compare** (function) - Optional - Comparison function. - **name** (character) - Optional - Custom snapshot name. ``` -------------------------------- ### expect_screenshot - Visual Regression Testing Source: https://context7.com/rstudio/shinytest2/llms.txt Takes a PNG screenshot of the application and compares it against a saved baseline image. ```APIDOC ## expect_screenshot ### Description Takes a PNG screenshot of the application (or a specific element) and compares it against a saved baseline image. Supports threshold-based fuzzy matching. ### Parameters #### Query Parameters - **selector** (character) - Optional - CSS selector for the element to capture. - **delay** (numeric) - Optional - Delay in seconds before taking the screenshot. - **threshold** (numeric) - Optional - Threshold for fuzzy matching. - **kernel_size** (numeric) - Optional - Kernel size for fuzzy matching. - **name** (character) - Optional - Custom name for the screenshot. ``` -------------------------------- ### record_test - Interactive Test Recording Source: https://context7.com/rstudio/shinytest2/llms.txt Launches an interactive recorder that captures user actions and generates test code automatically. ```APIDOC ## record_test ### Description Starts the interactive recording session for a Shiny application. ### Parameters #### Request Body - **app** (string) - Optional - Path to the Shiny app. - **name** (string) - Optional - Name of the test. - **run_test** (boolean) - Optional - Whether to run the test after recording. ``` -------------------------------- ### Run package tests Source: https://github.com/rstudio/shinytest2/blob/main/CLAUDE.md Execute tests using devtools or testthat. ```r # Run all package tests devtools::test() # Run specific test file testthat::test_file("tests/testthat/test-app-driver.R") # Run tests with filter devtools::test(filter = "screenshot") ``` -------------------------------- ### Manage Browser Window Size Source: https://context7.com/rstudio/shinytest2/llms.txt Control browser dimensions for responsive design testing and wait for re-renders after resizing. ```r app <- AppDriver$new("path/to/app", height = 800, width = 1200) # Get current window size size <- app$get_window_size() # Returns: list(width = 1200, height = 800) # Set new window size and wait for re-render app$set_window_size(width = 375, height = 667) # Mobile size # Set size without waiting app$set_window_size(width = 1920, height = 1080, wait = FALSE) app$wait_for_idle() # Test responsive design app$set_window_size(width = 768, height = 1024) # Tablet app$expect_screenshot(name = "tablet_view") app$set_window_size(width = 1920, height = 1080) # Desktop app$expect_screenshot(name = "desktop_view") app$stop() ``` -------------------------------- ### Load Application Support Files Source: https://context7.com/rstudio/shinytest2/llms.txt Load Shiny application helper files into the testing environment using scoped or permanent loading functions. ```r library(shinytest2) library(testthat) # For package testing: temporary scoped loading test_that("Module function works correctly", { # Load app's R/ folder temporarily local_app_support(test_path("apps/myapp")) # Now app helper functions are available result <- my_helper_function(x = 10) expect_equal(result, 20) }) # Support files automatically unloaded after test # Explicit scope with with_app_support test_that("Another test", { with_app_support(test_path("apps/myapp"), { # Helper functions available here expect_true(validate_input("test")) }) # Helper functions no longer available here }) # For standalone apps: permanent loading in setup-shinytest2.R # File: tests/testthat/setup-shinytest2.R shinytest2::load_app_support(test_path("../..")) ``` -------------------------------- ### Testing Multiple Apps with test-app Action Source: https://github.com/rstudio/shinytest2/blob/main/actions/test-app/README.md To test multiple Shiny applications, provide a newline-separated list of directories to the 'app-dir' input. ```yaml - uses: rstudio/shinytest2/actions/test-app@actions/v1 with: app-dir: | dir/to/app1 dir/to/app2 ``` -------------------------------- ### Migrate from shinytest Source: https://context7.com/rstudio/shinytest2/llms.txt Automate the migration of test files from the legacy shinytest package to the shinytest2 format. ```r # Migrate app tests from shinytest to shinytest2 shinytest2::migrate_from_shinytest( app_dir = "path/to/app", clean = TRUE, # Remove old shinytest files include_expect_screenshot = FALSE # Don't include screenshot tests ) # Keep old files for comparison shinytest2::migrate_from_shinytest( app_dir = "path/to/app", clean = FALSE, quiet = TRUE ) ``` -------------------------------- ### GitHub Action Workflow Configuration Source: https://github.com/rstudio/shinytest2/blob/main/actions/test-app/README.md The YAML configuration for running tests on a Shiny application within a package structure. ```yaml # Workflow derived from https://github.com/rstudio/shinytest2/tree/main/actions/test-app/example-test-app-package.yaml # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: branches: [main, master] pull_request: branches: [main, master] name: Test package app jobs: test-package-app: runs-on: ${{ matrix.config.os }} name: ${{ matrix.config.os }} (${{ matrix.config.r }}) strategy: fail-fast: false matrix: config: - {os: ubuntu-latest, r: release} env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} R_KEEP_PKG_SOURCE: yes steps: - uses: actions/checkout@v2 - uses: r-lib/actions/setup-pandoc@v2 - uses: r-lib/actions/setup-r@v2 with: r-version: ${{ matrix.config.r }} http-user-agent: ${{ matrix.config.http-user-agent }} use-public-rspm: true - uses: r-lib/actions/setup-r-dependencies@v2 with: extra-packages: local::. shinytest2 - uses: rstudio/shinytest2/actions/test-app@actions/v1 with: app-dir: "." ``` -------------------------------- ### Run Application Tests Source: https://context7.com/rstudio/shinytest2/llms.txt Execute testthat suites for Shiny applications. ```r # In tests/testthat.R: shinytest2::test_app() # Test app in a specific directory shinytest2::test_app("path/to/app") # Run specific test file only shinytest2::test_app(filter = "shinytest2") # With custom reporter shinytest2::test_app(reporter = testthat::ProgressReporter$new()) ``` -------------------------------- ### Create AppDriver Instance Source: https://context7.com/rstudio/shinytest2/llms.txt Instantiate an AppDriver to control a Shiny application. Can be created from a directory, a Shiny app object, or a function. Ensure to stop the app when done. ```R library(shinytest2) # Create an AppDriver from a Shiny app directory app <- AppDriver$new( app_dir = "path/to/app", name = "myapp", # Prefix for snapshot files variant = platform_variant(), # Platform-specific snapshots seed = 12345, # Random seed for reproducibility load_timeout = 15000, # Max time to wait for app load (ms) timeout = 4000, # Default timeout for operations (ms) height = 800, # Browser window height width = 1200, # Browser window width view = FALSE, # Open browser for debugging check_names = TRUE # Warn about duplicate input/output IDs ) # Create from a Shiny app object directly library(shiny) shiny_app <- shinyApp( ui = fluidPage( numericInput("n", "N", value = 10), textOutput("result") ), server = function(input, output) { output$result <- renderText({ input$n * 2 }) } ) app <- AppDriver$new(shiny_app) # Create from a function (useful for package testing) app <- AppDriver$new(function() { library(mypackage) mypackage::run_app() }) # Always stop the app when done app$stop() ``` -------------------------------- ### Execute JavaScript in Browser Source: https://context7.com/rstudio/shinytest2/llms.txt Interact with the browser context using get_js to return values or run_js for side-effect execution. ```r app <- AppDriver$new("path/to/app") # Get a value from JavaScript title <- app$get_js("document.title") # Returns: "My Shiny App" # Execute JavaScript and get result result <- app$get_js("1 + 1") # Returns: 2 # Wait for a Promise to resolve data <- app$get_js(" fetch('/api/data') .then(response => response.json()) ") # Run JavaScript without getting result app$run_js("console.log('Test started')") # Set up JavaScript state for testing app$run_js("window.testCounter = 0") app$click(input = "increment") count <- app$get_js("window.testCounter") # Execute JavaScript from a file app$run_js(file = "tests/testthat/helpers/setup.js") # With escaped arguments selector <- "#myElement" app$get_js(paste0( "document.querySelector(", jsonlite::toJSON(selector, auto_unbox = TRUE), ").textContent" )) app$stop() ``` -------------------------------- ### Run package tests Source: https://github.com/rstudio/shinytest2/blob/main/cran-comments.md Execute the test suite for the shinytest2 package using testthat. ```r library(testthat) library(shinytest2) test_check("shinytest2") ``` -------------------------------- ### Perform interactive testing Source: https://github.com/rstudio/shinytest2/blob/main/CLAUDE.md Initialize an AppDriver instance to inspect and manipulate a Shiny application during development. ```r # Create an AppDriver for interactive testing library(shinytest2) app <- AppDriver$new("path/to/app") app$view() # Opens browser for visual inspection app$get_values() app$set_inputs(input_name = value) app$stop() ``` -------------------------------- ### Verify Greeting Output Source: https://github.com/rstudio/shinytest2/blob/main/tests/testthat/apps/hello/tests/testthat/_snaps/mac-4.5/app.md This snippet shows the expected output of a basic Shiny application test. It verifies that the greeting message 'Hello Hadley!' is rendered correctly in the HTML. ```text "Hello Hadley!" ``` ```html "
Hello Hadley!
" ``` -------------------------------- ### Verify Greeting Output Source: https://github.com/rstudio/shinytest2/blob/main/tests/testthat/_snaps/mac/app-hello.md This snippet shows the expected output of a basic Shiny application test. It verifies that the greeting message 'Hello Hadley!' is rendered correctly in the HTML. ```text "Hello Hadley!" ``` -------------------------------- ### Synchronization Methods: wait_for_idle, wait_for_value, wait_for_js Source: https://context7.com/rstudio/shinytest2/llms.txt These methods are crucial for synchronizing tests by waiting for specific application states. Use wait_for_idle for general inactivity, wait_for_value to monitor input/output changes, and wait_for_js for custom JavaScript conditions. ```r app <- AppDriver$new("path/to/app") # Wait for app to be idle (no reactive updates) for 500ms app$wait_for_idle(duration = 500) # Wait with custom timeout app$wait_for_idle(duration = 200, timeout = 10000) # Wait for a specific output to have a non-NULL value new_value <- app$wait_for_value(output = "dynamic_content") # Wait for input to change app$wait_for_value(input = "computed_input", ignore = list(NULL, "")) ``` -------------------------------- ### Update Action Git Tags Source: https://github.com/rstudio/shinytest2/blob/main/actions/test-app/README.md Commands to force-update the sliding git tags for the test-app action release cycle. ```bash git tag -f v1 # update historical v1 tag git tag -f actions/v1 # update sliding tag git push --tags --force # push tag to github ``` -------------------------------- ### expect_values - Snapshot Test App State Source: https://context7.com/rstudio/shinytest2/llms.txt Takes a JSON snapshot of input, output, and export values for comparison in future test runs. ```APIDOC ## expect_values ### Description Takes a JSON snapshot of input, output, and export values for comparison in future test runs. This is the primary method for regression testing Shiny applications. ### Parameters #### Query Parameters - **input** (character/vector) - Optional - Specific input IDs to snapshot. - **output** (logical/vector) - Optional - Specific output IDs to snapshot. - **export** (logical/vector) - Optional - Specific export IDs to snapshot. - **name** (character) - Optional - Custom name for the snapshot file. - **screenshot_args** (logical) - Optional - Whether to include a debug screenshot. ``` -------------------------------- ### Visual Regression Testing with expect_screenshot Source: https://context7.com/rstudio/shinytest2/llms.txt Employ expect_screenshot to perform visual regression testing by capturing screenshots and comparing them against baselines. It supports fuzzy matching, element-specific screenshots, and custom comparison functions. ```r library(shinytest2) library(testthat) test_that("App screenshot matches baseline", { app <- AppDriver$new( "path/to/app", variant = platform_variant() # Separate snapshots per OS/R version ) # Screenshot entire scrollable area (default) app$expect_screenshot() # Screenshot a specific element by CSS selector app$expect_screenshot(selector = "#myPlot") # Screenshot current viewport only app$expect_screenshot(selector = "viewport") # Add delay before screenshot (useful for animations) app$expect_screenshot(delay = 2) # 2 second delay # Fuzzy matching with threshold (handles minor pixel differences) app$expect_screenshot(threshold = 10, kernel_size = 5) # Custom comparison function app$expect_screenshot( compare = function(old, new) { compare_screenshot_threshold(old, new, threshold = 5) } ) # Named screenshot for multiple captures app$set_inputs(tab = "settings") app$expect_screenshot(name = "settings_tab") app$stop() }) ``` -------------------------------- ### Snapshot App State with expect_values Source: https://context7.com/rstudio/shinytest2/llms.txt Use expect_values to capture the current state of inputs, outputs, and exports for regression testing. It can snapshot all values, specific categories, or named values, and optionally include a debug screenshot. ```r library(shinytest2) library(testthat) test_that("App values match expected", { app <- AppDriver$new("path/to/app") # Snapshot all values app$expect_values() # Snapshot only specific categories app$expect_values(output = TRUE) app$expect_values(export = TRUE) # Snapshot specific named values app$expect_values(input = "slider1", output = c("plot", "table")) # Custom snapshot name (useful for multiple snapshots) app$expect_values(name = "initial_state") app$set_inputs(n = 100) app$expect_values(name = "after_update") # Include debug screenshot with values snapshot app$expect_values(screenshot_args = TRUE) # Disable debug screenshot app$expect_values(screenshot_args = FALSE) app$stop() }) ``` -------------------------------- ### Compare Screenshots with Thresholds Source: https://context7.com/rstudio/shinytest2/llms.txt Perform fuzzy image comparisons to handle rendering variations and check for maximum pixel differences. ```r library(shinytest2) # Compare two images with threshold result <- compare_screenshot_threshold( old = "tests/testthat/_snaps/test/001.png", new = "tests/testthat/_snaps/test/001.new.png", threshold = 10, # Max allowed difference kernel_size = 5 # Convolution kernel size ) # Returns: TRUE (same) or FALSE (different) # Find the actual difference between images diff_value <- screenshot_max_difference( old = "baseline.png", new = "current.png", kernel_size = 5 ) # Returns: numeric value (e.g., 7.5) # Use with expect_screenshot app$expect_screenshot( threshold = 5, # Allow small differences kernel_size = 5, quiet = FALSE # Print diagnostic info on failure ) ``` -------------------------------- ### Test DOM Content Source: https://context7.com/rstudio/shinytest2/llms.txt Extract text or HTML from elements and create snapshots for regression testing. ```r app <- AppDriver$new("path/to/app") # Get text content of elements title_text <- app$get_text("h1") # Returns: "Welcome to My App" # Get multiple elements' text menu_items <- app$get_text(".nav-item") # Returns: c("Home", "Settings", "About") # Get HTML (outer HTML by default) html_content <- app$get_html("#output-container") # Get inner HTML only inner_html <- app$get_html("#output-container", outer_html = FALSE) # Snapshot text content (more stable than HTML) app$expect_text("#status-message") # Snapshot HTML structure app$expect_html("#data-table") app$stop() ``` -------------------------------- ### HTML Output of Greeting Source: https://github.com/rstudio/shinytest2/blob/main/tests/testthat/_snaps/mac/app-hello.md This snippet displays the generated HTML for the greeting message. It shows the structure and attributes of the HTML element that renders the greeting. ```html "
Hello Hadley!
" ``` -------------------------------- ### get_logs - Debug Logging Source: https://context7.com/rstudio/shinytest2/llms.txt Access application logs and add custom debug messages. ```APIDOC ## get_logs ### Description Retrieves application logs including worker ID, timestamp, location, level, and message content. ### Response #### Success Response (200) - **logs** (data.frame) - A data frame containing log entries. ``` -------------------------------- ### click - Simulate Mouse Clicks Source: https://context7.com/rstudio/shinytest2/llms.txt Clicks on Shiny inputs, outputs, or arbitrary DOM elements. ```APIDOC ## click ### Description Clicks on Shiny inputs, outputs, or arbitrary DOM elements. For input buttons, waits for reactive updates to complete. ### Parameters #### Query Parameters - **input** (character) - Optional - Input ID to click. - **output** (character) - Optional - Output ID to click. - **selector** (character) - Optional - CSS selector to click. - **wait_** (logical) - Optional - Whether to wait for reactive updates. - **timeout_** (numeric) - Optional - Timeout in milliseconds. ``` -------------------------------- ### Rebuild C++ code Source: https://github.com/rstudio/shinytest2/blob/main/CLAUDE.md Reload the package after making changes to C++ source files. ```r # Rebuild and reload package devtools::load_all() ``` -------------------------------- ### get_js / run_js - Execute JavaScript Source: https://context7.com/rstudio/shinytest2/llms.txt Methods to execute JavaScript code within the browser context of the Shiny application. ```APIDOC ## get_js / run_js ### Description Execute JavaScript code in the browser context. `get_js` returns the result of the execution, while `run_js` executes the code without returning a value. ### Method POST/GET (Internal Browser Context) ### Parameters #### Request Body - **script** (string) - Required - The JavaScript code to execute. - **file** (string) - Optional - Path to a JavaScript file to execute. ### Response #### Success Response (200) - **result** (any) - The return value of the executed JavaScript (for `get_js`). ``` -------------------------------- ### set_inputs - Set Shiny Input Values Source: https://context7.com/rstudio/shinytest2/llms.txt Sets one or more Shiny input values programmatically. By default, waits for the app to become idle after setting inputs to ensure reactive updates complete. ```APIDOC ## set_inputs - Set Shiny Input Values ### Description Sets one or more Shiny input values programmatically. By default, waits for the app to become idle after setting inputs to ensure reactive updates complete. ### Method `app$set_inputs(...) ### Parameters #### Query Parameters - **wait_** (boolean) - Optional - Whether to wait for reactive updates after setting inputs. Defaults to TRUE. - **timeout_** (numeric) - Optional - Custom timeout for operations in milliseconds. - **allow_no_input_binding_** (boolean) - Optional - Allow setting inputs without input bindings (e.g., plotly hover data). Defaults to FALSE. ### Request Example ```R app <- AppDriver$new("path/to/app") # Set a single input app$set_inputs(slider1 = 50) # Set multiple inputs at once app$set_inputs( name = "John Doe", age = 30, agree = TRUE ) # Set inputs without waiting for reactive updates app$set_inputs(bins = 20, wait_ = FALSE) # Set inputs with custom timeout app$set_inputs(dataset = "mtcars", timeout_ = 10000) # Allow setting inputs without bindings (e.g., plotly hover data) app$set_inputs( `.clientValue-plotly_hover` = list(x = 1, y = 2), allow_no_input_binding_ = TRUE ) app$stop() ``` ### Response #### Success Response (200) - **NULL** - The operation is performed in place. #### Response Example ```R # No explicit return value, side effects are applied to the app state. ``` ``` -------------------------------- ### Simulate Mouse Clicks with click Source: https://context7.com/rstudio/shinytest2/llms.txt Use the click function to simulate user clicks on Shiny inputs, outputs, or any DOM element. It can optionally wait for reactive updates to complete or use custom timeouts. ```r app <- AppDriver$new("path/to/app") # Click an action button by input ID app$click(input = "submit_btn") # Click an output element app$click(output = "clickable_plot") # Click any element by CSS selector app$click(selector = "#my-custom-button") app$click(selector = ".nav-link[data-value='tab2']") # Click without waiting for updates app$click(input = "refresh", wait_ = FALSE) # Click with custom timeout app$click(input = "long_process", timeout_ = 30000) app$stop() ``` -------------------------------- ### upload_file - Test File Uploads Source: https://context7.com/rstudio/shinytest2/llms.txt Uploads files to Shiny file input components. ```APIDOC ## upload_file ### Description Uploads files to Shiny file input components. Files must be located within the tests/testthat directory. ### Parameters #### Query Parameters - **file_input** (character/vector) - Required - Path(s) to file(s) to upload. - **timeout_** (numeric) - Optional - Timeout in milliseconds. ``` -------------------------------- ### Set Shiny Input Values Source: https://context7.com/rstudio/shinytest2/llms.txt Programmatically set one or more Shiny input values. By default, it waits for the app to become idle after setting inputs. Options include disabling waiting, setting custom timeouts, and allowing inputs without bindings. ```R app <- AppDriver$new("path/to/app") # Set a single input app$set_inputs(slider1 = 50) # Set multiple inputs at once app$set_inputs( name = "John Doe", age = 30, agree = TRUE ) # Set inputs without waiting for reactive updates app$set_inputs(bins = 20, wait_ = FALSE) # Set inputs with custom timeout app$set_inputs(dataset = "mtcars", timeout_ = 10000) # Allow setting inputs without bindings (e.g., plotly hover data) app$set_inputs( `.clientValue-plotly_hover` = list(x = 1, y = 2), allow_no_input_binding_ = TRUE ) app$stop() ``` -------------------------------- ### Debug Logging Source: https://context7.com/rstudio/shinytest2/llms.txt Access application logs and inject custom messages for debugging test flows. ```r app <- AppDriver$new("path/to/app") # Add custom log message app$log_message("Starting input validation test") app$set_inputs(email = "invalid-email") app$log_message("Checking error message display") # Get all logs logs <- app$get_logs() # Returns data.frame with columns: # - workerid: Shiny worker ID # - timestamp: POSIXct timestamp # - location: "shinytest2", "shiny", or "chromote" # - level: "info", "stdout", "stderr", "error", "websocket" # - message: Log message content # Filter logs shiny_errors <- subset(logs, location == "shiny" & level == "stderr") js_errors <- subset(logs, location == "chromote" & level == "error") # Enable verbose WebSocket logging app_verbose <- AppDriver$new( "path/to/app", options = list(shiny.trace = TRUE) ) ws_logs <- subset(app_verbose$get_logs(), level == "websocket") app$stop() ``` -------------------------------- ### Wait for Application State Source: https://context7.com/rstudio/shinytest2/llms.txt Use these methods to pause test execution until specific export values or JavaScript conditions are met. ```r app$wait_for_value(export = "processed_data", timeout = 15000) # Wait for JavaScript condition to be true app$wait_for_js("window.myApp.isReady === true") app$wait_for_js( "document.querySelectorAll('.loaded').length > 0", timeout = 10000, interval = 200 ) app$stop() ``` -------------------------------- ### get_values / get_value - Retrieve App State Source: https://context7.com/rstudio/shinytest2/llms.txt Retrieves current values of inputs, outputs, and exported test values from the running Shiny application. ```APIDOC ## get_values / get_value - Retrieve App State ### Description Retrieves current values of inputs, outputs, and exported test values from the running Shiny application. ### Method `app$get_values(...) `app$get_value(...) ### Parameters #### Query Parameters - **input** (boolean or string) - Optional - If TRUE, retrieve all input values. If a string, retrieve a specific input value by name. - **output** (boolean or string) - Optional - If TRUE, retrieve all output values. If a string, retrieve a specific output value by name. - **export** (boolean or string) - Optional - If TRUE, retrieve all exported test values. If a string, retrieve a specific exported value by name. ### Request Example ```R library(shiny) shiny_app <- shinyApp( ui = fluidPage( numericInput("A", "A", 3), numericInput("B", "B", 4), verbatimTextOutput("C") ), server = function(input, output) { c_squared <- reactive({ input$A^2 + input$B^2 }) output$C <- renderText({ sqrt(c_squared()) }) # Export internal reactive values for testing exportTestValues( c_squared = { c_squared() } ) } ) app <- AppDriver$new(shiny_app) # Get all values (inputs, outputs, exports) all_values <- app$get_values() # Returns: list(input = list(A = 3, B = 4), # output = list(C = "5"), # export = list(c_squared = 25)) # Get specific categories input_values <- app$get_values(input = TRUE) output_values <- app$get_values(output = TRUE) export_values <- app$get_values(export = TRUE) # Get specific named values specific_values <- app$get_values(input = "A", output = "C") # Get a single value (shorthand) a_value <- app$get_value(input = "A") # Returns: 3 c_output <- app$get_value(output = "C") # Returns: "5" app$stop() ``` ### Response #### Success Response (200) - **list** - A list containing the requested input, output, and/or exported values. #### Response Example ```R # Example for app$get_values(): list( input = list(A = 3, B = 4), output = list(C = "5"), export = list(c_squared = 25) ) # Example for app$get_value(input = "A"): 3 ``` ``` -------------------------------- ### Test File Uploads with upload_file Source: https://context7.com/rstudio/shinytest2/llms.txt The upload_file function is used to test file upload components in Shiny applications. It requires files to be located within the tests/testthat directory and supports uploading single or multiple files with custom timeouts. ```r app <- AppDriver$new("path/to/app") # Upload a single file app$upload_file(file_input = "tests/testthat/data/sample.csv") # Upload multiple files to the same input app$upload_file( file_input = c( "tests/testthat/data/file1.csv", "tests/testthat/data/file2.csv" ) ) # Upload with custom timeout for large files app$upload_file( large_file_input = "tests/testthat/data/big_file.xlsx", timeout_ = 60000 ) # Use system.file for package data app$upload_file( data_input = system.file("extdata/example.csv", package = "mypackage") ) app$stop() ``` -------------------------------- ### get_text / get_html - DOM Content Testing Source: https://context7.com/rstudio/shinytest2/llms.txt Methods to extract text or HTML content from DOM elements for validation. ```APIDOC ## get_text / get_html ### Description Extract or snapshot text content and HTML from DOM elements. ### Parameters #### Query Parameters - **selector** (string) - Required - CSS selector for the target element. - **outer_html** (boolean) - Optional - Whether to return outer HTML (default: TRUE). ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.