12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ from pathlib import Path
15
16
from typing import Callable
16
17
18
+ from PIL import Image
19
+
17
20
from playwright .async_api import Page
18
21
from tests .server import Server
19
22
from tests .utils import must
20
23
21
24
25
+ def assert_image_file_format (path : Path , image_format : str ) -> None :
26
+ with Image .open (path ) as img :
27
+ assert img .format == image_format
28
+
29
+
22
30
async def test_should_screenshot_with_mask (
23
31
page : Page , server : Server , assert_to_be_golden : Callable [[bytes , str ], None ]
24
32
) -> None :
@@ -43,3 +51,29 @@ async def test_should_screenshot_with_mask(
43
51
),
44
52
"mask-should-work-with-element-handle.png" ,
45
53
)
54
+
55
+
56
+ async def test_should_infer_screenshot_type_from_path (
57
+ page : Page , tmp_path : Path
58
+ ) -> None :
59
+ output_png_file = tmp_path / "foo.png"
60
+ await page .screenshot (path = output_png_file )
61
+ assert_image_file_format (output_png_file , "PNG" )
62
+
63
+ output_jpeg_file = tmp_path / "bar.jpeg"
64
+ await page .screenshot (path = output_jpeg_file )
65
+ assert_image_file_format (output_jpeg_file , "JPEG" )
66
+
67
+ output_jpg_file = tmp_path / "bar.jpg"
68
+ await page .screenshot (path = output_jpg_file )
69
+ assert_image_file_format (output_jpg_file , "JPEG" )
70
+
71
+
72
+ async def test_should_screenshot_with_type_argument (page : Page , tmp_path : Path ) -> None :
73
+ output_jpeg_with_png_extension = tmp_path / "foo_jpeg.png"
74
+ await page .screenshot (path = output_jpeg_with_png_extension , type = "jpeg" )
75
+ assert_image_file_format (output_jpeg_with_png_extension , "JPEG" )
76
+
77
+ output_png_with_jpeg_extension = tmp_path / "bar_png.jpeg"
78
+ await page .screenshot (path = output_png_with_jpeg_extension , type = "png" )
79
+ assert_image_file_format (output_png_with_jpeg_extension , "PNG" )
0 commit comments