Class BrowserImpl
- java.lang.Object
-
- com.microsoft.playwright.impl.LoggingSupport
-
- com.microsoft.playwright.impl.ChannelOwner
-
- com.microsoft.playwright.impl.BrowserImpl
-
- All Implemented Interfaces:
Browser,java.lang.AutoCloseable
class BrowserImpl extends ChannelOwner implements Browser
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classBrowserImpl.EventType-
Nested classes/interfaces inherited from interface com.microsoft.playwright.Browser
Browser.NewContextOptions, Browser.NewPageOptions, Browser.StartTracingOptions
-
-
Field Summary
Fields Modifier and Type Field Description (package private) BrowserTypeImplbrowserType(package private) java.util.Set<BrowserContextImpl>contextsprivate booleanisConnected(package private) booleanisConnectedOverWebSocket(package private) BrowserType.LaunchOptionslaunchOptionsprivate ListenerCollection<BrowserImpl.EventType>listenersprivate java.nio.file.PathtracePath-
Fields inherited from class com.microsoft.playwright.impl.ChannelOwner
connection, guid, initializer, type
-
-
Constructor Summary
Constructors Constructor Description BrowserImpl(ChannelOwner parent, java.lang.String type, java.lang.String guid, com.google.gson.JsonObject initializer)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BrowserTypebrowserType()Get 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 voidcloseImpl()java.util.List<BrowserContext>contexts()Returns an array of all open browser contexts.private voiddidClose()(package private) voidhandleEvent(java.lang.String event, com.google.gson.JsonObject parameters)(package private) booleanisChromium()booleanisConnected()Indicates that the browser is connected.private java.lang.Stringname()CDPSessionnewBrowserCDPSession()NOTE: CDP Sessions are only supported on Chromium-based browsers.BrowserContextImplnewContext(Browser.NewContextOptions options)Creates a new browser context.private BrowserContextImplnewContextImpl(Browser.NewContextOptions options)PagenewPage(Browser.NewPageOptions options)Creates a new page in a new browser context.private PagenewPageImpl(Browser.NewPageOptions options)(package private) voidnotifyRemoteClosed()voidoffDisconnected(java.util.function.Consumer<Browser> handler)Removes handler that was previously added withonDisconnected(handler).voidonDisconnected(java.util.function.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[]stopTracing()NOTE: This API controls Chromium Tracing which is a low-level chromium-specific debugging tool.private byte[]stopTracingImpl()java.lang.Stringversion()Returns the browser version.-
Methods inherited from class com.microsoft.playwright.impl.ChannelOwner
adopt, disposeChannelOwner, runUntil, sendMessage, sendMessage, sendMessageAsync, toProtocolRef, withLogging, withWaitLogging
-
Methods inherited from class com.microsoft.playwright.impl.LoggingSupport
logApi, logApiIfEnabled, logWithTimestamp, withLogging
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.microsoft.playwright.Browser
newContext, newPage, startTracing, startTracing
-
-
-
-
Field Detail
-
contexts
final java.util.Set<BrowserContextImpl> contexts
-
listeners
private final ListenerCollection<BrowserImpl.EventType> listeners
-
isConnectedOverWebSocket
boolean isConnectedOverWebSocket
-
isConnected
private boolean isConnected
-
browserType
BrowserTypeImpl browserType
-
launchOptions
BrowserType.LaunchOptions launchOptions
-
tracePath
private java.nio.file.Path tracePath
-
-
Constructor Detail
-
BrowserImpl
BrowserImpl(ChannelOwner parent, java.lang.String type, java.lang.String guid, com.google.gson.JsonObject initializer)
-
-
Method Detail
-
onDisconnected
public void onDisconnected(java.util.function.Consumer<Browser> handler)
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
public void offDisconnected(java.util.function.Consumer<Browser> handler)
Description copied from interface:BrowserRemoves handler that was previously added withonDisconnected(handler).- Specified by:
offDisconnectedin interfaceBrowser
-
browserType
public BrowserType 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.
-
closeImpl
private void closeImpl()
-
notifyRemoteClosed
void notifyRemoteClosed()
-
contexts
public java.util.List<BrowserContext> 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
public BrowserContextImpl newContext(Browser.NewContextOptions options)
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
private BrowserContextImpl newContextImpl(Browser.NewContextOptions options)
-
newPage
public Page newPage(Browser.NewPageOptions options)
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
public void startTracing(Page page, Browser.StartTracingOptions options)
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
private void startTracingImpl(Page page, Browser.StartTracingOptions options)
-
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
private Page newPageImpl(Browser.NewPageOptions options)
-
name
private java.lang.String name()
-
isChromium
boolean isChromium()
-
version
public java.lang.String version()
Description copied from interface:BrowserReturns the browser version.
-
handleEvent
void handleEvent(java.lang.String event, com.google.gson.JsonObject parameters)- Overrides:
handleEventin classChannelOwner
-
newBrowserCDPSession
public CDPSession 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()
-
-