Skip to content

Commit bdaa021

Browse files
committed
New snapshot code
1 parent 4b6f014 commit bdaa021

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

.pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ repos:
1111
- id: check-docstring-first
1212
exclude: ^examples/streamlit
1313
- id: check-added-large-files
14+
args: ['--maxkb=1024']
1415
- id: requirements-txt-fixer
1516
- id: file-contents-sorter
1617
files: requirements-dev.txt
930 KB
Loading

tests/test_regressions.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import importlib
2+
import io
3+
import os
4+
import shutil
5+
6+
import pytest
7+
from PIL import Image
8+
from pixelmatch.contrib.PIL import pixelmatch
9+
from selenium import webdriver
10+
11+
options = webdriver.chrome.options.Options()
12+
options.add_argument("--headless")
13+
14+
15+
paths = os.listdir("tests/regressions/")
16+
paths = [p.replace(".py", "") for p in paths if p.endswith(".py")]
17+
18+
19+
@pytest.mark.parametrize("path", paths)
20+
def test_screenshot(path: str):
21+
driver = webdriver.Chrome(options=options)
22+
m = importlib.import_module(f"tests.regressions.{path}").m
23+
img_data = m._to_png(3, driver=driver, size=(800, 800))
24+
img_a = Image.open(io.BytesIO(img_data))
25+
img_a.save(f"/tmp/screenshot_new_{path}.png")
26+
27+
if os.path.exists(f"tests/screenshots/screenshot_{path}.png"):
28+
img_b = Image.open(f"tests/screenshots/screenshot_{path}.png")
29+
30+
img_diff = Image.new("RGBA", img_a.size)
31+
# note how there is no need to specify dimensions
32+
mismatch = pixelmatch(img_a, img_b, img_diff, threshold=0.2, includeAA=False)
33+
34+
img_diff.save(f"/tmp/screenshot_diff_{path}.png")
35+
assert mismatch < 1500
36+
37+
else:
38+
shutil.copy(
39+
f"/tmp/screenshot_new_{path}.png",
40+
f"tests/screenshots/screenshot_{path}.png",
41+
)
42+
raise Exception("no screenshot available, generating new")

0 commit comments

Comments
 (0)