Interface Browser

  • All Superinterfaces:
    AutoCloseable

    public interface Browser
    extends AutoCloseable
    A Browser is created via 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();
         }
       }
     }
     
    • 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.
      • 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 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
        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 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();
         
        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 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();
         
        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 the BrowserContext.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 the BrowserContext.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() 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();
         
        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() 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();
         
        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() 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();
         
        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