Skip to content

Commit a6f2559

Browse files
authored
feat: extend AutoCloseable in Playwright, BrowserContext, Browser and… (microsoft#238)
1 parent 5af0918 commit a6f2559

13 files changed

Lines changed: 175 additions & 145 deletions

File tree

README.md

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,22 @@ import java.util.Arrays;
6464
import java.util.List;
6565

6666
public class PageScreenshot {
67-
public static void main(String[] args) throws Exception {
68-
Playwright playwright = Playwright.create();
69-
List<BrowserType> browserTypes = Arrays.asList(
67+
public static void main(String[] args) {
68+
try (Playwright playwright = Playwright.create()) {
69+
List<BrowserType> browserTypes = Arrays.asList(
7070
playwright.chromium(),
7171
playwright.webkit(),
7272
playwright.firefox()
73-
);
74-
for (BrowserType browserType : browserTypes) {
75-
Browser browser = browserType.launch();
76-
BrowserContext context = browser.newContext(
77-
new Browser.NewContextOptions().withViewport(800, 600));
78-
Page page = context.newPage();
79-
page.navigate("http://whatsmyuseragent.org/");
80-
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("screenshot-" + browserType.name() + ".png")));
81-
browser.close();
73+
);
74+
for (BrowserType browserType : browserTypes) {
75+
try (Browser browser = browserType.launch();
76+
BrowserContext context = browser.newContext(new Browser.NewContextOptions().withViewport(800, 600));
77+
Page page = context.newPage()) {
78+
page.navigate("http://whatsmyuseragent.org/");
79+
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("screenshot-" + browserType.name() + ".png")));
80+
}
81+
}
8282
}
83-
playwright.close();
8483
}
8584
}
8685
```
@@ -92,25 +91,22 @@ This snippet emulates Mobile Chromium on a device at a given geolocation, naviga
9291
```java
9392
import com.microsoft.playwright.*;
9493
import java.nio.file.Paths;
95-
import static java.util.Arrays.asList;
94+
import java.util.Arrays;
9695

9796
public class MobileAndGeolocation {
98-
public static void main(String[] args) throws Exception {
99-
Playwright playwright = Playwright.create();
100-
BrowserType browserType = playwright.chromium();
101-
Browser browser = browserType.launch(new BrowserType.LaunchOptions().withHeadless(false));
102-
DeviceDescriptor pixel2 = playwright.devices().get("Pixel 2");
103-
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
104-
.withDevice(pixel2)
105-
.withLocale("en-US")
106-
.withGeolocation(new Geolocation(41.889938, 12.492507))
107-
.withPermissions(asList("geolocation")));
108-
Page page = context.newPage();
109-
page.navigate("https://www.openstreetmap.org/");
110-
page.click("a[data-original-title=\"Show My Location\"]");
111-
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("colosseum-pixel2.png")));
112-
browser.close();
113-
playwright.close();
97+
public static void main(String[] args) {
98+
try (Playwright playwright = Playwright.create();
99+
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().withHeadless(false));
100+
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
101+
.withDevice(playwright.devices().get("Pixel 2"))
102+
.withLocale("en-US")
103+
.withGeolocation(new Geolocation(41.889938, 12.492507))
104+
.withPermissions(Arrays.asList("geolocation")));
105+
Page page = context.newPage()) {
106+
page.navigate("https://www.openstreetmap.org/");
107+
page.click("a[data-original-title=\"Show My Location\"]");
108+
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("colosseum-pixel2.png")));
109+
}
114110
}
115111
}
116112
```
@@ -123,23 +119,21 @@ This code snippet navigates to example.com in Firefox, and executes a script in
123119
import com.microsoft.playwright.*;
124120

125121
public class EvaluateInBrowserContext {
126-
public static void main(String[] args) throws Exception {
127-
Playwright playwright = Playwright.create();
128-
BrowserType browserType = playwright.firefox();
129-
Browser browser = browserType.launch(new BrowserType.LaunchOptions().withHeadless(false));
130-
BrowserContext context = browser.newContext();
131-
Page page = context.newPage();
132-
page.navigate("https://www.example.com/");
133-
Object dimensions = page.evaluate("() => {\n" +
134-
" return {\n" +
135-
" width: document.documentElement.clientWidth,\n" +
136-
" height: document.documentElement.clientHeight,\n" +
137-
" deviceScaleFactor: window.devicePixelRatio\n" +
138-
" }\n" +
139-
"}");
140-
System.out.println(dimensions);
141-
browser.close();
142-
playwright.close();
122+
public static void main(String[] args) {
123+
try (Playwright playwright = Playwright.create();
124+
Browser browser = playwright.firefox().launch(new BrowserType.LaunchOptions().withHeadless(false));
125+
BrowserContext context = browser.newContext();
126+
Page page = context.newPage()) {
127+
page.navigate("https://www.example.com/");
128+
Object dimensions = page.evaluate("() => {\n" +
129+
" return {\n" +
130+
" width: document.documentElement.clientWidth,\n" +
131+
" height: document.documentElement.clientHeight,\n" +
132+
" deviceScaleFactor: window.devicePixelRatio\n" +
133+
" }\n" +
134+
"}");
135+
System.out.println(dimensions);
136+
}
143137
}
144138
}
145139
```
@@ -152,19 +146,17 @@ This code snippet sets up request routing for a WebKit page to log all network r
152146
import com.microsoft.playwright.*;
153147

