Path: blob/main/tests/snapshots/test_snapshots.py
1601 views
import importlib1import io2import os3import shutil45import pytest6from PIL import Image7from pixelmatch.contrib.PIL import pixelmatch8from selenium import webdriver910options = webdriver.chrome.options.Options()11options.add_argument("--headless")121314paths = os.listdir("tests/snapshots/modules")15paths = [p.replace(".py", "") for p in paths if p.endswith(".py")]161718@pytest.mark.parametrize("path", paths)19def test_screenshot(path: str):20driver = webdriver.Chrome(options=options)21m = importlib.import_module(f"tests.snapshots.modules.{path}").m22img_data = m._to_png(3, driver=driver, size=(800, 800))23img_a = Image.open(io.BytesIO(img_data))24img_a.save(f"/tmp/screenshot_new_{path}.png")2526if os.path.exists(f"tests/snapshots/screenshots/screenshot_{path}.png"):27img_b = Image.open(f"tests/snapshots/screenshots/screenshot_{path}.png")2829img_diff = Image.new("RGBA", img_a.size)30# note how there is no need to specify dimensions31mismatch = pixelmatch(img_a, img_b, img_diff, threshold=0.2, includeAA=False)3233img_diff.save(f"/tmp/screenshot_diff_{path}.png")34m.save(f"/tmp/folium_map_{path}.html")35assert mismatch < 2003637else: # pragma: no cover38shutil.copy(39f"/tmp/screenshot_new_{path}.png",40f"tests/snapshots/screenshots/screenshot_{path}.png",41)42raise Exception("no screenshot available, generating new")434445