Class BrowserImpl

All Implemented Interfaces:
Browser, AutoCloseable

class BrowserImpl extends ChannelOwner implements Browser
  • Field Details

  • Constructor Details

  • Method Details

    • onDisconnected

      public void onDisconnected(Consumer<Browser> handler)
      Description copied from interface: Browser
      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.
      Specified by:
      onDisconnected in interface Browser
    • offDisconnected

      public void offDisconnected(Consumer<Browser> handler)
      Description copied from interface: Browser
      Removes handler that was previously added with onDisconnected(handler).
      Specified by:
      offDisconnected in interface Browser
    • browserType

      public BrowserType browserType()
      Description copied from interface: Browser
      Get the browser type (chromium, firefox or webkit) that the browser belongs to.
      Specified by:
      browserType in interface Browser
    • close

      public void close()
      Description copied from interface: Browser
      In case this browser is obtained using 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.

      NOTE: This is similar to force quitting the browser. Therefore, you should call BrowserContext.close() on any BrowserContext's you explicitly created earlier with Browser.newContext() **before** calling Browser.close().

      The Browser object itself is considered to be disposed and cannot be used anymore.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Browser
    • closeImpl

      private void closeImpl()
    • notifyRemoteClosed

      void notifyRemoteClosed()
    • contexts

      public List<BrowserContext> contexts()
      Description copied from interface: Browser
      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"
       
      Specified by:
      contexts in interface Browser
    • isConnected

      public boolean isConnected()
      Description copied from interface: Browser
      Indicates that the browser is connected.
      Specified by:
      isConnected in interface Browser
    • newContext

      public BrowserContextImpl newContext(Browser.NewContextOptions options)
      Description copied from interface: Browser
      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 via BrowserContext.close() when your code is done with the BrowserContext, and before calling Browser.close(). This will ensure the context is 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:
      newContext in interface Browser
    • newContextImpl

      private BrowserContextImpl newContextImpl(Browser.NewContextOptions options)
    • newPage

      public Page newPage(Browser.NewPageOptions options)
      Description copied from interface: Browser
      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 the BrowserContext.newPage() to control their exact life times.

      Specified by:
      newPage in interface Browser
    • startTracing

      public void startTracing(Page page, Browser.StartTracingOptions options)
      Description copied from interface: Browser
      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() and Browser.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:
      startTracing in interface Browser
      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: Browser
      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.

      Specified by:
      stopTracing in interface Browser
    • stopTracingImpl

      private byte[] stopTracingImpl()
    • newPageImpl

      private Page newPageImpl(Browser.NewPageOptions options)
    • name

      private String name()
    • isChromium

      boolean isChromium()
    • version

      public String version()
      Description copied from interface: Browser
      Returns the browser version.
      Specified by:
      version in interface Browser
    • handleEvent

      void handleEvent(String event, com.google.gson.JsonObject parameters)
      Overrides:
      handleEvent in class ChannelOwner
    • newBrowserCDPSession

      public CDPSession newBrowserCDPSession()
      Description copied from interface: Browser
      NOTE: CDP Sessions are only supported on Chromium-based browsers.

      Returns the newly created browser session.

      Specified by:
      newBrowserCDPSession in interface Browser
    • didClose

      private void didClose()