Class SnapshotTester
SnapshotTester captures the rendered HTML of Oorian Element objects and compares
it against previously saved snapshots. This detects unintended changes in component rendering
and makes it easy to review intentional changes.
Snapshot files are stored as .snap files in a configurable
directory. On first run, the snapshot is automatically created. On subsequent runs, the
current output is compared against the saved file.
Update mode: When enabled, mismatched snapshots are automatically overwritten with the current output instead of failing. Use this when you've made intentional changes and want to update all baselines.
Usage:
// Create tester with snapshot directory
SnapshotTester snapshots = new SnapshotTester("test/snapshots");
// Compare element output against saved snapshot
Div card = new Div();
card.addClass("card");
card.addElement(new H1("Title"));
snapshots.assertMatchesSnapshot("user-card", card);
// Compare raw HTML string
snapshots.assertMatchesSnapshot("header-html", someHtmlString);
// Update mode: overwrite all snapshots
snapshots.setUpdateMode(true);
snapshots.assertMatchesSnapshot("user-card", card); // saves instead of comparing
- Since:
- 2.1
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionSnapshotTester(String snapshotDirectory) Constructs a new SnapshotTester with the specified snapshot directory.SnapshotTester(Path snapshotDirectory) Constructs a new SnapshotTester with the specified snapshot directory path. -
Method Summary
Modifier and TypeMethodDescriptionassertMatchesSnapshot(String name, Element element) Asserts that an element's rendered HTML matches the saved snapshot.assertMatchesSnapshot(String name, String content) Asserts that a string matches the saved snapshot.booleandeleteSnapshot(String name) Deletes a snapshot file.Returns the path to the snapshot directory.booleanReturns whether update mode is enabled.voidsetUpdateMode(boolean updateMode) Sets whether update mode is enabled.booleansnapshotExists(String name) Checks whether a snapshot file exists for the given name.voidupdateSnapshot(String name, Element element) Saves an element's rendered HTML as a snapshot without comparing.voidupdateSnapshot(String name, String content) Saves a string as a snapshot without comparing.
-
Constructor Details
-
SnapshotTester
Constructs a new SnapshotTester with the specified snapshot directory.The directory will be created if it does not exist when snapshots are saved.
- Parameters:
snapshotDirectory- the path to the directory for storing snapshot files
-
SnapshotTester
Constructs a new SnapshotTester with the specified snapshot directory path.- Parameters:
snapshotDirectory- the path to the directory for storing snapshot files
-
-
Method Details
-
setUpdateMode
public void setUpdateMode(boolean updateMode) Sets whether update mode is enabled.When update mode is
true, mismatched snapshots are automatically overwritten with the current output instead of throwingSnapshotMismatchException. This is useful when intentional changes have been made and all baselines need updating.- Parameters:
updateMode- true to enable update mode
-
isUpdateMode
public boolean isUpdateMode()Returns whether update mode is enabled.- Returns:
- true if update mode is enabled
-
assertMatchesSnapshot
Asserts that an element's rendered HTML matches the saved snapshot.If no snapshot exists, the current output is saved as the new baseline and the result is
SnapshotResult.NEW_SNAPSHOT. If a snapshot exists and matches, the result isSnapshotResult.MATCH. If it doesn't match and update mode is disabled, aSnapshotMismatchExceptionis thrown.- Parameters:
name- the snapshot name (used as the filename without extension)element- the element to render and compare- Returns:
- the result of the snapshot comparison
- Throws:
SnapshotMismatchException- if the output doesn't match and update mode is off
-
assertMatchesSnapshot
Asserts that a string matches the saved snapshot.If no snapshot exists, the content is saved as the new baseline. If a snapshot exists and matches, the result is
SnapshotResult.MATCH. If it doesn't match and update mode is disabled, aSnapshotMismatchExceptionis thrown.- Parameters:
name- the snapshot name (used as the filename without extension)content- the content to compare- Returns:
- the result of the snapshot comparison
- Throws:
SnapshotMismatchException- if the content doesn't match and update mode is off
-
updateSnapshot
Saves an element's rendered HTML as a snapshot without comparing.This overwrites any existing snapshot with the same name.
- Parameters:
name- the snapshot nameelement- the element to render and save
-
updateSnapshot
Saves a string as a snapshot without comparing.This overwrites any existing snapshot with the same name.
- Parameters:
name- the snapshot namecontent- the content to save
-
snapshotExists
Checks whether a snapshot file exists for the given name.- Parameters:
name- the snapshot name- Returns:
- true if a snapshot file exists
-
deleteSnapshot
Deletes a snapshot file.- Parameters:
name- the snapshot name to delete- Returns:
- true if the file was deleted, false if it didn't exist
-
getSnapshotDirectory
Returns the path to the snapshot directory.- Returns:
- the snapshot directory path
-