| 
 | 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