Class Example03


  • public class Example03
    extends java.lang.Object
    Demonstrates several CDP browser features without using JavaScript evaluation. This example demonstrates how to use the Chrome DevTools Protocol (CDP) from Java in order to:


    Example Features:
    • Launch and connect to a browser
    • Navigate to a live web page
    • Generate screenshots
    • Generate PDF files
    • Capture cookies and network events


    💡 Once the fonts are installed correctly, Example03 demonstrates that CDP is fully capable of rendering:

    • Unicode web pages
    • Traditional Chinese text
    • Complex HTML layouts
    • Multi-page PDF documents
    • Embedded charts and images

    ... directly from a completely headless browser session.


    Installing & Starting Chrome:
    If you are using the processor on a computer inside your office or on your laptop, there is probably not any need to install a "Chrome CDP Compliant Browser." However, if you are using a virtual machine, you might need to run a browser installations script.

    ⚠️ If you are running this example from a cloud-based Linux VM such as: GCP, AWS, or Azure, please review the following helper scripts for installing & starting a Web Browser instance in the cloud. Cloud based accounts do not necessarily have pre-installed browser executables.




    File Explanations:
    • install.sh - script installs the minimum Linux packages required to run headless Chrome.

    • chinese-fonts.sh script installs additional CJK (Chinese / Japanese / Korean) Unicode fonts needed by Example03.java.

    • start.sh - script launches Chrome in headless mode, with remote debugging enabled on TCP-Port 9222.

    If the fonts are not installed beforehand, then generated screenshots and PDF files may contain empty Unicode square-box glyphs instead of Chinese text.


    Supported Browsers:
    Modern versions of these browsers are, generally, compliant with the CDP API. You may view a somewhat "extensive" explanation by Chat-GPT regarding which browsers will support the Chromium Engine CDP API, below. Thee list after the link below is a "quick summary" of what to look for when running this Java Tool.

    Supported Browsers

    • Google Chrome
    • Chromium
    • Microsoft Edge
    • Brave Browser


    • Field Detail

      • chineseURL

        🡇     🗕  🗗  🗖
        protected static final java.lang.String chineseURL
        The URL used for demonstrating Network, Page, Screenshot, PDF and Cookie commands.
        See Also:
        Constant Field Values
        Code:
        Exact Field Declaration Expression:
         protected static final String chineseURL =
                 "https://zh.wikipedia.org/wiki/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD";
        
      • networkURLs

        🡅  🡇     🗕  🗗  🗖
        protected static final java.util.List<java.lang.String> networkURLs
        Network URLs collected by the page-level handler.
        Code:
        Exact Field Declaration Expression:
         protected static final List<String> networkURLs = new ArrayList<>();
        
    • Method Detail

      • main

        🡅  🡇     🗕  🗗  🗖
        public static void main​(java.lang.String[] argv)
                         throws java.lang.Exception
        This class is intended to be invoked from the Command Line.
        Throws:
        java.lang.Exception
        Code:
        Exact Method Body:
         STEP_01_initializeBrowserAndPage();
        
         try
         {
             STEP_02_enablePageAndNetwork(connRec.pws);
        
             STEP_03_navigateAndCollectNetworkTraffic(connRec.pws);
        
             STEP_04_captureScreenshot(connRec.pws);
        
             STEP_05_printToPDF(connRec.pws);
        
             STEP_06_printCookies(connRec.pws);
         }
        
         finally
         {
             connRec.bws.disconnect();
             connRec.pws.disconnect();
         }
        
      • STEP_01_initializeBrowserAndPage

        🡅  🡇     🗕  🗗  🗖
        protected static void STEP_01_initializeBrowserAndPage()
                                                        throws java.lang.Exception
        Open Chrome, close old tabs, create a new tab, and return both WebSocket senders.
        Throws:
        java.lang.Exception
        Code:
        Exact Method Body:
         Printing.notice("Initialize BrowserConn, PageConn and WebSocketSender");
        
         final BrowserConn browserConn = BrowserConn.getBrowserConn(9222, false);
        
         System.out.println(
             '\n' + BCYAN + "Example03.java: " + RESET +
             BRED + "Opened Browser Connection:\n" + RESET + browserConn.toString()
         );
        
         final WebSocketSender bws = browserConn.createSender(connRec);
        
         final Target.TargetInfo[] allTabs = Target
             .getTargets(null /* FilterEntry[] */)
             .exec(bws)
             .await();
        
         for (int i = 0; i < allTabs.length; i++)
             if ("page".equals(allTabs[i].type))
                 Target.closeTarget(allTabs[i].targetId).exec(bws).await();
        
         final String targetID = Target
             .createTarget()
             .accept("url", "about:blank")
             .build()
             .exec(bws)
             .await();
        
         final PageConn pageConn = PageConn
             .getAllPageConn(9222, false)
             .filter((PageConn pc) -> pc.id.equals(targetID))
             .findFirst()
             .orElseThrow(() -> new RuntimeException("The Page-Connection was Not Found"));
        
         System.out.println(
             '\n' + BCYAN + "Example03.java: " + RESET +
             BRED + "Opened Page Connection:\n" + RESET + pageConn.toString()
         );
        
         final WebSocketSender pws = pageConn.createSender(connRec);
        
         Thread.sleep(1000);
        
         connRec.bws = bws;
         connRec.pws = pws;
        
      • STEP_02_enablePageAndNetwork

        🡅  🡇     🗕  🗗  🗖
        protected static void STEP_02_enablePageAndNetwork​(WebSocketSender pws)
                                                    throws java.lang.Exception
        Enables the two CDP domains used throughout this example.
        Throws:
        java.lang.Exception
        Code:
        Exact Method Body:
         Printing.notice("Enable Page and Network Domains");
        
         System.out.println('\n' + BCYAN + "Example03.java: " + RESET + "Page.enable()");
         Page.enable(null /* enableFileChooserOpenedEvent */).exec(connRec.pws).await();
        
         System.out.println('\n' + BCYAN + "Example03.java: " + RESET + "Network.enable()");
         Network
             .enable(
                 null,   // maxTotalBufferSize
                 null,   // maxResourceBufferSize
                 null,   // maxPostDataSize,
                 null    // reportDirectSocketTraffic
             )
             .exec(connRec.pws)
             .await();
        
      • STEP_03_navigateAndCollectNetworkTraffic

        🡅  🡇     🗕  🗗  🗖
        protected static void STEP_03_navigateAndCollectNetworkTraffic​
                    (WebSocketSender pws)
                throws java.lang.Exception
        
        Navigate to the target page and allow Network events to arrive through the handler.
        Throws:
        java.lang.Exception
        Code:
        Exact Method Body:
         Printing.notice("Navigate and Collect Network Traffic");
        
         System.out.println(
             '\n' + BCYAN + "Example03.java: " + RESET +
             "Navigating To: " + chineseURL
         );
        
         Page
             .navigate()
             .accept("url", chineseURL)
             .build()
             .exec(connRec.pws)
             .await();
        
         Thread.sleep(5000);
        
         System.out.println(
             '\n' + BCYAN + "Example03.java: " + RESET +
             BRED + "Total Network Request URL's Collected: " + RESET +
             networkURLs.size()
         );
        
         for (int i = 0; i < networkURLs.size(); i++)
             System.out.println("    " + networkURLs.get(i));
        
      • STEP_04_captureScreenshot

        🡅  🡇     🗕  🗗  🗖
        protected static void STEP_04_captureScreenshot​(WebSocketSender pws)
                                                 throws java.lang.Exception
        Capture the rendered browser page as a PNG file.
        Throws:
        java.lang.Exception
        Code:
        Exact Method Body:
         Printing.notice("Capture a PNG Screenshot");
        
         final String pngBase64 = Page
             .captureScreenshot()
             .accept("format", "png")
             .accept("fromSurface", true)
             .build()
             .exec(connRec.pws)
             .await();
        
         Files.write(Paths.get(screenshotFile), Base64.getDecoder().decode(pngBase64));
        
         System.out.println(
             '\n' + BCYAN + "Example03.java: " + RESET +
             BRED + "Wrote Screenshot File: " + RESET + screenshotFile
         );
        
      • STEP_05_printToPDF

        🡅  🡇     🗕  🗗  🗖
        protected static void STEP_05_printToPDF​(WebSocketSender pws)
                                          throws java.lang.Exception
        Print the rendered browser page to a PDF file.
        Throws:
        java.lang.Exception
        Code:
        Exact Method Body:
         Printing.notice("Print the Page to a PDF File");
        
         final Page.printToPDF$$RET pdf = Page
             .printToPDF()
             .accept("printBackground", true)
             .build()
             .exec(connRec.pws)
             .await();
        
         Files.write(Paths.get(pdfFile), Base64.getDecoder().decode(pdf.data));
        
         System.out.println(
             '\n' + BCYAN + "Example03.java: " + RESET +
             BRED + "Wrote PDF File: " + RESET + pdfFile
         );
        
      • STEP_06_printCookies

        🡅     🗕  🗗  🗖
        protected static void STEP_06_printCookies​(WebSocketSender pws)
                                            throws java.lang.Exception
        Print the cookies visible to Chrome for the current page.
        Throws:
        java.lang.Exception
        Code:
        Exact Method Body:
         Printing.notice("Read Browser Cookies for the Current Page");
        
         final Network.Cookie[] cookies = Network
             .getCookies(null /* String[] urls */)
             .exec(connRec.pws)
             .await();
        
         System.out.println(
             '\n' + BCYAN + "Example03.java: " + RESET +
             BRED + "Cookies Returned: " + RESET + cookies.length
         );
        
         for (int i = 0; i < cookies.length; i++)
             System.out.println(StrIndent.indent(cookies[i].toString(), 4));