Skip to contents

Retrieves screenshots from testthat's _snaps/ directory. If several files match dir_ls_snaps(), they are merged into an animated gif.

Usage

snaps2fig(
  test_file = character(),
  name = NULL,
  auto_numbered = TRUE,
  variant = shinytest2::platform_variant(),
  fps = 5,
  ...
)

snaps2md(...)

snaps2rd(
  test_file = character(),
  name = NULL,
  auto_numbered = TRUE,
  variant = shinytest2::platform_variant(),
  fps = 5
)

dir_ls_snaps(
  test_file = character(),
  regexp = glue_regexp_snaps(),
  variant = shinytest2::platform_variant()
)

image_animate_snaps(snaps = fs::path(), fps = 5, ...)

image_write_snaps(image, path = withr::local_tempfile())

Arguments

test_file

Name of the test file, in which the snapshots are generated, without:

  • the extension

  • the test- prefix. If you're using testthat convention, this will be the name of the file in R/, which you are currently testing.

name

The name passed to shinytest2::AppDriver to be used for screenshots. Can be NULL, for no filtering by name.

auto_numbered

If TRUE, filter for snapshot files automatically numbered according to the scheme used by shinytest2::AppDriver. If you pass a name only to shinytest2::AppDriver$new() (recommended), and then invoke several shinytest2::AppDriver$expect_snapshot(), they resulting snapshots will all have the same name, appended by a counter from 000 to 999. If FALSE, any filename {name}*.png will be selected. You may need to set FALSE if you pass a name toshinytest2::AppDriver$expect_snapshot() directly.

variant

If not-NULL, results will be saved in _snaps/{variant}/{test}/{name}. This allows you to create different snapshots for different scenarios, like different operating systems or different R versions.

Note that there's no way to declare all possible variants up front which means that as soon as you start using variants, you are responsible for deleting snapshot variants that are no longer used. (testthat will still delete all variants if you delete the test.)

fps

frames per second. Ignored if delay is not NULL.

...

Arguments passed on to magick::image_animate

delay

delay after each frame, in 1/100 seconds. Must be length 1, or number of frames. If specified, then fps is ignored.

loop

how many times to repeat the animation. Default is infinite.

dispose

a frame disposal method from dispose_types()

optimize

optimize the gif animation by storing only the differences between frames. Input images must be exactly the same size.

regexp

A regular expression (e.g. [.]csv$) passed on to grep() to filter paths.

snaps

Vector of file names, as returned by dir_ls_snaps()

image

magick image object returned by image_read() or image_graph()

path

Path to file to snapshot. Optional for announce_snapshot_file() if name is supplied.

Value

For image_animate_snaps() A magick-image.

For image_write_snaps(), path to the (merged) screenshots.

Functions

  • snaps2fig(): Save screenshots to man/figures and return relative path from there.

  • snaps2md(): Save screenshots to man/figures and return markdown image markup, to be inserted in roxygen2 documentation.

  • snaps2rd(): Save screenshots to man/figures and return R documentation image markup, to be inserted in R documentation. For a custom roxygen2 tag with equivalent functionality, see crowInsertSnaps().

  • dir_ls_snaps(): List all testthat _snaps/ screenshots Finds all files for a variant, file and name.

  • image_animate_snaps(): Read in screenshot. If several, animate into a gif.

  • image_write_snaps(): Write out (merged) screenshots to new path.

Matching several screenshots

You can deposit several screenshots of a shiny app using shinytest2::AppDriver in testing. Use dir_ls_snaps() to identify all the resulting images. Typically used for consecutive screenshots.