Skip to content

Instantly share code, notes, and snippets.

@dmarjenburgh
Created February 11, 2015 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmarjenburgh/b5881906e95085056265 to your computer and use it in GitHub Desktop.
Save dmarjenburgh/b5881906e95085056265 to your computer and use it in GitHub Desktop.
Calling https://github.com/xebia/VisualReview from clj-webdriver
(ns visualreview-amsclj.visualreview.core
(:require [clj-webdriver.taxi :as taxi]
[clj-http.client :as client]
[visualreview-amsclj.visualreview.config :as config])
(:import [org.openqa.selenium Capabilities HasCapabilities]))
(defn- endpoint [& paths]
(format "http://%s:%d/%s"
config/visualreview-hostname
config/visualreview-port
(apply str (interpose \/ paths))))
(defn- extract-capabilities [drv]
(when (instance? HasCapabilities drv)
(let [capabilities ^Capabilities (.getCapabilities ^HasCapabilities drv)]
{:platform (str (.getPlatform capabilities))
:browser-name (.getBrowserName capabilities)
:version (.getVersion capabilities)
:javascript-enabled? (.isJavascriptEnabled capabilities)})))
(defn- capabilities [drv]
(or (-> drv :capabilities :actual)
(extract-capabilities (:webdriver drv))))
(defn create-new-run [project-name suite-name]
(let [response (client/post (endpoint "api" "runs")
{:form-params {:project-name project-name
:suite-name suite-name}
:as :json})
run-id (-> response :body :id)]
(swap! config/current-config assoc
:project-name project-name
:suite-name suite-name
:run-id run-id)
run-id))
(defn take-screenshot [screenshot-name]
(when-let [capabilities (capabilities taxi/*driver*)]
(let [{:keys [width height]} (taxi/window-size)
meta {:os (:platform capabilities)
:browser (:browser-name capabilities)
:version (:version capabilities)
:resolution (str width \x height)}
response (client/post (endpoint "api" "runs" (config/run-id) "screenshots")
{:multipart [{:name "file" :content (taxi/take-screenshot) :mime-type "image/png"}
{:name "meta[os]" :content (:os meta)}
{:name "meta[browser]" :content (:browser meta)}
{:name "meta[version]" :content (:version meta)}
{:name "meta[resolution]" :content (:resolution meta)}
{:name "screenshot-name" :content screenshot-name}]
:throw-exceptions false})]
(when-not (= 201 (:status response))
(println "Error:" (:body response)))
response)))
@dmarjenburgh
Copy link
Author

Note: clj-webdriver 0.6.1 has a dependency on an old version of clj-http (0.3.0) that doesn't support multipart parameters well. You need to include your own dependency of clj-http (I used 1.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment