From 44e99a744a1621fa17fbbdd87d2ba72b8fd249f2 Mon Sep 17 00:00:00 2001 From: Kyle McGonagle Date: Wed, 3 Sep 2025 21:48:30 -0400 Subject: [PATCH 1/2] Default to png file type for screenshots if type is not provided and path does not include extension. --- playwright/_impl/_element_handle.py | 2 +- tests/async/test_screenshot.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/playwright/_impl/_element_handle.py b/playwright/_impl/_element_handle.py index 1561e19fc..986e2b20d 100644 --- a/playwright/_impl/_element_handle.py +++ b/playwright/_impl/_element_handle.py @@ -457,7 +457,7 @@ def convert_select_option_values( def determine_screenshot_type(path: Union[str, Path]) -> Literal["jpeg", "png"]: mime_type, _ = mimetypes.guess_type(path) - if mime_type == "image/png": + if mime_type == "image/png" or mime_type is None: return "png" if mime_type == "image/jpeg": return "jpeg" diff --git a/tests/async/test_screenshot.py b/tests/async/test_screenshot.py index 36149225f..54c5f957d 100644 --- a/tests/async/test_screenshot.py +++ b/tests/async/test_screenshot.py @@ -68,6 +68,10 @@ async def test_should_infer_screenshot_type_from_path( await page.screenshot(path=output_jpg_file) assert_image_file_format(output_jpg_file, "JPEG") + output_png_file = tmp_path / "foo" + await page.screenshot(path=output_png_file) + assert_image_file_format(output_png_file, "PNG") + async def test_should_screenshot_with_type_argument(page: Page, tmp_path: Path) -> None: output_jpeg_with_png_extension = tmp_path / "foo_jpeg.png" From 3bc8038dc705858285a49ec765e536aa3b7241b4 Mon Sep 17 00:00:00 2001 From: Kyle McGonagle Date: Wed, 3 Sep 2025 22:01:28 -0400 Subject: [PATCH 2/2] Add test for type ag without extension in path. --- tests/async/test_screenshot.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/async/test_screenshot.py b/tests/async/test_screenshot.py index 54c5f957d..6d6710188 100644 --- a/tests/async/test_screenshot.py +++ b/tests/async/test_screenshot.py @@ -71,7 +71,7 @@ async def test_should_infer_screenshot_type_from_path( output_png_file = tmp_path / "foo" await page.screenshot(path=output_png_file) assert_image_file_format(output_png_file, "PNG") - + async def test_should_screenshot_with_type_argument(page: Page, tmp_path: Path) -> None: output_jpeg_with_png_extension = tmp_path / "foo_jpeg.png" @@ -81,3 +81,7 @@ async def test_should_screenshot_with_type_argument(page: Page, tmp_path: Path) output_png_with_jpeg_extension = tmp_path / "bar_png.jpeg" await page.screenshot(path=output_png_with_jpeg_extension, type="png") assert_image_file_format(output_png_with_jpeg_extension, "PNG") + + output_png_with_jpeg_extension = tmp_path / "bar_jpeg" + await page.screenshot(path=output_png_with_jpeg_extension, type="jpeg") + assert_image_file_format(output_png_with_jpeg_extension, "JPEG")