From ce9f4c1064fff46e2f76278daad4df44143b133e Mon Sep 17 00:00:00 2001 From: lread Date: Sun, 22 May 2022 17:37:23 -0400 Subject: [PATCH] road to bb: ImageIO no longer used in tests 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 #380 --- .github/workflows/test.yml | 8 ++++++++ script/tools_versions.clj | 11 +++++++++-- test/etaoin/api_test.clj | 36 +++++++++++++++++++++++------------- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8f7e5a55..a1429777 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -69,6 +69,14 @@ jobs: key: cljdeps-${{ hashFiles('deps.edn', 'bb.edn') }} restore-keys: cljdeps- + - name: "Install Missing Windows Bits" + if: ${{ matrix.os == 'windows' }} + run: | + Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" + choco install --no-progress --yes imagemagick + refreshenv + Write-Output "$env:PATH" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append + - name: "Setup Java" uses: actions/setup-java@v3 with: diff --git a/script/tools_versions.clj b/script/tools_versions.clj index f9540dcb..a2836e66 100644 --- a/script/tools_versions.clj +++ b/script/tools_versions.clj @@ -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"} @@ -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"} @@ -114,7 +120,8 @@ [{:keys [app shell-opts args version-post-fn]}] (if-let [found-bin (some-> (fs/which app {:win-exts ["com" "exe" "bat" "cmd" "ps1"]}) str)] - (let [version-result (->> (shell/command shell-opts found-bin args) + ;; call with app rather than found-bin to avoid Windows headaches + (let [version-result (->> (shell/command shell-opts app args) (version-cmd-result shell-opts)) version-result (assoc version-result :path found-bin)] (if (:error version-result) diff --git a/test/etaoin/api_test.clj b/test/etaoin/api_test.clj index a1ce4431..d3ed9d8c 100644 --- a/test/etaoin/api_test.clj +++ b/test/etaoin/api_test.clj @@ -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) @@ -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] @@ -566,13 +569,23 @@ (is (str/starts-with? src "")) (is (str/starts-with? src ""))))) +(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 @@ -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"