Visual Comparison

Overview

Attention

Supported for allure and pytest frameworks.

  • allure for reporting;

  • pytest for 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, element=None)
visual_regression_path: str = ''
-   Path where visual regression images (reference, output, and diff) will be stored.
test_item: pytest.Item = None
-   The pytest `request.node.` object associated with the visual comparison.
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.
visual_reference_generation: bool = False
-   Enables generation of visual references.
hard_visual_reference_generation: bool = False
-   Forces generation of visual references, replacing existing ones.
soft_visual_reference_generation: bool = False
-   Allows generation of visual references only if they do not exist.
default_delay: Union[int, float] = 0.75
-   Default delay before taking a screenshot.
default_threshold: Union[int, float] = 0
-   Default threshold for image comparison.
dynamic_threshold_factor: int = 0
-   Factor for dynamically calculating threshold based on image size.
diff_color_scheme: tuple = (0, 255, 0)
-   Color scheme used for highlighting differences in images.
assert_screenshot(filename: str, test_name: str, name_suffix: str, threshold: int | float, delay: int | float, scroll: bool, remove: List[Any], fill_background: str | bool, cut_box: CutBox | None) VisualComparison

Assert given (by name) and taken screenshot equals

Parameters:
  • filename – full screenshot name. Custom filename will be used if empty string given

  • test_name – test name for custom filename. Will try to find it automatically if empty string given

  • name_suffix – filename suffix. Good to use for same element with positive/negative case

  • threshold – possible threshold

  • delay – delay before taking screenshot

  • scroll – scroll to element before taking the screenshot

  • remove – remove elements from screenshot

  • fill_background – fill background with given color or black color by default

  • cut_box – custom coordinates, that will be cut from original image (left, top, right, bottom)

Returns:

self


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.