Martin Kavalar / Jun 27 2018

📸 Screenshotting with Sparkledriver

Trying out Sparkledriver to take screenshots with the built-in webkit browser that's bundled with the JDK.

1.
Sparkledriver

We start by requring sparkledriver.

(require '[sparkledriver.browser :refer [with-browser make-browser fetch!]])
(require '[sparkledriver.element :refer [screenshot]])
(require '[clojure.java.io :as io])

Let's create a browser and a helper function.

(def browser
  (make-browser :ajax-wait 10000, :ajax-load-timeout 60000))

(defn preview [url]
  (-> (fetch! browser (str url "?preview=1"))
    screenshot
    io/file
    (io/copy (io/file "/results/screenshot.png"))))

And, let's shoot! 📸

(preview "https://staging.nextjournal.com/gkoehler/digit-recognition-with-keras")
(preview "https://staging.nextjournal.com/a/17592186485566")
(sparkledriver.browser/logs browser)

2.
Environment Setup

JavaFX doesn't ship with the OpenJDK by default, so we need to install it.

apt-get update
apt-get install -y openjfx

Next, we create an environment with sparkledriver.

echo '{:deps {sparkledriver {:mvn/version "0.2.2"}}}' > deps.edn
clj -Sforce -e ":ok"