Giter Site home page Giter Site logo

cdp4j's Introduction

cdp4j - Chrome DevTools Protocol for Java

License Travis AppVeyor Codacy Badge

cdp4j is a web-automation library for Java. It can be used for automating the use of web pages and for testing web pages. It use Google Chrome DevTools Protocol to automate Chrome/Chromium based browsers.

Features

  • Supports full capabilities of the Chrome DevTools Protocol (tip-of-tree)
  • Evaluate JavaScript
  • Invoke JavaScript function
  • Supports native CSS selector engine
  • Supports Sizzle selector engine
  • Supports Xpath queries
  • Incognito Browsing (private tab)
  • Full page screen capture
  • Trigger Mouse events (click etc...)
  • Send keys (text, tab, enter etc...)
  • Redirect log entries (javascript, network, storage etc...) from browser to slf4j
  • Intercept Network (request & response)
  • Upload file programmatically without third party solutions (does not requires AWT Robot etc...)
  • get & set Element properties
  • Supports Headless Chrome/Chromium
  • Navigate back, forward, stop, reload
  • clear cache, clear cookies, list cookies
  • set & get values of form elements
  • Supports event handling

Supported Java Versions

Oracle & OpenJDK Java 8.

Both the JRE and the JDK are suitable for use with this library.

Licensing

cdp4j is released under the terms of the MIT License (MIT). You are free to use cdp4j or any of its constituent parts in any other project (even commercial projects) so long as its copyright headers are left intact.

Stability

This library is suitable for use in production systems.

Download

https://webfolder.io/download/cdp4j-1.0.1.jar - 723 KB

https://webfolder.io/download/cdp4j-1.0.1-sources.jar - 496 KB

Integration with Maven

  1. Download jar and sources file.

  2. install the jar into your local Maven repository as follows:

mvn install:install-file -Dfile=cdp4j-1.0.1.jar -DgroupId=io.webfolder -DartifactId=cdp4j -Dversion=1.0.1 -Dpackaging=jar -DgeneratePom=true
  1. install source jar into your local Maven repository as follows:
mvn install:install-file -Dfile=cdp4j-1.0.1-sources.jar -DgroupId=io.webfolder -DartifactId=cdp4j -Dversion=1.0.1 -Dpackaging=jar -DgeneratePom=true -Dclassifier=sources
  1. Add the following dependency to pom.xml file:
<dependency>
    <groupId>io.webfolder</groupId>
    <artifactId>cdp4j</artifactId>
    <version>1.0.1</version>
</dependency>

Supported Platforms

cdp4j has been tested under Windows 10 and Ubuntu, but should work on any platform where a Java & Chrome/Chromium available.

Headless Mode

cdp4j can be run in "headless" mode using with Headless Chrome.

Install Chrome on Debian/Ubuntu

# https://askubuntu.com/questions/79280/how-to-install-chrome-browser-properly-via-command-line
sudo apt-get install libxss1 libappindicator1 libindicator7
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb # Might show "errors", fixed by next line
sudo apt-get install -f

Install Chrome on RHEL/CentOS/Fedora

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
sudo yum install google-chrome-stable_current_*.rpm

Test headless Chrome

google-chrome --headless --remote-debugging-port=9222 --disable-gpu

JavaDoc

cdp4j api

Logging

Simple logger for Java (SLF4J) is supported.

Design Principles

  • Avoid external dependencies as much as possible.
  • Support only Chrome/Chromium based browsers.
  • Supports full capabilities of the Chrome DevTools Protocol.
  • Keep the API simple.

CSS Selector Engine

cdp4j use W3C selector engine which is default selector engine of Chrome/Chromium. Alternatively Sizzle selector engine might be used. Sizzle is the css selector engine of JQuery and it supports extra selectors like :has(div), :text, contains(text) etc. Check the Sizzle.java for using sizzle with cdp4j.

Usage Examples

Print text content with cdp4j

import io.webfolder.cdp.Launcher;
import io.webfolder.cdp.session.Session;
import io.webfolder.cdp.session.SessionFactory;

public class HelloWorld {

    public static void main(String[] args) {
        SessionFactory factory = new Launcher().launch();

        try (Session session = factory.create()) {
            session.navigate("https://webfolder.io");
            session.waitDocumentReady();
            String content = (String) session.getProperty("//body", "outerText");
            System.out.println(content);
        }

        factory.close();
    }
}

Full page screen capture with cdp4j

import static java.awt.Desktop.getDesktop;
import static java.awt.Desktop.isDesktopSupported;
import static java.nio.file.Files.createTempFile;
import static java.nio.file.Files.write;

import java.io.IOException;
import java.nio.file.Path;

import io.webfolder.cdp.Launcher;
import io.webfolder.cdp.session.Session;
import io.webfolder.cdp.session.SessionFactory;

public class Screenshot {

    public static void main(String[] args) throws IOException, InterruptedException {
        SessionFactory factory = new Launcher().launch();

        Path file = createTempFile("screenshot", ".png");

        try (Session session = factory.create()) {
            session.navigate("https://news.ycombinator.com");
            session.waitDocumentReady();
            // activate the tab/session before capturing the screenshot
            session.activate();
            byte[] data = session.captureScreenshot();
            write(file, data);
        }

        if (isDesktopSupported()) {
            getDesktop().open(file.toFile());
        }

        factory.close();
    }
}

Samples

Attributes.java

Bing.java

BingTranslator.java

CheckBox.java

ExecuteJavascript.java

GoogleTranslate.java

HelloWorld.java

IncognitoBrowsing.java

Logging.java

MultiSelect.java

NetworkResponse.java

Screenshot.java

Select.java

SendKeys.java

SharedSession.java

Sizzle.java

UserAgent.java

WaitUntil.java

XPathSelector.java

Building cdp4j

mvn install

How it is tested

cdp4j is regularly built and tested on Travis CI and AppVeyor.

Getting Help

WebFolder

cdp4j is an MIT licensed open source project and completely free to use. However, the amount of effort needed to maintain and develop new features for the project is not sustainable without proper financial backing. You can support cdp4j development by buying support package. Please contact us for support packages & pricing.

cdp4j's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.