Class BrowserImpl
- All Implemented Interfaces:
Browser,AutoCloseable
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface com.microsoft.playwright.Browser
Browser.NewContextOptions, Browser.NewPageOptions, Browser.StartTracingOptions -
Field Summary
FieldsModifier and TypeFieldDescription(package private) BrowserTypeImpl(package private) final Set<BrowserContextImpl> private boolean(package private) boolean(package private) BrowserType.LaunchOptionsprivate final ListenerCollection<BrowserImpl.EventType> private PathFields inherited from class com.microsoft.playwright.impl.ChannelOwner
connection, guid, initializer, type -
Constructor Summary
ConstructorsConstructorDescriptionBrowserImpl(ChannelOwner parent, String type, String guid, com.google.gson.JsonObject initializer) -
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).private voidcontexts()Returns an array of all open browser contexts.private voiddidClose()(package private) voidhandleEvent(String event, com.google.gson.JsonObject parameters) (package private) booleanbooleanIndicates that the browser is connected.private Stringname()NOTE: CDP Sessions are only supported on Chromium-based browsers.newContext(Browser.NewContextOptions options) Creates a new browser context.private BrowserContextImplnewContextImpl(Browser.NewContextOptions options) newPage(Browser.NewPageOptions options) Creates a new page in a new browser context.private PagenewPageImpl(Browser.NewPageOptions options) (package private) voidvoidoffDisconnected(Consumer<Browser> handler) Removes handler that was previously added withonDisconnected(handler).voidonDisconnected(Consumer<Browser> handler) Emitted when Browser gets disconnected from the browser application.voidstartTracing(Page page, Browser.StartTracingOptions options) NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool.private voidstartTracingImpl(Page page, Browser.StartTracingOptions options) byte[]NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool.private byte[]version()Returns the browser version.Methods inherited from class com.microsoft.playwright.impl.ChannelOwner
adopt, disposeChannelOwner, runUntil, sendMessage, sendMessage, sendMessageAsync, toProtocolRef, withLogging, withWaitLoggingMethods inherited from class com.microsoft.playwright.impl.LoggingSupport
logApi, logApiIfEnabled, logWithTimestamp, withLoggingMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.microsoft.playwright.Browser
newContext, newPage, startTracing, startTracing
-
Field Details
-
contexts
-
listeners
-
isConnectedOverWebSocket
boolean isConnectedOverWebSocket -
isConnected
private boolean isConnected -
browserType
BrowserTypeImpl browserType -
launchOptions
BrowserType.LaunchOptions launchOptions -
tracePath
-
-
Constructor Details
-
BrowserImpl
BrowserImpl(ChannelOwner parent, String type, String guid, com.google.gson.JsonObject initializer)
-
-
Method Details
-
onDisconnected
Description copied from interface:BrowserEmitted 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.
- Specified by:
onDisconnectedin interfaceBrowser
-
offDisconnected
Description copied from interface:BrowserRemoves handler that was previously added withonDisconnected(handler).- Specified by:
offDisconnectedin interfaceBrowser
-
browserType
Description copied from interface:BrowserGet the browser type (chromium, firefox or webkit) that the browser belongs to.- Specified by:
browserTypein interfaceBrowser
-
close
public void close()Description copied from interface:BrowserIn 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- Specified by:
closein interfaceBrowser
-
closeImpl
private void closeImpl() -
notifyRemoteClosed
void notifyRemoteClosed() -
contexts
Description copied from interface:BrowserReturns 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" -
isConnected
public boolean isConnected()Description copied from interface:BrowserIndicates that the browser is connected.- Specified by:
isConnectedin interfaceBrowser
-
newContext
Description copied from interface:BrowserCreates 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();- Specified by:
newContextin interfaceBrowser
-
newContextImpl
-
newPage
Description copied from interface:BrowserCreates 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. -
startTracing
Description copied from interface:BrowserNOTE: 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();- Specified by:
startTracingin interfaceBrowser- Parameters:
page- Optional, if specified, tracing includes screenshots of the given page.
-
startTracingImpl
-
stopTracing
public byte[] stopTracing()Description copied from interface:BrowserNOTE: 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.
- Specified by:
stopTracingin interfaceBrowser
-
stopTracingImpl
private byte[] stopTracingImpl() -
newPageImpl
-
name
-
isChromium
boolean isChromium() -
version
Description copied from interface:BrowserReturns the browser version. -
handleEvent
- Overrides:
handleEventin classChannelOwner
-
newBrowserCDPSession
Description copied from interface:BrowserNOTE: CDP Sessions are only supported on Chromium-based browsers.Returns the newly created browser session.
- Specified by:
newBrowserCDPSessionin interfaceBrowser
-
didClose
private void didClose()
-