expect_snapshot_html: Visual test for an HTML document

View source: R/expect_snapshot_doc.R

expect_snapshot_htmlR Documentation

Visual test for an HTML document

Description

This expectation can be used with 'tinytest' and 'testthat' to check if a current document of type HTML matches a target document. When the expectation is checked for the first time, the expectation fails and a target miniature of the document is saved in a folder named _tinytest_doconv or _snaps.

Usage

expect_snapshot_html(
  name,
  x,
  tolerance = 0.001,
  engine = c("tinytest", "testthat"),
  ...
)

Arguments

name

a string to identify the test. Each document in the test suite must have a unique name.

x

file path of an HTML document

tolerance

the ratio of different pixels that is acceptable before triggering a failure.

engine

test package being used in the test suite, one of "tinytest" or "testthat".

...

arguments used by webshot::webshot2().

Value

A tinytest::tinytest() or a testthat::expect_snapshot_file object.

Examples

file <- tempfile(fileext = ".html")
html <- paste0("<html><head><title>hello</title></head>",
       "<body><h1>Hello World</h1></body></html>\n")
cat(html, file = file)

## Not run: 
if (require("tinytest") && require("webshot2")){
  # first run add a new snapshot
  expect_snapshot_html(x = file, name = "html file",
    engine = "tinytest")
  # next runs compare with the snapshot
  expect_snapshot_html(x = file, name = "html file",
    engine = "tinytest")

  # cleaning directory
  unlink("_tinytest_doconv", recursive = TRUE,
    force = TRUE)
}
if (require("testthat") && require("webshot2")){
  local_edition(3)
  # first run add a new snapshot
  expect_snapshot_html(x = file, name = "html file",
    engine = "testthat")
  # next runs compare with the snapshot
  expect_snapshot_html(x = file, name = "html file",
    engine = "testthat")
}

## End(Not run)

doconv documentation built on Feb. 16, 2023, 8:31 p.m.