Skip to content

Commit b8008b8

Browse files
committed
improve screenshot_regression types; add tests for pytest-splinter
1 parent e218925 commit b8008b8

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed
Loading
Loading

pytest_image_diff/_types.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import BinaryIO, Union, Tuple, Callable, Optional
1+
from typing import BinaryIO, Union, Tuple, Optional
22

33
from PIL.Image import Image
44
from typing_extensions import Literal, Protocol
@@ -7,8 +7,6 @@
77
ImageFileType = Union[Image, PathOrFileType]
88
ImageSize = Tuple[int, int]
99

10-
ScreenshotRegressionCallableType = Callable[[float, Optional[str]], bool]
11-
1210

1311
class ImageRegressionCallableType(Protocol):
1412
def __call__(

pytest_image_diff/splinter.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1+
import pytest
12
from tempfile import NamedTemporaryFile
2-
from typing import Optional
3+
from typing import Optional, Generator
4+
from typing_extensions import Protocol
35

4-
import pytest
56

67
try:
78
# Check pytest-splinter
89
from pytest_splinter.plugin import Browser
910
except ImportError:
1011
raise
1112

12-
from ._types import ScreenshotRegressionCallableType, ImageRegressionCallableType
13+
from ._types import ImageRegressionCallableType
14+
15+
16+
class ScreenshotRegressionCallableType(Protocol):
17+
def __call__(
18+
self,
19+
browser: Optional[Browser] = None,
20+
threshold: Optional[float] = None,
21+
suffix: Optional[str] = None,
22+
) -> bool:
23+
pass
24+
1325

1426
__all__ = ["screenshot_regression"]
1527

@@ -19,19 +31,29 @@ def screenshot_regression(
1931
browser: Browser,
2032
image_regression: ImageRegressionCallableType,
2133
image_diff_threshold: float,
22-
) -> ScreenshotRegressionCallableType:
34+
) -> Generator[ScreenshotRegressionCallableType, None, None]:
2335
"""
2436
Check regression browser screenshot
37+
38+
:param browser: optional, by default from `browser` fixture
2539
:param threshold: float, by default from `image_diff_threshold`
2640
:param suffix: str, need for multiple checks by one test
2741
"""
42+
default_browser = browser
2843

2944
def _factory(
30-
threshold: float = image_diff_threshold, suffix: Optional[str] = ""
45+
browser: Optional[Browser] = None,
46+
threshold: Optional[float] = None,
47+
suffix: Optional[str] = "",
3148
) -> bool:
49+
if browser is None:
50+
browser = default_browser
51+
52+
if threshold is None:
53+
threshold = image_diff_threshold
3254
tf = NamedTemporaryFile(suffix=".png")
3355
image = tf.name
3456
browser.driver.save_screenshot(image)
3557
return image_regression(image, threshold, suffix)
3658

37-
return _factory
59+
yield _factory

tests/test_splinter.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
23
import pytest
34

45
try:
@@ -35,3 +36,15 @@ def splinter_webdriver_executable(request, splinter_webdriver):
3536
@with_splinter
3637
def test_splinter_fixture(screenshot_regression):
3738
assert screenshot_regression
39+
40+
41+
HTML_FILE = os.path.join(os.path.dirname(__file__), "files/example.html")
42+
43+
44+
@with_splinter
45+
def test_splinter(browser, screenshot_regression):
46+
browser.driver.set_window_size(1280, 1024)
47+
browser.visit("file://" + HTML_FILE)
48+
screenshot_regression()
49+
browser.driver.set_window_size(800, 600)
50+
screenshot_regression(suffix="small_window")

0 commit comments

Comments
 (0)