Skip to content

Commit

Permalink
Resize height by default, but not width, and hide scrollbars
Browse files Browse the repository at this point in the history
  • Loading branch information
oliyh committed Jul 10, 2020
1 parent d0f6b3f commit d1405ab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ Each target must provide a `:url` and `:reference-file` and can override any set
;; see element-exists? as an example
:normalisations [:trim :crop] ;; normalisations to apply to images before comparison, in order of application
:assert? true ;; runs a clojure.test assert on the expected/actual when true, makes no assertions when false
:resize-to-contents? false} ;; resize browser window to fit contents before screenshot - true is legacy behaviour
:resize-to-contents {:height? true ;; resize browser window dimensions to fit contents before screenshot - true for both is legacy behaviour
:width? false}

:normalisation-fns ;; normalisation functions, add your own if desired
{:trim trim-images
Expand All @@ -160,7 +161,8 @@ Each target must provide a `:url` and `:reference-file` and can override any set
;; see https://github.com/tatut/clj-chrome-devtools/blob/master/src/clj_chrome_devtools/automation/launcher.clj#L52
{:chrome-binary "/opt/bin/google-chrome-stable"
:headless? true
:extra-chrome-args ["--window-size=1600,900"]}
:extra-chrome-args ["--window-size=1600,900"
"--hide-scrollbars"]}
}
```

Expand Down
32 changes: 20 additions & 12 deletions src/clj/kamera/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[clj-chrome-devtools.commands.page :as page]
[clj-chrome-devtools.commands.dom :as dom]
[clj-chrome-devtools.commands.emulation :as emulation]
[clj-chrome-devtools.commands.browser :as browser]
[clojure.string :as string]
[clojure.tools.logging :as log])
(:import [java.io File]
Expand Down Expand Up @@ -168,20 +169,26 @@
(when-let [body (cdp-automation/sel1 session "body")]
(:model (dom/get-box-model connection body))))

(defn- resize-window-to-contents! [{:keys [connection] :as session}]
(let [{:keys [width height]} (body-dimensions session)]
(emulation/set-visible-size connection {:width width :height height})
(emulation/set-device-metrics-override connection {:width width
:height height
:device-scale-factor 1.0
:mobile false})
(defn- browser-dimensions [{:keys [connection]}]
(select-keys (:bounds (browser/get-window-for-target connection {}))
[:width :height]))

(defn- resize-window-to-contents! [{:keys [connection] :as session} {:keys [width? height?]}]
(let [{:keys [width height]} (body-dimensions session)
dimensions (cond-> (browser-dimensions session)
width? (assoc :width width)
height? (assoc :height height))]
(emulation/set-visible-size connection dimensions)
(emulation/set-device-metrics-override connection (merge dimensions
{:device-scale-factor 1.0
:mobile false}))
(emulation/set-page-scale-factor connection {:page-scale-factor 1.0})))

(defn- take-screenshot [session {:keys [reference-file screenshot-directory resize-to-contents?] :as target} opts]
(when resize-to-contents?
(resize-window-to-contents! session))
(defn- take-screenshot [session {:keys [reference-file screenshot-directory resize-to-contents]} opts]
(when (and resize-to-contents (some resize-to-contents [:height? :width?]))
(resize-window-to-contents! session resize-to-contents))

(let [{:keys [data]} (page/capture-screenshot (:connection session) {:from-surface true}) ;; hides scrollbar
(let [{:keys [data]} (page/capture-screenshot (:connection session) {:from-surface true})
file (append-suffix screenshot-directory (io/file reference-file) ".actual")]
(if data
(do (io/make-parents file)
Expand Down Expand Up @@ -255,7 +262,8 @@
:ready? nil ;; (fn [session] ... ) a predicate that should return true when ready to take the screenshot
;; see element-exists?
:assert? true ;; runs a clojure.test assert on the expected/actual when true, makes no assertions when false
:resize-to-contents? false}
:resize-to-contents {:height? true
:width? false}}
:normalisation-fns {:trim trim-images
:crop crop-images}
:imagemagick-options {:path nil ;; directory where binaries reside on linux, or executable on windows
Expand Down
2 changes: 1 addition & 1 deletion test/clj/kamera/devcards_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
targets)})
(update :default-target merge {:reference-directory "example/test-resources/kamera"
:screenshot-directory target-dir
:metric-threshold 0.02}))]
:metric-threshold 0.05}))]

(let [passes (atom [])
failures (atom [])
Expand Down

0 comments on commit d1405ab

Please sign in to comment.