Skip to main content

Tiny snapshot assertion for tests

Project description

tiny-match-snapshot

Supported Python Versions PyPI - Version GitHub License PyPI - Downloads GitHub Actions Workflow Status

This package provides a tiny match snaptshot utility for usages with Playwright or with Selenium to capture screenshot of some web element and store it as snapshot and in next runs of tests, compare the taken screenshot with the stored as snapshot.

Requirements

  • Python 3.8+
  • Selenium or Playwright (or any other, see "Extending the behaviour" section)

Instalation

The package can be installed with the pip package manager:

pip install tiny-match-snapshot

Usage

Some examples of usage is disponible at examples folder on the root of this repository. For more complex usages, this package is used to run UI regression tests on the inventare/django-image-uploader-widget package.

With unittest and playwright

A first way, using unittest and playwright is:

import unittest
from unittest.case import TestCase
from match_snapshot import MatchSnapshot
from playwright.sync_api import sync_playwright

class MyTests(TestCase, MatchSnapshot):
    snapshot_path = "./__snapshots__"
    failed_path = "./__errors__"

    def test_selenium(self):
        playwright = sync_playwright().start()
        browser = playwright.chromium.launch()
        page = browser.new_page()
        page.goto("https://playwright.dev/")

        banner = page.query_selector(".hero.hero--primary")

        self.assert_match_snapshot(banner, "test_unittest_playwright")

        browser.close()

if __name__ == '__main__':
    unittest.main()

With unittest and selenium

An simple example of using unittest and selenium is:

import unittest
from unittest.case import TestCase
from match_snapshot import MatchSnapshot
from selenium import webdriver
from selenium.webdriver.common.by import By

class MyTests(TestCase, MatchSnapshot):
    snapshot_path = "./__snapshots__"
    failed_path = "./__errors__"

    def test_selenium(self):
        driver = webdriver.Chrome()
        driver.get("http://www.python.org")

        banner = driver.find_element(By.CLASS_NAME, "main-header")

        self.assert_match_snapshot(banner, "test_unittest_selenium")

if __name__ == '__main__':
    unittest.main()

With pytest and playwright

To use with pytest and playwright we can encapsulate our tests inside a class to inherits the MatchSnapshot class. To run the tests, we recommend to use the plugin pytest test runner and run the tests using the command: pytest --browser webkit --headed.

from playwright.sync_api import Page
from match_snapshot import MatchSnapshot

class TestsPlaywright(MatchSnapshot):
    snapshot_path = "./__snapshots__"
    failed_path = "./__errors__"

    def test_playwright(self, page: Page):
        page.goto("https://playwright.dev/")

        element = page.query_selector('.hero.hero--primary')
        self.assert_match_snapshot(element, 'test_pytest_playwright')

With pytest and selenium

A, not beautifull way to use with pytest and selenium is to use:

from match_snapshot import MatchSnapshot
from selenium import webdriver
from selenium.webdriver.common.by import By

class TestsSelenium(MatchSnapshot):
    snapshot_path = "./__snapshots__"
    failed_path = "./__errors__"

    def test_selenium(self):
        driver = webdriver.Chrome()
        driver.get("http://www.python.org")

        banner = driver.find_element(By.CLASS_NAME, "main-header")

        self.assert_match_snapshot(banner, "test_pytest_selenium")

Extending the behaviour

The basic behaviour of this library is the usage of the .screenshot() method of a web element. To extends the behaviour of the package to be used with other tool is basically write a wrapper class to compose with the element:

class MyCustomElement:
    def __init__(self, element):
        self.element = element

    def screenshot(self, path: str):
        # TODO: TAKE the screenshot and save it to the path
        pass

Now, instead of use the API element to the assert_match_snapshot() we use the composed MyCustomElement class:

class MyTestCase(MatchSnapshot, ...)
    def test_any_thing(self):
        element = ...

        match_element = MyCustomElement(element)
        self.assert_match_snapshot(match_element, 'my_test')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tiny-match-snapshot-0.0.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

tiny_match_snapshot-0.0.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file tiny-match-snapshot-0.0.2.tar.gz.

File metadata

  • Download URL: tiny-match-snapshot-0.0.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for tiny-match-snapshot-0.0.2.tar.gz
Algorithm Hash digest
SHA256 88139d1e639f2381d2d7e643eaa5dde60a8885ad58a12304e294d9e5893ee58d
MD5 b14e5ac4ca20ecad638dd3906016d9ec
BLAKE2b-256 d1557ecf3d0707cfc50c3a0dff4d4076ad76399e1de0259bcaad645edc76c21f

See more details on using hashes here.

File details

Details for the file tiny_match_snapshot-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for tiny_match_snapshot-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 70d4b09d1064986aaaf26a71c27e3a1caca521964772ad30406401d4141a2e20
MD5 4eadca7236988b3dc12be8db8df8aac3
BLAKE2b-256 e811022f40f8f3a125334451ac2efcb5909cd573e399c9d05833a99f706e34b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page