public interface Browser extends AutoCloseable
BrowserType.launch()
. An example of using a Browser
to create a
Page
:
import com.microsoft.playwright.*;
public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType firefox = playwright.firefox()
Browser browser = firefox.launch();
Page page = browser.newPage();
page.navigate('https://example.com');
browser.close();
}
}
}
Modifier and Type | Interface and Description |
---|---|
static class |
Browser.NewContextOptions |
static class |
Browser.NewPageOptions |
static class |
Browser.StartTracingOptions |
Modifier and Type | Method and Description |
---|---|
void |
close()
In case this browser is obtained using
BrowserType.launch() , closes the browser and all of
its pages (if any were opened). |
List<BrowserContext> |
contexts()
Returns an array of all open browser contexts.
|
boolean |
isConnected()
Indicates that the browser is connected.
|
default BrowserContext |
newContext()
Creates a new browser context.
|
BrowserContext |
newContext(Browser.NewContextOptions options)
Creates a new browser context.
|
default Page |
newPage()
Creates a new page in a new browser context.
|
Page |
newPage(Browser.NewPageOptions options)
Creates a new page in a new browser context.
|
void |
offDisconnected(Consumer<Browser> handler)
Removes handler that was previously added with
onDisconnected(handler) . |
void |
onDisconnected(Consumer<Browser> handler)
Emitted when Browser gets disconnected from the browser application.
|
default void |
startTracing()
NOTE: This API controls Chromium Tracing
which is a low-level chromium-specific debugging tool.
|
default void |
startTracing(Page page)
NOTE: This API controls Chromium Tracing
which is a low-level chromium-specific debugging tool.
|
void |
startTracing(Page page,
Browser.StartTracingOptions options)
NOTE: This API controls Chromium Tracing
which is a low-level chromium-specific debugging tool.
|
byte[] |
stopTracing()
NOTE: This API controls Chromium Tracing
which is a low-level chromium-specific debugging tool.
|
String |
version()
Returns the browser version.
|
void onDisconnected(Consumer<Browser> handler)
Browser.close()
method was called.void offDisconnected(Consumer<Browser> handler)
onDisconnected(handler)
.void close()
BrowserType.launch()
, closes the browser and all of
its pages (if any were opened).
In case this browser is connected to, clears all created contexts belonging to this browser and disconnects from the browser server.
The Browser
object itself is considered to be disposed and cannot be used anymore.
close
in interface AutoCloseable
List<BrowserContext> contexts()
Browser browser = pw.webkit().launch();
System.out.println(browser.contexts().size()); // prints "0"
BrowserContext context = browser.newContext();
System.out.println(browser.contexts().size()); // prints "1"
boolean isConnected()
default BrowserContext newContext()
Browser browser = playwright.firefox().launch(); // Or 'chromium' or 'webkit'.
// Create a new incognito browser context.
BrowserContext context = browser.newContext();
// Create a new page in a pristine context.
Page page = context.newPage();
page.navigate('https://example.com');
BrowserContext newContext(Browser.NewContextOptions options)
Browser browser = playwright.firefox().launch(); // Or 'chromium' or 'webkit'.
// Create a new incognito browser context.
BrowserContext context = browser.newContext();
// Create a new page in a pristine context.
Page page = context.newPage();
page.navigate('https://example.com');
default Page newPage()
This is a convenience API that should only be used for the single-page scenarios and short snippets. Production code and
testing frameworks should explicitly create Browser.newContext()
followed by the BrowserContext.newPage()
to control their exact life times.
Page newPage(Browser.NewPageOptions options)
This is a convenience API that should only be used for the single-page scenarios and short snippets. Production code and
testing frameworks should explicitly create Browser.newContext()
followed by the BrowserContext.newPage()
to control their exact life times.
default void startTracing(Page page)
You can use Browser.startTracing()
and Browser.stopTracing()
to
create a trace file that can be opened in Chrome DevTools performance panel.
browser.startTracing(page, new Browser.StartTracingOptions()
.setPath(Paths.get("trace.json")));
page.goto('https://www.google.com');
browser.stopTracing();
page
- Optional, if specified, tracing includes screenshots of the given page.default void startTracing()
You can use Browser.startTracing()
and Browser.stopTracing()
to
create a trace file that can be opened in Chrome DevTools performance panel.
browser.startTracing(page, new Browser.StartTracingOptions()
.setPath(Paths.get("trace.json")));
page.goto('https://www.google.com');
browser.stopTracing();
void startTracing(Page page, Browser.StartTracingOptions options)
You can use Browser.startTracing()
and Browser.stopTracing()
to
create a trace file that can be opened in Chrome DevTools performance panel.
browser.startTracing(page, new Browser.StartTracingOptions()
.setPath(Paths.get("trace.json")));
page.goto('https://www.google.com');
browser.stopTracing();
page
- Optional, if specified, tracing includes screenshots of the given page.byte[] stopTracing()
Returns the buffer with trace data.
String version()
Copyright © 2022. All rights reserved.