Skip to content

Commit 02bd360

Browse files
authored
feat: roll driver, implement context tracing and network APIs (microsoft#444)
1 parent bcf879b commit 02bd360

File tree

15 files changed

+536
-39
lines changed

15 files changed

+536
-39
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,55 @@ public interface BrowserContext extends AutoCloseable {
8181
*/
8282
void offPage(Consumer<Page> handler);
8383

84+
/**
85+
* Emitted when a request is issued from any pages created through this context. The [request] object is read-only. To only
86+
* listen for requests from a particular page, use {@link Page#onRequest Page.onRequest()}.
87+
*
88+
* <p> In order to intercept and mutate requests, see {@link BrowserContext#route BrowserContext.route()} or {@link Page#route
89+
* Page.route()}.
90+
*/
91+
void onRequest(Consumer<Request> handler);
92+
/**
93+
* Removes handler that was previously added with {@link #onRequest onRequest(handler)}.
94+
*/
95+
void offRequest(Consumer<Request> handler);
96+
97+
/**
98+
* Emitted when a request fails, for example by timing out. To only listen for failed requests from a particular page, use
99+
* {@link Page#onRequestFailed Page.onRequestFailed()}.
100+
*
101+
* <p> <strong>NOTE:</strong> HTTP Error responses, such as 404 or 503, are still successful responses from HTTP standpoint, so request will complete
102+
* with {@link BrowserContext#onRequestFinished BrowserContext.onRequestFinished()} event and not with {@link
103+
* BrowserContext#onRequestFailed BrowserContext.onRequestFailed()}.
104+
*/
105+
void onRequestFailed(Consumer<Request> handler);
106+
/**
107+
* Removes handler that was previously added with {@link #onRequestFailed onRequestFailed(handler)}.
108+
*/
109+
void offRequestFailed(Consumer<Request> handler);
110+
111+
/**
112+
* Emitted when a request finishes successfully after downloading the response body. For a successful response, the
113+
* sequence of events is {@code request}, {@code response} and {@code requestfinished}. To listen for successful requests from a particular
114+
* page, use {@link Page#onRequestFinished Page.onRequestFinished()}.
115+
*/
116+
void onRequestFinished(Consumer<Request> handler);
117+
/**
118+
* Removes handler that was previously added with {@link #onRequestFinished onRequestFinished(handler)}.
119+
*/
120+
void offRequestFinished(Consumer<Request> handler);
121+
122+
/**
123+
* Emitted when [response] status and headers are received for a request. For a successful response, the sequence of events
124+
* is {@code request}, {@code response} and {@code requestfinished}. To listen for response events from a particular page, use {@link
125+
* Page#onResponse Page.onResponse()}.
126+
*/
127+
void onResponse(Consumer<Response> handler);
128+
/**
129+
* Removes handler that was previously added with {@link #onResponse onResponse(handler)}.
130+
*/
131+
void offResponse(Consumer<Response> handler);
132+
84133
class ExposeBindingOptions {
85134
/**
86135
* Whether to pass the argument as a handle, instead of passing by value. When passing a handle, only one argument is
@@ -664,6 +713,7 @@ default String storageState() {
664713
* Returns storage state for this browser context, contains current cookies and local storage snapshot.
665714
*/
666715
String storageState(StorageStateOptions options);
716+
Tracing tracing();
667717
/**
668718
* Removes a route created with {@link BrowserContext#route BrowserContext.route()}. When {@code handler} is not specified,
669719
* removes all routes for the {@code url}.

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class LaunchOptions {
111111
*/
112112
public BrowserChannel channel;
113113
/**
114-
* Enable Chromium sandboxing. Defaults to {@code false}.
114+
* Enable Chromium sandboxing. Defaults to {@code true}.
115115
*/
116116
public Boolean chromiumSandbox;
117117
/**
@@ -181,6 +181,10 @@ class LaunchOptions {
181181
* disable timeout.
182182
*/
183183
public Double timeout;
184+
/**
185+
* If specified, traces are saved into this directory.
186+
*/
187+
public Path traceDir;
184188

185189
public LaunchOptions setArgs(List<String> args) {
186190
this.args = args;
@@ -253,6 +257,10 @@ public LaunchOptions setTimeout(double timeout) {
253257
this.timeout = timeout;
254258
return this;
255259
}
260+
public LaunchOptions setTraceDir(Path traceDir) {
261+
this.traceDir = traceDir;
262+
return this;
263+
}
256264
}
257265
class LaunchPersistentContextOptions {
258266
/**
@@ -302,8 +310,8 @@ class LaunchPersistentContextOptions {
302310
public Map<String, String> env;
303311
/**
304312
* Path to a browser executable to run instead of the bundled one. If {@code executablePath} is a relative path, then it is
305-
* resolved relative to the current working directory. **BEWARE**: Playwright is only guaranteed to work with the bundled
306-
* Chromium, Firefox or WebKit, use at your own risk.
313+
* resolved relative to the current working directory. Note that Playwright only works with the bundled Chromium, Firefox
314+
* or WebKit, use at your own risk.
307315
*/
308316
public Path executablePath;
309317
/**
@@ -407,7 +415,6 @@ class LaunchPersistentContextOptions {
407415
public ScreenSize screenSize;
408416
/**
409417
* Slows down Playwright operations by the specified amount of milliseconds. Useful so that you can see what is going on.
410-
* Defaults to 0.
411418
*/
412419
public Double slowMo;
413420
/**
@@ -421,6 +428,10 @@ class LaunchPersistentContextOptions {
421428
* metaZones.txt</a> for a list of supported timezone IDs.
422429
*/
423430
public String timezoneId;
431+
/**
432+
* If specified, traces are saved into this directory.
433+
*/
434+
public Path traceDir;
424435
/**
425436
* Specific user agent to use in this context.
426437
*/
@@ -589,6 +600,10 @@ public LaunchPersistentContextOptions setTimezoneId(String timezoneId) {
589600
this.timezoneId = timezoneId;
590601
return this;
591602
}
603+
public LaunchPersistentContextOptions setTraceDir(Path traceDir) {
604+
this.traceDir = traceDir;
605+
return this;
606+
}
592607
public LaunchPersistentContextOptions setUserAgent(String userAgent) {
593608
this.userAgent = userAgent;
594609
return this;

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
/**
2424
* {@code Download} objects are dispatched by page via the {@link Page#onDownload Page.onDownload()} event.
2525
*
26-
* <p> All the downloaded files belonging to the browser context are deleted when the browser context is closed. All downloaded
27-
* files are deleted when the browser closes.
26+
* <p> If {@code downloadsPath} isn't specified, all the downloaded files belonging to the browser context are deleted when the
27+
* browser context is closed. And all downloaded files are deleted when the browser closes.
2828
*
2929
* <p> Download event is emitted once the download starts. Download path becomes available once download completes:
3030
* <pre>{@code
@@ -59,15 +59,20 @@ public interface Download {
5959
* Returns download error if any. Will wait for the download to finish if necessary.
6060
*/
6161
String failure();
62+
/**
63+
* Get the page that the download belongs to.
64+
*/
65+
Page page();
6266
/**
6367
* Returns path to the downloaded file in case of successful download. The method will wait for the download to finish if
6468
* necessary. The method throws when connected remotely.
6569
*/
6670
Path path();
6771
/**
68-
* Saves the download to a user-specified path. It is safe to call this method while the download is still in progress.
72+
* Copy the download to a user-specified path. It is safe to call this method while the download is still in progress. Will
73+
* wait for the download to finish if necessary.
6974
*
70-
* @param path Path where the download should be saved.
75+
* @param path Path where the download should be copied.
7176
*/
7277
void saveAs(Path path);
7378
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3142,7 +3142,8 @@ default void press(String selector, String key) {
31423142
void press(String selector, String key, PressOptions options);
31433143
/**
31443144
* The method finds an element matching the specified selector within the page. If no elements match the selector, the
3145-
* return value resolves to {@code null}.
3145+
* return value resolves to {@code null}. To wait for an element on the page, use {@link Page#waitForSelector
3146+
* Page.waitForSelector()}.
31463147
*
31473148
* <p> Shortcut for main frame's {@link Frame#querySelector Frame.querySelector()}.
31483149
*
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 java.nio.file.Path;
20+
import java.util.*;
21+
22+
/**
23+
* API for collecting and saving Playwright traces. Playwright traces can be opened using the Playwright CLI after
24+
* Playwright script runs.
25+
*
26+
* <p> Start with specifying the folder traces will be stored in:
27+
* <pre>{@code
28+
* Browser browser = chromium.launch(new BrowserType.LaunchOptions().setTraceDir("trace"));
29+
* BrowserContext context = browser.newContext();
30+
* context.tracing.start(page, new Tracing.StartOptions()
31+
* .setName("trace")
32+
* .setScreenshots(true)
33+
* .setSnapshots(true);
34+
* Page page = context.newPage();
35+
* page.goto("https://playwright.dev");
36+
* context.tracing.stop();
37+
* context.tracing.export(Paths.get("trace.zip")))
38+
* }</pre>
39+
*/
40+
public interface Tracing {
41+
class StartOptions {
42+
/**
43+
* If specified, the trace is going to be saved into the file with the given name inside the {@code traceDir} folder specified in
44+
* {@link BrowserType#launch BrowserType.launch()}.
45+
*/
46+
public String name;
47+
/**
48+
* Whether to capture screenshots during tracing. Screenshots are used to build a timeline preview.
49+
*/
50+
public Boolean screenshots;
51+
/**
52+
* Whether to capture DOM snapshot on every action.
53+
*/
54+
public Boolean snapshots;
55+
56+
public StartOptions setName(String name) {
57+
this.name = name;
58+
return this;
59+
}
60+
public StartOptions setScreenshots(boolean screenshots) {
61+
this.screenshots = screenshots;
62+
return this;
63+
}
64+
public StartOptions setSnapshots(boolean snapshots) {
65+
this.snapshots = snapshots;
66+
return this;
67+
}
68+
}
69+
/**
70+
* Export trace into the file with the given name. Should be called after the tracing has stopped.
71+
*
72+
* @param path File to save the trace into.
73+
*/
74+
void export(Path path);
75+
/**
76+
* Start tracing.
77+
* <pre>{@code
78+
* context.tracing.start(page, new Tracing.StartOptions()
79+
* .setName("trace")
80+
* .setScreenshots(true)
81+
* .setSnapshots(true);
82+
* Page page = context.newPage();
83+
* page.goto('https://playwright.dev');
84+
* context.tracing.stop();
85+
* context.tracing.export(Paths.get("trace.zip")))
86+
* }</pre>
87+
*/
88+
default void start() {
89+
start(null);
90+
}
91+
/**
92+
* Start tracing.
93+
* <pre>{@code
94+
* context.tracing.start(page, new Tracing.StartOptions()
95+
* .setName("trace")
96+
* .setScreenshots(true)
97+
* .setSnapshots(true);
98+
* Page page = context.newPage();
99+
* page.goto('https://playwright.dev');
100+
* context.tracing.stop();
101+
* context.tracing.export(Paths.get("trace.zip")))
102+
* }</pre>
103+
*/
104+
void start(StartOptions options);
105+
/**
106+
* Stop tracing.
107+
*/
108+
void stop();
109+
}
110+

0 commit comments

Comments
 (0)