tiny-match-snapshot
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')
Metadata
Release files for tiny-match-snapshot 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tiny-match-snapshot-0.0.2.tar.gz | 6.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tiny_match_snapshot-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.1 kB
Release files / tiny-match-snapshot-0.0.2.tar.gz
| Download URL | tiny-match-snapshot-0.0.2.tar.gz |
|---|---|
| Size | 6.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
88139d1e639f2381d2d7e643eaa5dde60a8885ad58a12304e294d9e5893ee58d
|
|
BLAKE2b-256 checksum How to use checksums |
d1557ecf3d0707cfc50c3a0dff4d4076ad76399e1de0259bcaad645edc76c21f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.19
|
Release files / tiny_match_snapshot-0.0.2-py3-none-any.whl
| Download URL | tiny_match_snapshot-0.0.2-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
70d4b09d1064986aaaf26a71c27e3a1caca521964772ad30406401d4141a2e20
|
|
BLAKE2b-256 checksum How to use checksums |
e811022f40f8f3a125334451ac2efcb5909cd573e399c9d05833a99f706e34b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.19
|