Cromium

An simple CDP (Chrome Devtools Protocol) client, made for testing, scraping, screenshot and html to pdf.

Installation

  1. Install chromium, on ubuntu:

    sudo apt-get install chromium-browser

    or on mac:

    brew install chromium
  2. Add the dependency to your shard.yml:

    dependencies:
      cromium:
        github: confact/cromium
  3. Run shards install

Usage

require "cromium"

Settings

Default is local chromium executable. Cromium is using Crystal's Process, which looks for chrome in absolute path, path relative to pwd, or in path.

If you want to use a remote address and turn off the process:

Cromium.remote = true
Cromium.endpoint = "https://chrome.browserless.io/sdsdsdfsd"

If you want to run your chromium process on a specific port, instead of default 9222, just set the endpoint, but keep remote as false

Cromium.endpoint = "http://localhost:9000"

API

Browser

Page

Example

require "cromium"

browser = Cromium.start

page = browser.new_page

page.goto "https://www.google.com"

page.wait_to_page_load do
  page.screenshot_to_file "google.png", full_page: true, hide_scrollbars: true
  page.pdf_to_file "google.pdf", print_background: true
end
browser.close # do not need to call it though, it will close when code exits

Contributing

  1. Fork it (https://github.com/confact/cromium/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors