Skip to content

Commit 1e592bb

Browse files
authored
fix(screenshot): derive the screenshot type from the path extension like the Node.js client (#2021)
1 parent 4a6900e commit 1e592bb

4 files changed

Lines changed: 46 additions & 34 deletions

File tree

‎playwright/src/main/java/com/microsoft/playwright/impl/ElementHandleImpl.java‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
import static com.microsoft.playwright.impl.Serialization.*;
3737
import static com.microsoft.playwright.impl.Utils.*;
3838
import static com.microsoft.playwright.impl.Utils.addFilePathUploadParams;
39-
import static com.microsoft.playwright.options.ScreenshotType.JPEG;
40-
import static com.microsoft.playwright.options.ScreenshotType.PNG;
41-
import static com.microsoft.playwright.options.ScreenshotType.WEBP;
4239

4340
public class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
4441
private final FrameImpl frame;
@@ -270,20 +267,8 @@ public byte[] screenshot(ScreenshotOptions options) {
270267
if (options == null) {
271268
options = new ScreenshotOptions();
272269
}
273-
if (options.type == null) {
274-
options.type = PNG;
275-
if (options.path != null) {
276-
String fileName = options.path.getFileName().toString();
277-
int extStart = fileName.lastIndexOf('.');
278-
if (extStart != -1) {
279-
String extension = fileName.substring(extStart).toLowerCase();
280-
if (".jpeg".equals(extension) || ".jpg".equals(extension)) {
281-
options.type = JPEG;
282-
} else if (".webp".equals(extension)) {
283-
options.type = WEBP;
284-
}
285-
}
286-
}
270+
if (options.type == null && options.path != null) {
271+
options.type = Utils.screenshotTypeForPath(options.path);
287272
}
288273
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
289274
params.remove("path");

‎playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
import static com.microsoft.playwright.impl.Serialization.gson;
3434
import static com.microsoft.playwright.impl.Serialization.parseError;
3535
import static com.microsoft.playwright.impl.Utils.*;
36-
import static com.microsoft.playwright.options.ScreenshotType.JPEG;
37-
import static com.microsoft.playwright.options.ScreenshotType.PNG;
38-
import static com.microsoft.playwright.options.ScreenshotType.WEBP;
3936
import static java.nio.charset.StandardCharsets.UTF_8;
4037
import static java.nio.file.Files.readAllBytes;
4138
import static java.util.Arrays.asList;
@@ -1273,20 +1270,8 @@ private byte[] screenshotImpl(ScreenshotOptions options) {
12731270
if (options == null) {
12741271
options = new ScreenshotOptions();
12751272
}
1276-
if (options.type == null) {
1277-
options.type = PNG;
1278-
if (options.path != null) {
1279-
String fileName = options.path.getFileName().toString();
1280-
int extStart = fileName.lastIndexOf('.');
1281-
if (extStart != -1) {
1282-
String extension = fileName.substring(extStart).toLowerCase();
1283-
if (".jpeg".equals(extension) || ".jpg".equals(extension)) {
1284-
options.type = JPEG;
1285-
} else if (".webp".equals(extension)) {
1286-
options.type = WEBP;
1287-
}
1288-
}
1289-
}
1273+
if (options.type == null && options.path != null) {
1274+
options.type = Utils.screenshotTypeForPath(options.path);
12901275
}
12911276
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
12921277
params.remove("path");

‎playwright/src/main/java/com/microsoft/playwright/impl/Utils.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import com.microsoft.playwright.options.ClientCertificate;
2424
import com.microsoft.playwright.options.FilePayload;
2525
import com.microsoft.playwright.options.HttpHeader;
26+
import com.microsoft.playwright.options.ScreenshotType;
2627

2728
import java.io.FileOutputStream;
2829
import java.io.IOException;
2930
import java.io.InputStream;
3031
import java.io.OutputStream;
3132
import java.lang.reflect.Field;
3233
import java.lang.reflect.Modifier;
34+
import java.net.URLConnection;
3335
import java.nio.file.Files;
3436
import java.nio.file.Path;
3537
import java.util.*;
@@ -246,6 +248,21 @@ static void mkParentDirs(Path file) {
246248
}
247249
}
248250

251+
/** The screenshot type for a path when no type is given, from its extension, like the Node.js client. */
252+
static ScreenshotType screenshotTypeForPath(Path path) {
253+
String fileName = path.getFileName().toString();
254+
int extStart = fileName.lastIndexOf('.');
255+
String extension = extStart == -1 ? "" : fileName.substring(extStart + 1).toLowerCase();
256+
switch (extension) {
257+
case "png": return ScreenshotType.PNG;
258+
case "jpg":
259+
case "jpeg": return ScreenshotType.JPEG;
260+
case "webp": return ScreenshotType.WEBP;
261+
default:
262+
throw new PlaywrightException("path: unsupported mime type \"" + URLConnection.guessContentTypeFromName(fileName) + "\"");
263+
}
264+
}
265+
249266
static void writeToFile(byte[] buffer, Path path) {
250267
mkParentDirs(path);
251268
try (FileOutputStream out = new FileOutputStream(path.toFile())) {

‎playwright/src/test/java/com/microsoft/playwright/TestPageScreenshot.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)