154148
public class InterceptNetworkRequests {
155-
public static void main(String[] args) throws Exception {
156-
Playwright playwright = Playwright.create();
157-
BrowserType browserType = playwright.webkit();
158-
Browser browser = browserType.launch();
159-
BrowserContext context = browser.newContext();
160-
Page page = context.newPage();
161-
page.route("**", route -> {
162-
System.out.println(route.request().url());
163-
route.continue_();
164-
});
165-
page.navigate("http://todomvc.com");
166-
browser.close();
167-
playwright.close();
149+
public static void main(String[] args) {
150+
try (Playwright playwright = Playwright.create();
151+
Browser browser = playwright.webkit().launch();
152+
BrowserContext context = browser.newContext();
153+
Page page = context.newPage()) {
154+
page.route("**", route -> {
155+
System.out.println(route.request().url());
156+
route.continue_();
157+
});
158+
page.navigate("http://todomvc.com");
159+
}
168160
}
169161
}
170162
```

examples/src/main/java/org/example/EvaluateInBrowserContext.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,20 @@
1919
import com.microsoft.playwright.*;
2020

2121
public class EvaluateInBrowserContext {
22-
public static void main(String[] args) throws Exception {
23-
Playwright playwright = Playwright.create();
24-
BrowserType browserType = playwright.firefox();
25-
Browser browser = browserType.launch();
26-
BrowserContext context = browser.newContext();
27-
Page page = context.newPage();
28-
page.navigate("https://www.example.com/");
29-
Object dimensions = page.evaluate("() => {\n" +
30-
" return {\n" +
31-
" width: document.documentElement.clientWidth,\n" +
32-
" height: document.documentElement.clientHeight,\n" +
33-
" deviceScaleFactor: window.devicePixelRatio\n" +
34-
" }\n" +
35-
"}");
36-
System.out.println(dimensions);
37-
browser.close();
38-
playwright.close();
22+
public static void main(String[] args) {
23+
try (Playwright playwright = Playwright.create();
24+
Browser browser = playwright.firefox().launch();
25+
BrowserContext context = browser.newContext();
26+
Page page = context.newPage()) {
27+
page.navigate("https://www.example.com/");
28+
Object dimensions = page.evaluate("() => {\n" +
29+
" return {\n" +
30+
" width: document.documentElement.clientWidth,\n" +
31+
" height: document.documentElement.clientHeight,\n" +
32+
" deviceScaleFactor: window.devicePixelRatio\n" +
33+
" }\n" +
34+
"}");
35+
System.out.println(dimensions);
36+
}
3937
}
4038
}

examples/src/main/java/org/example/InterceptNetworkRequests.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,16 @@
1919
import com.microsoft.playwright.*;
2020

2121
public class InterceptNetworkRequests {
22-
public static void main(String[] args) throws Exception {
23-
Playwright playwright = Playwright.create();
24-
BrowserType browserType = playwright.webkit();
25-
Browser browser = browserType.launch();
26-
BrowserContext context = browser.newContext();
27-
Page page = context.newPage();
28-
page.route("**", route -> {
29-
System.out.println(route.request().url());
30-
route.continue_();
31-
});
32-
page.navigate("http://todomvc.com");
33-
browser.close();
34-
playwright.close();
22+
public static void main(String[] args) {
23+
try (Playwright playwright = Playwright.create();
24+
Browser browser = playwright.webkit().launch();
25+
BrowserContext context = browser.newContext();
26+
Page page = context.newPage()) {
27+
page.route("**", route -> {
28+
System.out.println(route.request().url());
29+
route.continue_();
30+
});
31+
page.navigate("http://todomvc.com");
32+
}
3533
}
3634
}

examples/src/main/java/org/example/MobileAndGeolocation.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,18 @@
2323
import static java.util.Arrays.asList;
2424

2525
public class MobileAndGeolocation {
26-
public static void main(String[] args) throws Exception {
27-
Playwright playwright = Playwright.create();
28-
BrowserType browserType = playwright.chromium();
29-
Browser browser = browserType.launch(new BrowserType.LaunchOptions().withHeadless(false));
30-
DeviceDescriptor pixel2 = playwright.devices().get("Pixel 2");
31-
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
32-
.withDevice(pixel2)
33-
.withLocale("en-US")
34-
.withGeolocation(new Geolocation(41.889938, 12.492507))
35-
.withPermissions(asList("geolocation")));
36-
Page page = context.newPage();
37-
page.navigate("https://www.openstreetmap.org/");
38-
page.click("a[data-original-title=\"Show My Location\"]");
39-
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("colosseum-pixel2.png")));
40-
browser.close();
41-
playwright.close();
26+
public static void main(String[] args) {
27+
try (Playwright playwright = Playwright.create();
28+
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().withHeadless(false));
29+
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
30+
.withDevice(playwright.devices().get("Pixel 2"))
31+
.withLocale("en-US")
32+
.withGeolocation(new Geolocation(41.889938, 12.492507))
33+
.withPermissions(asList("geolocation")));
34+
Page page = context.newPage()) {
35+
page.navigate("https://www.openstreetmap.org/");
36+
page.click("a[data-original-title=\"Show My Location\"]");
37+
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("colosseum-pixel2.png")));
38+
}
4239
}
4340
}

examples/src/main/java/org/example/PageScreenshot.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,21 @@
2323
import java.util.List;
2424

2525
public class PageScreenshot {
26-
public static void main(String[] args) throws Exception {
27-
Playwright playwright = Playwright.create();
28-
List<BrowserType> browserTypes = Arrays.asList(
29-
playwright.chromium(),
30-
playwright.webkit(),
31-
playwright.firefox()
32-
);
33-
for (BrowserType browserType : browserTypes) {
34-
Browser browser = browserType.launch();
35-
BrowserContext context = browser.newContext(
36-
new Browser.NewContextOptions().withViewport(800, 600));
37-
Page page = context.newPage();
38-
page.navigate("http://whatsmyuseragent.org/");
39-
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("screenshot-" + browserType.name() + ".png")));
40-
browser.close();
26+
public static void main(String[] args) {
27+
try (Playwright playwright = Playwright.create()) {
28+
List<BrowserType> browserTypes = Arrays.asList(
29+
playwright.chromium(),
30+
playwright.webkit(),
31+
playwright.firefox()
32+
);
33+
for (BrowserType browserType : browserTypes) {
34+
try (Browser browser = browserType.launch();
35+
BrowserContext context = browser.newContext(new Browser.NewContextOptions().withViewport(800, 600));
36+
Page page = context.newPage()) {
37+
page.navigate("http://whatsmyuseragent.org/");
38+
page.screenshot(new Page.ScreenshotOptions().withPath(Paths.get("screenshot-" + browserType.name() + ".png")));
39+
}
40+
}
4141
}
42-
playwright.close();
4342
}
4443
}

playwright/src/main/java/com/microsoft/playwright/Browser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* <p> A Browser is created via [{@code method: BrowserType.launch}]. An example of using a {@code Browser} to create a {@code Page}:
2727
*/
28-
public interface Browser {
28+
public interface Browser extends AutoCloseable {
2929
class VideoSize {
3030
private final int width;
3131
private final int height;

playwright/src/main/java/com/microsoft/playwright/BrowserContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* <p> Playwright allows creation of "incognito" browser contexts with {@code browser.newContext()} method. "Incognito" browser
3434
* contexts don't write any browsing data to disk.
3535
*/
36-
public interface BrowserContext {
36+
public interface BrowserContext extends AutoCloseable {
3737
enum SameSite { STRICT, LAX, NONE }
3838

3939
class HTTPCredentials {

playwright/src/main/java/com/microsoft/playwright/Page.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*
3636
* <p> To unsubscribe from events use the {@code removeListener} method:
3737
*/
38-
public interface Page {
38+
public interface Page extends AutoCloseable {
3939
class Viewport {
4040
private final int width;
4141
private final int height;

playwright/src/main/java/com/microsoft/playwright/Playwright.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Playwright module provides a method to launch a browser instance. The following is a typical example of using Playwright
2424
* to drive automation:
2525
*/
26-
public interface Playwright {
26+
public interface Playwright extends AutoCloseable {
2727
/**
2828
* This object can be used to launch or connect to Chromium, returning instances of {@code ChromiumBrowser}.
2929
*/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.microsoft.playwright;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
/**
24+
* This test simply makes sure that all 4 main interfaces implement AutoCloseable. If it compiles, then it passes.
25+
*/
26+
public class TestAutoClose {
27+
@Test
28+
void shouldAllowUsingTryWithResources() throws Exception {
29+
try (Playwright playwright = Playwright.create();
30+
Browser browser = Utils.getBrowserTypeFromEnv(playwright).launch();
31+
BrowserContext context = browser.newContext();
32+
Page page = context.newPage()) {
33+
assertEquals(2021, page.evaluate("() => 2021"));
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)