Skip to content

deripas/chrome-devtools-java

Repository files navigation

Chrome DevTools Java

maven version

Chrome DevTools Java is a Java client for working with the Chrome DevTools Protocol.

Note: This project is mainly developed to understand how the Chrome DevTools Protocol works. It is not yet ready for production use.


👥 Alternatives

If you're looking for other libraries to interact with the Chrome DevTools Protocol, here are some popular alternatives:


📁 Project Structure


📦 Adding the Library

To include the library in your project, add the following dependency to your pom.xml (if you're using Maven):

<dependency>
    <groupId>io.github.deripas</groupId>
    <artifactId>chrome-devtools-client</artifactId>
    <version>${chrome-devtools-client.version}</version>
</dependency>

🖥 Usage Examples

Using the Simplified DSL Wrapper:

A DSL wrapper was implemented to simplify working with the protocol, minimizing the complexity of the low-level API.

try (final BrowserDsl browser = DSL.connect("http://localhost:9222")) {
    final BrowserContextDsl context = browser.createContext();
    final PageDsl page = context.createPage();
    page.navigate("https://www.google.com");
    Thread.sleep(1000);
    byte[] bytes = page.screenshot(Page.CaptureScreenshotRequest.Format.PNG);
    Files.write(Path.of("screenshot.png"), bytes);
}

Using the Raw API:

The raw API provides full control over all aspects of interacting with the protocol but can feel less convenient for beginners.

Session session = CDP.createDefault()
    .connect(URI.create("http://localhost:9222"))
    .get();
Protocol protocol = new Protocol(session);
Target.CreateBrowserContextResponse browserContextResponse = protocol.getTarget()
    .createBrowserContext(Target.CreateBrowserContextRequest
        .builder()
        .disposeOnDetach(true)
        .build())
    .get();
Target.CreateTargetResponse targetResponse = protocol.getTarget()
    .createTarget(Target.CreateTargetRequest.builder()
        .url("about:blank")
        .browserContextId(browserContextResponse.getBrowserContextId())
        .build())
    .get();
Target.AttachToTargetResponse attachToTargetResponse = protocol.getTarget()
    .attachToTarget(Target.AttachToTargetRequest.builder()
        .targetId(targetResponse.getTargetId())
        .flatten(true)
        .build())
    .get();
Protocol protocolSession = protocol.withSessionId(attachToTargetResponse.getSessionId().getValue());
Page.NavigateResponse pageResponse = protocolSession.getPage()
    .navigate(Page.NavigateRequest.builder()
        .url("https://www.google.com")
        .build())
    .get();
Thread.sleep(1_000); // wait for page rendering
Page.CaptureScreenshotResponse screenshotResponse = protocolSession.getPage()
    .captureScreenshot(Page.CaptureScreenshotRequest.builder()
        .format(Page.CaptureScreenshotRequest.Format.PNG)
        .build())
    .get();
byte[] bytes = Base64.getDecoder().decode(screenshotResponse.getData());
Files.write(Path.of("screenshot.png"), bytes);
protocol.close();

💡 Notes

  • The project is still under development and may contain errors or missing functionality.
  • The main goal of the project is to provide an educational and research-oriented tool for working with the Chrome DevTools Protocol.

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks