-
Notifications
You must be signed in to change notification settings - Fork 163
/
browser_snapshot_test.clj
94 lines (84 loc) · 3.57 KB
/
browser_snapshot_test.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(ns quil.snippets.browser-snapshot-test
(:require
[clj-http.client :as http]
[clojure.edn :as edn]
[clojure.test :as t]
[etaoin.api :as etaoin]
[quil.snippets.test-helper :as sth]))
;; geckodriver is producing 625x625 images for actuals vs 500x500 for reference
(defn- geckodriver-installed? []
(sth/installed? "geckodriver" "--version"))
;; download driver from https://googlechromelabs.github.io/chrome-for-testing/
(defn- chromedriver-installed? []
(sth/installed? "chromedriver" "--version"))
(defn- test-file-server-running? []
(try
(= (:status (http/get "http://localhost:3000/test.html"
{:throw-exceptions false}))
200)
(catch java.lang.Exception _e false)))
(defn preconditions [f]
(t/is (geckodriver-installed?)
"geckodriver is not installed. Install it and rerun the test.")
(t/is (chromedriver-installed?)
"chromedriver is not installed. Install it and rerun the test.")
(f))
(defn server-setup [f]
;; if server exists, re-use existing
(if (test-file-server-running?)
(f)
;; late bind to figwheel so test can load without the :fig alias
(do ((requiring-resolve 'figwheel.main.api/start) {:mode :serve} "snippets")
(try (f)
(finally
((requiring-resolve 'figwheel.main.api/stop) "snippets"))))))
(def driver (atom nil))
(defn etaoin-setup [f]
(t/is (test-file-server-running?)
(str "Seems like file server with test page is not running. "))
(let [browser (etaoin/chrome {:size [1280 1024]})]
(reset! driver browser)
(try (f)
(finally
(etaoin/quit browser)
(reset! driver nil)))))
(t/use-fixtures :once
(t/join-fixtures [sth/imagemagick-installed
preconditions
server-setup
etaoin-setup]))
(defn snippet-elements [driver]
(->> (etaoin/query-all driver {:tag :option})
(map-indexed
(fn [ind el]
{:name (etaoin/get-element-text-el driver el)
:index ind
:skip-image-diff?
(etaoin/get-element-attr-el driver el :data-skip-image-diff)
:accepted-diff-threshold
(edn/read-string (etaoin/get-element-attr-el driver el :data-accepted-diff-threshold))}))
;; test only snippets that don't have skip-image-diff attribute.
(remove :skip-image-diff?)
(doall)))
;; TODO: generate snippet test set without starting server instead of combining
;; all tests into one. Currently the tests are ordered by
;; test/cljs/quil/snippet/init-test-selection hence why `snippet-elements` above
;; crawls the site for each test. However, if tests are generated ahead of time,
;; kaocha focus-test and friends could run individual examples.
(t/deftest ^:cljs-snippets
all-cljs-snippets-produce-expected-output
(etaoin/go @driver "http://localhost:3000/test.html")
(let [elements (snippet-elements @driver)]
(t/is (seq elements) "unable to find tests on test.html harness")
(doseq [{:keys [name index accepted-diff-threshold]} elements]
(etaoin/go @driver (str "http://localhost:3000/test.html#" index))
(etaoin/refresh @driver)
(let [actual-file (sth/actual-image "cljs" name)]
(etaoin/screenshot-element @driver {:tag :canvas} actual-file)
(sth/verify-reference-or-update
name "cljs" actual-file accepted-diff-threshold)
;; disable for now, as lots of chatty logging, but useful to inspect
;; (t/is (empty? (etaoin/get-logs @driver)))
))))
;; view image diffs with
;; $ eog dev-resources/snippet-snapshots/cljs/normal/*difference.png