Skip to content

Commit f795239

Browse files
committed
test: add screenshot file type tests (#2955)
1 parent 789ab2a commit f795239

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

tests/async/test_screenshot.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from pathlib import Path
1516
from typing import Callable
1617

18+
from PIL import Image
19+
1720
from playwright.async_api import Page
1821
from tests.server import Server
1922
from tests.utils import must
2023

2124

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+
2230
async def test_should_screenshot_with_mask(
2331
page: Page, server: Server, assert_to_be_golden: Callable[[bytes, str], None]
2432
) -> None:
@@ -43,3 +51,29 @@ async def test_should_screenshot_with_mask(
4351
),
4452
"mask-should-work-with-element-handle.png",
4553
)
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

Comments
 (0)