@@ -352,4 +352,29 @@ private byte[] expectedScreenshot(BrowserType browserType, String name) throws I
352352 Path path = Paths .get ("src/test/resources/expectations" );
353353 return Files .readAllBytes (path .resolve (name + "-" + browserType .name () + ".png" ));
354354 }
355+
356+ @ PlaywrightTest
357+ void pathOptionShouldDetectJpeg (Page page , @ TempDir Path tmpDir ) throws IOException {
358+ Path outputPath = tmpDir .resolve ("screenshot.jpg" );
359+ byte [] buffer = page .screenshot (new Page .ScreenshotOptions ().setPath (outputPath ));
360+ assertEquals (asList (0xFF , 0xD8 , 0xFF ), asList (buffer [0 ] & 0xFF , buffer [1 ] & 0xFF , buffer [2 ] & 0xFF ));
361+ assertArrayEquals (buffer , Files .readAllBytes (outputPath ));
362+ }
363+
364+ @ PlaywrightTest
365+ void shouldPreferTypeOverPathExtension (Page page , @ TempDir Path tmpDir ) {
366+ Path outputPath = tmpDir .resolve ("screenshot.png" );
367+ byte [] buffer = page .screenshot (new Page .ScreenshotOptions ().setPath (outputPath ).setType (ScreenshotType .JPEG ));
368+ assertEquals (asList (0xFF , 0xD8 , 0xFF ), asList (buffer [0 ] & 0xFF , buffer [1 ] & 0xFF , buffer [2 ] & 0xFF ));
369+ }
370+
371+ @ PlaywrightTest
372+ void pathOptionShouldThrowForUnsupportedExtension (Page page , @ TempDir Path tmpDir ) {
373+ PlaywrightException e = assertThrows (PlaywrightException .class ,
374+ () -> page .screenshot (new Page .ScreenshotOptions ().setPath (tmpDir .resolve ("screenshot.txt" ))));
375+ assertTrue (e .getMessage ().contains ("path: unsupported mime type \" text/plain\" " ), e .getMessage ());
376+ e = assertThrows (PlaywrightException .class ,
377+ () -> page .locator ("body" ).screenshot (new Locator .ScreenshotOptions ().setPath (tmpDir .resolve ("screenshot.txt" ))));
378+ assertTrue (e .getMessage ().contains ("path: unsupported mime type \" text/plain\" " ), e .getMessage ());
379+ }
355380}
0 commit comments