Visual Comparison¶
Overview¶
Attention
Supported for allure and pytest frameworks.
allurefor reporting;pytestfor sreenshot name definition.
The VisualComparison class is designed to facilitate visual regression testing by comparing screenshots of UI
elements across different runs. It supports screenshot capturing, comparison, and visual difference highlighting.
The class also handles dynamic thresholds, image comparison metrics, and can generate visual references for testing purposes.
Interface¶
- class mops.visual_comparison.VisualComparison(driver_wrapper: DriverWrapper, element: Element = None)[source]¶
A class for performing visual regression comparisons between screenshots.
This class allows for managing visual regression settings, controlling the generation and comparison of visual references, and customizing various aspects of visual comparison, such as thresholds, delays, and the color scheme for diffs.
- test_item: Any = None¶
The pytest request.node object associated with the visual comparison.
- visual_regression_path: str = ''¶
The path where visual regression images (reference, output, and diff) will be stored.
- attach_diff_image_path: bool = False¶
Flag to determine whether to attach the diff image path to the report.
- skip_screenshot_comparison: bool = False¶
If set to True, the screenshot comparison will be skipped.
- hard_visual_reference_generation: bool = False¶
Forces the generation of visual references, replacing existing ones.
- soft_visual_reference_generation: bool = False¶
Allows the generation of visual references only if they do not already exist.
- dynamic_threshold_factor: int = 0¶
A factor for dynamically calculating the threshold based on image size.
- diff_color_scheme: tuple = (0, 255, 0)¶
The color scheme used for highlighting differences in images.
- assert_screenshot(filename: str, test_name: str, name_suffix: str, threshold: float, remove: list[Any], fill_background: str | bool, cut_box: Box | None) VisualComparison[source]¶
Assert that the given (by name) and taken screenshots are equal.
- Parameters:
filename (str) – The full screenshot name. A custom filename will be used if an empty string is given.
test_name (str) – Test name for the custom filename. It will try to find it automatically if an empty string is given.
name_suffix (str) – Filename suffix. Useful for the same element with positive/negative cases.
threshold (float) – Possible threshold for image comparison.
remove (bool) – Whether to remove elements from the screenshot.
fill_background (bool) – Whether to fill the background with a given color or black by default.
cut_box (
Box) – Custom coordinates to cut from the original image (left, top, right, bottom).
- Returns:
Usage¶
Usage
The assert_screenshot method of the VisualComparison class is utilized indirectly through the assert_screenshot
and soft_assert_screenshot methods provided by the Group, Element, and DriverWrapper classes.
These methods are designed to take screenshots of elements or pages and compare them against a reference image to
validate visual consistency across tests.
Allure Integration¶
If the Allure framework is available in the project, the results of the visual comparison will be automatically attached to the Allure report as part of the test case. This includes:
Actual Screenshot:
The screenshot taken during the test.
Expected Screenshot:
The reference image used for comparison.
Difference Screenshot:
An image highlighting any differences found between the actual and expected screenshots.