mirror of
https://github.com/microsoft/playwright-java.git
synced 2025-09-08 21:01:00 +00:00
feat: page.pdf() (#39)
This commit is contained in:
parent
f38a1d15cc
commit
114fd54060
@ -83,7 +83,7 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Browser browser() {
|
||||
public BrowserImpl browser() {
|
||||
return browser;
|
||||
}
|
||||
|
||||
|
@ -96,8 +96,17 @@ class BrowserImpl extends ChannelOwner implements Browser {
|
||||
return page;
|
||||
}
|
||||
|
||||
private String name() {
|
||||
return initializer.get("name").getAsString();
|
||||
}
|
||||
|
||||
boolean isChromium() {
|
||||
return "chromium".equals(name());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String version() {
|
||||
return initializer.get("version").getAsString();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -491,7 +491,20 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
|
||||
@Override
|
||||
public byte[] pdf(PdfOptions options) {
|
||||
return new byte[0];
|
||||
if (!browserContext.browser().isChromium()) {
|
||||
throw new RuntimeException("Page.pdf only supported in headless Chromium");
|
||||
}
|
||||
if (options == null) {
|
||||
options = new PdfOptions();
|
||||
}
|
||||
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
|
||||
params.remove("path");
|
||||
JsonObject json = sendMessage("pdf", params).getAsJsonObject();
|
||||
byte[] buffer = Base64.getDecoder().decode(json.get("pdf").getAsString());
|
||||
if (options.path != null) {
|
||||
Utils.writeToFile(buffer, options.path);
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestPdf extends TestBase {
|
||||
@Test
|
||||
void shouldBeAbleToSaveFile() throws IOException {
|
||||
// TODO: test.skip(headful || browserName !== "chromium", "Printing to pdf is currently only supported in headless chromium.");
|
||||
File path = File.createTempFile("output", ".pdf");
|
||||
page.pdf(new Page.PdfOptions().withPath(path));
|
||||
long size = Files.size(path.toPath());
|
||||
assertTrue(size > 0);
|
||||
Browser b = playwright.webkit().launch();
|
||||
System.out.println(b.version());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldOnlyHavePdfInChromium() {
|
||||
// TODO: test.skip(browserName === "chromium");
|
||||
try {
|
||||
page.pdf();
|
||||
if (isChromium) {
|
||||
return;
|
||||
}
|
||||
fail("did not throw");
|
||||
} catch (RuntimeException e) {
|
||||
assertFalse(e.getMessage().contains("did not throw"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user