Interface Browser
-
- All Superinterfaces:
AutoCloseable
public interface Browser extends AutoCloseable
A Browser is created viaBrowserType.launch(). An example of using aBrowserto create aPage: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(); } } }
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classBrowser.CloseOptionsstatic classBrowser.NewContextOptionsstatic classBrowser.NewPageOptionsstatic classBrowser.StartTracingOptions
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description BrowserTypebrowserType()Get the browser type (chromium, firefox or webkit) that the browser belongs to.default voidclose()In case this browser is obtained usingBrowserType.launch(), closes the browser and all of its pages (if any were opened).voidclose(Browser.CloseOptions options)In case this browser is obtained usingBrowserType.launch(), closes the browser and all of its pages (if any were opened).List<BrowserContext>contexts()Returns an array of all open browser contexts.booleanisConnected()Indicates that the browser is connected.CDPSessionnewBrowserCDPSession()NOTE: CDP Sessions are only supported on Chromium-based browsers.default BrowserContextnewContext()Creates a new browser context.BrowserContextnewContext(Browser.NewContextOptions options)Creates a new browser context.default PagenewPage()Creates a new page in a new browser context.PagenewPage(Browser.NewPageOptions options)Creates a new page in a new browser context.voidoffDisconnected(Consumer<Browser> handler)Removes handler that was previously added withonDisconnected(handler).voidonDisconnected(Consumer<Browser> handler)Emitted when Browser gets disconnected from the browser application.default voidstartTracing()NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool.default voidstartTracing(Page page)NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool.voidstartTracing(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.Stringversion()Returns the browser version.
-
-
-
Method Detail
-
onDisconnected
void onDisconnected(Consumer<Browser> handler)
Emitted when Browser gets disconnected from the browser application. This might happen because of one of the following:- Browser application is closed or crashed.
- The
Browser.close()method was called.
-
offDisconnected
void offDisconnected(Consumer<Browser> handler)
Removes handler that was previously added withonDisconnected(handler).
-
browserType
BrowserType browserType()
Get the browser type (chromium, firefox or webkit) that the browser belongs to.- Since:
- v1.23
-
close
default void close()
In case this browser is obtained usingBrowserType.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.
NOTE: This is similar to force quitting the browser. Therefore, you should call
BrowserContext.close()on anyBrowserContext's you explicitly created earlier withBrowser.newContext()**before** callingBrowser.close().The
Browserobject itself is considered to be disposed and cannot be used anymore.- Specified by:
closein interfaceAutoCloseable- Since:
- v1.8
-
close
void close(Browser.CloseOptions options)
In case this browser is obtained usingBrowserType.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.
NOTE: This is similar to force quitting the browser. Therefore, you should call
BrowserContext.close()on anyBrowserContext's you explicitly created earlier withBrowser.newContext()**before** callingBrowser.close().The
Browserobject itself is considered to be disposed and cannot be used anymore.- Since:
- v1.8
-
contexts
List<BrowserContext> contexts()
Returns an array of all open browser contexts. In a newly created browser, this will return zero browser contexts.Usage
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"- Since:
- v1.8
-
isConnected
boolean isConnected()
Indicates that the browser is connected.- Since:
- v1.8
-
newBrowserCDPSession
CDPSession newBrowserCDPSession()
NOTE: CDP Sessions are only supported on Chromium-based browsers.Returns the newly created browser session.
- Since:
- v1.11
-
newContext
default BrowserContext newContext()
Creates a new browser context. It won't share cookies/cache with other browser contexts.NOTE: If directly using this method to create
BrowserContexts, it is best practice to explicitly close the returned context viaBrowserContext.close()when your code is done with theBrowserContext, and before callingBrowser.close(). This will ensure thecontextis closed gracefully and any artifacts—like HARs and videos—are fully flushed and saved.Usage
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'); // Graceful close up everything context.close(); browser.close();- Since:
- v1.8
-
newContext
BrowserContext newContext(Browser.NewContextOptions options)
Creates a new browser context. It won't share cookies/cache with other browser contexts.NOTE: If directly using this method to create
BrowserContexts, it is best practice to explicitly close the returned context viaBrowserContext.close()when your code is done with theBrowserContext, and before callingBrowser.close(). This will ensure thecontextis closed gracefully and any artifacts—like HARs and videos—are fully flushed and saved.Usage
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'); // Graceful close up everything context.close(); browser.close();- Since:
- v1.8
-
newPage
default Page newPage()
Creates a new page in a new browser context. Closing this page will close the context as well.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 theBrowserContext.newPage()to control their exact life times.- Since:
- v1.8
-
newPage
Page newPage(Browser.NewPageOptions options)
Creates a new page in a new browser context. Closing this page will close the context as well.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 theBrowserContext.newPage()to control their exact life times.- Since:
- v1.8
-
startTracing
default void startTracing(Page page)
NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool. API to control Playwright Tracing could be found here.You can use
Browser.startTracing()andBrowser.stopTracing()to create a trace file that can be opened in Chrome DevTools performance panel.Usage
browser.startTracing(page, new Browser.StartTracingOptions() .setPath(Paths.get("trace.json"))); page.goto('https://www.google.com'); browser.stopTracing();- Parameters:
page- Optional, if specified, tracing includes screenshots of the given page.- Since:
- v1.11
-
startTracing
default void startTracing()
NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool. API to control Playwright Tracing could be found here.You can use
Browser.startTracing()andBrowser.stopTracing()to create a trace file that can be opened in Chrome DevTools performance panel.Usage
browser.startTracing(page, new Browser.StartTracingOptions() .setPath(Paths.get("trace.json"))); page.goto('https://www.google.com'); browser.stopTracing();- Since:
- v1.11
-
startTracing
void startTracing(Page page, Browser.StartTracingOptions options)
NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool. API to control Playwright Tracing could be found here.You can use
Browser.startTracing()andBrowser.stopTracing()to create a trace file that can be opened in Chrome DevTools performance panel.Usage
browser.startTracing(page, new Browser.StartTracingOptions() .setPath(Paths.get("trace.json"))); page.goto('https://www.google.com'); browser.stopTracing();- Parameters:
page- Optional, if specified, tracing includes screenshots of the given page.- Since:
- v1.11
-
stopTracing
byte[] stopTracing()
NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool. API to control Playwright Tracing could be found here.Returns the buffer with trace data.
- Since:
- v1.11
-
version
String version()
Returns the browser version.- Since:
- v1.8
-
-