feat: extend AutoCloseable in Playwright, BrowserContext, Browser and… (#238)
This commit is contained in:
@@ -19,22 +19,20 @@ package org.example;
|
||||
import com.microsoft.playwright.*;
|
||||
|
||||
public class EvaluateInBrowserContext {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Playwright playwright = Playwright.create();
|
||||
BrowserType browserType = playwright.firefox();
|
||||
Browser browser = browserType.launch();
|
||||
BrowserContext context = browser.newContext();
|
||||
Page page = context.newPage();
|
||||
page.navigate("https://www.example.com/");
|
||||
Object dimensions = page.evaluate("() => {\n" +
|
||||
" return {\n" +
|
||||
" width: document.documentElement.clientWidth,\n" +
|
||||
" height: document.documentElement.clientHeight,\n" +
|
||||
" deviceScaleFactor: window.devicePixelRatio\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
System.out.println(dimensions);
|
||||
browser.close();
|
||||
playwright.close();
|
||||
public static void main(String[] args) {
|
||||
try (Playwright playwright = Playwright.create();
|
||||
Browser browser = playwright.firefox().launch();
|
||||
BrowserContext context = browser.newContext();
|
||||
Page page = context.newPage()) {
|
||||
page.navigate("https://www.example.com/");
|
||||
Object dimensions = page.evaluate("() => {\n" +
|
||||
" return {\n" +
|
||||
" width: document.documentElement.clientWidth,\n" +
|
||||
" height: document.documentElement.clientHeight,\n" +
|
||||
" deviceScaleFactor: window.devicePixelRatio\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
System.out.println(dimensions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,18 +19,16 @@ package org.example;
|
||||
import com.microsoft.playwright.*;
|
||||
|
||||
public class InterceptNetworkRequests {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Playwright playwright = Playwright.create();
|
||||
BrowserType browserType = playwright.webkit();
|
||||
Browser browser = browserType.launch();
|
||||
BrowserContext context = browser.newContext();
|
||||
Page page = context.newPage();
|
||||
page.route("**", route -> {
|
||||
System.out.println(route.request().url());
|
||||
route.continue_();
|
||||
});
|
||||
page.navigate("http://todomvc.com");
|
||||
browser.close();
|
||||
playwright.close();
|
||||
public static void main(String[] args) {
|
||||
try (Playwright playwright = Playwright.create();
|
||||
Browser browser = playwright.webkit().launch();
|
||||
BrowserContext context = browser.newContext();
|
||||
Page page = context.newPage()) {
|
||||
page.route("**", route -> {
|
||||
System.out.println(route.request().url());
|
||||
route.continue_();
|
||||
});
|
||||
page.navigate("http://todomvc.com");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,21 +23,18 @@ import java.nio.file.Paths;
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
public class MobileAndGeolocation {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Playwright playwright = Playwright.create();
|
||||
BrowserType browserType = playwright.chromium();
|
||||
Browser browser = browserType.launch(new BrowserType.LaunchOptions().withHeadless(false));
|
||||
DeviceDescriptor pixel2 = playwright.devices().get("Pixel 2");
|
||||
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
|
||||
.withDevice(pixel2)
|
||||
.withLocale("en-US")
|
||||
.withGeolocation(new Geolocation(41.889938, 12.492507))
|
||||
.withPermissions(asList("geolocation")));
|
||||
Page page = context.newPage();
|
||||
page.navigate("https://www.openstreetmap.org/");
|
||||
page.click("a[data-original-title=\"Show My Location\"]");
|
||||
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("colosseum-pixel2.png")));
|
||||
browser.close();
|
||||
playwright.close();
|
||||
public static void main(String[] args) {
|
||||
try (Playwright playwright = Playwright.create();
|
||||
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().withHeadless(false));
|
||||
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
|
||||
.withDevice(playwright.devices().get("Pixel 2"))
|
||||
.withLocale("en-US")
|
||||
.withGeolocation(new Geolocation(41.889938, 12.492507))
|
||||
.withPermissions(asList("geolocation")));
|
||||
Page page = context.newPage()) {
|
||||
page.navigate("https://www.openstreetmap.org/");
|
||||
page.click("a[data-original-title=\"Show My Location\"]");
|
||||
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("colosseum-pixel2.png")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,22 +23,21 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class PageScreenshot {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Playwright playwright = Playwright.create();
|
||||
List<BrowserType> browserTypes = Arrays.asList(
|
||||
playwright.chromium(),
|
||||
playwright.webkit(),
|
||||
playwright.firefox()
|
||||
);
|
||||
for (BrowserType browserType : browserTypes) {
|
||||
Browser browser = browserType.launch();
|
||||
BrowserContext context = browser.newContext(
|
||||
new Browser.NewContextOptions().withViewport(800, 600));
|
||||
Page page = context.newPage();
|
||||
page.navigate("http://whatsmyuseragent.org/");
|
||||
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("screenshot-" + browserType.name() + ".png")));
|
||||
browser.close();
|
||||
public static void main(String[] args) {
|
||||
try (Playwright playwright = Playwright.create()) {
|
||||
List<BrowserType> browserTypes = Arrays.asList(
|
||||
playwright.chromium(),
|
||||
playwright.webkit(),
|
||||
playwright.firefox()
|
||||
);
|
||||
for (BrowserType browserType : browserTypes) {
|
||||
try (Browser browser = browserType.launch();
|
||||
BrowserContext context = browser.newContext(new Browser.NewContextOptions().withViewport(800, 600));
|
||||
Page page = context.newPage()) {
|
||||
page.navigate("http://whatsmyuseragent.org/");
|
||||
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("screenshot-" + browserType.name() + ".png")));
|
||||
}
|
||||
}
|
||||
}
|
||||
playwright.close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user