Skip to content

Commit

Permalink
Automatically create parent dirs for screenshots (#460)
Browse files Browse the repository at this point in the history
Closes #455
  • Loading branch information
lread authored Jun 20, 2022
1 parent 926bf02 commit 257ea1e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ This will allow for easier testing of unreleased versions of Etaoin via git deps
It also unconvered that our minimum Clojure version was 1.10, instead of the advertised v1.9.
Fixed.
* https://github.com/clj-commons/etaoin/issues/444[#444]: Visibility checks fixed for firefox and chrome (thanks https://github.com/daveyarwood[@daveyarwood]!)
* https://github.com/clj-commons/etaoin/issues/455[#455]: Automatically create specified parent dirs for screenshots
* https://github.com/clj-commons/etaoin/issues/446[#446]: Bump Etaoin dependencies to current releases
* Docs
** Reviewed and updated API docstrings
Expand Down
12 changes: 3 additions & 9 deletions doc/01-user-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1456,15 +1456,15 @@ Specify a string:

[source,clojure]
----
(e/screenshot driver "target/etaoin-play/page.png")
(e/screenshot driver "target/etaoin-play/screens1/page.png")
----

or a `File` object:

[source,clojure]
----
(require '[clojure.java.io :as io])
(e/screenshot driver (io/file "target/etaoin-play/test.png"))
(e/screenshot driver (io/file "target/etaoin-play/screens2/test.png"))
----

=== Screenshots for Specific Elements
Expand All @@ -1474,7 +1474,7 @@ It doesn't work with other browsers at this time.

[source,clojure]
----
(e/screenshot-element driver {:tag :form :class :formy} "target/etaoin-play/form-element.png")
(e/screenshot-element driver {:tag :form :class :formy} "target/etaoin-play/screens3/form-element.png")
----

=== Screenshots after each form
Expand All @@ -1484,10 +1484,7 @@ The file naming convention is `<webdriver-name>-<milliseconds-since-1970>.png`

[source,clojure]
----
(require '[clojure.java.io :as io])
(e/refresh driver)
(.mkdirs (io/file "target/etaoin-play/saved-screenshots"))
(e/with-screenshots driver "target/etaoin-play/saved-screenshots"
(e/fill driver :uname "et")
(e/fill driver :uname "ao")
Expand All @@ -1498,10 +1495,7 @@ this is equivalent to something along the lines of:

[source,clojure]
----
(require '[clojure.java.io :as io])
(e/refresh driver)
(.mkdirs (io/file "target/etaoin-play/saved-screenshots"))
(e/fill driver :uname "et")
(e/screenshot driver "target/etaoin-play/saved-screenshots/chrome-1.png")
(e/fill driver :uname "ao")
Expand Down
22 changes: 14 additions & 8 deletions src/etaoin/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3117,6 +3117,10 @@
;; screenshots
;;

(defn- create-dirs-for-file [f]
(when (fs/parent f)
(-> f fs/parent fs/create-dirs)))

(defn- b64-decode [s]
(-> (Base64/getDecoder)
(.decode s)))
Expand All @@ -3142,7 +3146,7 @@
"Have `driver` save a PNG format screenshot of the current page to `file`.
Throws if screenshot is empty.
`file` can be either a string or `java.io.File`."
`file` can be either a string or `java.io.File`, any missing parent directories are automatically created."
{:arglists '([driver file])}
dispatch-driver)

Expand All @@ -3152,16 +3156,17 @@
:method :get
:path [:session (:session driver) :screenshot]})
b64str (-> resp :value not-empty)]
(if b64str
(b64-to-file b64str file)
(util/error "Empty screenshot"))))
(when (not b64str)
(util/error "Empty screenshot"))
(create-dirs-for-file file)
(b64-to-file b64str file)))

(defmulti screenshot-element
"Have `driver` save a PNG format screenshot of the element found by query `q` to `file`.
See [[query]] for details on `q`.
`file` can be either a string or `java.io.File`."
`file` can be either a string or `java.io.File`, any missing parent directories are automatically created."
{:arglists '([driver q file])}
dispatch-driver)

Expand All @@ -3178,9 +3183,10 @@
:method :get
:path [:session (:session driver) :element el :screenshot]})
b64str (-> resp :value not-empty)]
(if b64str
(b64-to-file b64str file)
(util/error "Empty screenshot, query: %s" q))))
(when (not b64str)
(util/error "Empty screenshot, query: %s" q))
(create-dirs-for-file file)
(b64-to-file b64str file)))

(defn ^:no-doc make-screenshot-file-path
[driver-type dir]
Expand Down

0 comments on commit 257ea1e

Please sign in to comment.