Interface Browser
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
BrowserImpl
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();
}
}
}
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classstatic classstatic class -
Method Summary
Modifier and TypeMethodDescriptionGet the browser type (chromium, firefox or webkit) that the browser belongs to.voidclose()In case this browser is obtained usingBrowserType.launch(), closes the browser and all of its pages (if any were opened).contexts()Returns an array of all open browser contexts.booleanIndicates that the browser is connected.NOTE: CDP Sessions are only supported on Chromium-based browsers.default BrowserContextCreates a new browser context.newContext(Browser.NewContextOptions options) Creates a new browser context.default PagenewPage()Creates a new page in a new browser context.newPage(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 voidNOTE: 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[]NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool.version()Returns the browser version.
-
Method Details
-
onDisconnected
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
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
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
-
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
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
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
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
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
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
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
-