Skip to content

Commit

Permalink
road to bb: ImageIO no longer used in tests
Browse files Browse the repository at this point in the history
We were using java's ImageIO to validate that we could successfully read
screenshot png images. There's nothing wrong with this, but babashka
does not currently include ImageIO support.

So I've switch to calling out to Image Magick to validate our screenshot
png are valid.

A minor developer setup burden for bb compatibility.

Contributes to clj-commons#380
  • Loading branch information
lread committed May 22, 2022
1 parent f8cc870 commit cbf4fee
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
20 changes: 19 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Test

on:
push:
branches: ['master']
branches: ['lread-*']
pull_request:

jobs:
Expand Down Expand Up @@ -69,6 +69,24 @@ jobs:
key: cljdeps-${{ hashFiles('deps.edn', 'bb.edn') }}
restore-keys: cljdeps-

- name: "Install Missing Windows Bits"
if: ${{ matrix.os == 'windows' }}
run: |
choco install --no-progress --yes imagemagick
- name: "Update Windows Path"
if: ${{ matrix.os == 'windows' }}
shell: cmd
run: |
call refreshenv
echo path
echo %PATH%
echo github path
echo %GITHUB_PATH%
echo github path contents
cat %GITHUB_PATH%
echo %PATH% >> %GITHUB_PATH%
- name: "Setup Java"
uses: actions/setup-java@v3
with:
Expand Down
5 changes: 4 additions & 1 deletion script/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
(remove nil?)
(string/join " "))})

(defn- github-actions-matrix []
#_(defn- github-actions-matrix []
(let [oses ["macos" "ubuntu" "windows"]
ide-browsers ["chrome" "firefox"]
api-browsers ["chrome" "firefox" "edge" "safari"]]
Expand All @@ -37,6 +37,9 @@
(sort-by :desc)
(into []))))

(defn- github-actions-matrix []
[{:os "windows" :cmd "bb test unit" :desc "windows unit"}])

(defn- launch-xvfb []
(if (fs/which "Xvfb")
(process/process "Xvfb :99 -screen 0 1024x768x24" {:out (fs/file "/dev/null")
Expand Down
8 changes: 7 additions & 1 deletion script/tools_versions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
[helper.os :as os]
[helper.shell :as shell]))

(defn- first-line [s]
(-> s string/split-lines first))

(def tools
[;; earlier versions of java used -version and spit version info to stderr
{:oses :all :name "Java" :type :bin :app "java" :args "-version" :shell-opts {:out :string :err :string :continue true}}
{:oses :all :name "Babashka" :type :bin :app "bb"}

{:oses [:mac :win] :name "Image Magick" :type :bin :app "magick" :version-post-fn first-line}
{:oses [:unix] :name "Image Magick" :type :bin :app "identify" :version-post-fn first-line}

{:oses [:unix] :name "Chrome" :type :bin :app "google-chrome"} ;; only handling nix for now
{:oses [:mac] :name "Chrome" :type :mac-app :app "Google Chrome"}
{:oses [:win] :name "Chrome" :type :win-package :app "Google Chrome"}
Expand All @@ -20,7 +26,7 @@
{:oses [:unix] :name "Firefox" :type :bin :app "firefox"} ;; only handling nix for now
{:oses [:mac] :name "Firefox" :type :mac-app :app "Firefox"}
{:oses [:win] :name "Firefox" :type :win-package :app #"Mozilla Firefox .*"}
{:oses :all :name "Firefox Webdriver" :type :bin :app "geckodriver" :version-post-fn #(->> % string/split-lines first)}
{:oses :all :name "Firefox Webdriver" :type :bin :app "geckodriver" :version-post-fn first-line}

{:oses [:mac] :name "Edge" :type :mac-app :app "Microsoft Edge"}
{:oses [:win] :name "Edge" :type :win-package :app "Microsoft Edge"}
Expand Down
36 changes: 23 additions & 13 deletions test/etaoin/api_test.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(ns etaoin.api-test
(:require [clojure.edn :as edn]
(:require [babashka.fs :as fs]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.java.shell :as shell]
[clojure.string :as str]
[clojure.test :refer :all]
[etaoin.api :refer :all]
[etaoin.test-report :as test-report]
[etaoin.util :refer [with-tmp-dir with-tmp-file]]
[slingshot.slingshot :refer [try+]])
(:import javax.imageio.ImageIO))

[slingshot.slingshot :refer [try+]]))

(defn numeric? [val]
(or (instance? Double val)
Expand All @@ -22,8 +22,11 @@
(when-let [override (System/getenv "ETAOIN_TEST_DRIVERS")]
(edn/read-string override)))

(defn os-name []
(first (str/split (System/getProperty "os.name") #"\s+")))

(defn get-drivers-from-prop []
(case (first (str/split (System/getProperty "os.name") #"\s+"))
(case (os-name)
"Linux" [:firefox :chrome]
"Mac" [:chrome :edge :firefox :safari]
"Windows" [:firefox :chrome :edge]
Expand Down Expand Up @@ -566,13 +569,23 @@
(is (str/starts-with? src "<!DOCTYPE html>"))
(is (str/starts-with? src "<html><head>")))))

(defn- valid-image? [file]
(if-let [image-magick (some-> (fs/which (if (= "Linux" (os-name))
"identify" ;; sacre ubuntu!
"magick"))
str)]
(let [{:keys [exit out]}
(if (= "Linux" (os-name))
(shell/sh image-magick (str file))
(shell/sh image-magick "identify" (str file)))]
(println out)
(zero? exit))
(throw (ex-info "please install image magick, we use it for screenshot image verification" {}))))

(deftest test-screenshot
(with-tmp-file "screenshot" ".png" path
(screenshot *driver* path)
(-> path
io/file
ImageIO/read
is)))
(is (valid-image? path))))

(deftest test-with-screenshots
(with-tmp-dir "screenshots" dir
Expand All @@ -587,10 +600,7 @@
(firefox? *driver*))
(with-tmp-file "screenshot" ".png" path
(screenshot-element *driver* {:id :css-test} path)
(-> path
io/file
ImageIO/read
is))))
(is (valid-image? path)))))

(deftest test-js-execute
(testing "simple result"
Expand Down

0 comments on commit cbf4fee

Please sign in to comment.