Skip to content

Commit

Permalink
ci: fix for safari (#562)
Browse files Browse the repository at this point in the history
It seems that GitHub Actions no longer supports `file:` urls for when
using safaridriver. I spent some time trying to coax it to do so but
safaridriver is not well documented, so instead of trying to come up
with brittle incantations, now hitting a local http test server for api
tests instead.

Cookie tests were affected by this changed and adjusted/simplified:
- browser specific tests consolidated (and edge now included)
- turfed phantom test, we no longer support it

Fixes #561
  • Loading branch information
lread authored Mar 17, 2024
1 parent 17f2f23 commit 38a9466
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 314 deletions.
5 changes: 4 additions & 1 deletion bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
:deps {doric/doric {:mvn/version "0.9.0"}
lread/status-line {:git/url "https://github.com/lread/status-line.git"
:sha "cf44c15f30ea3867227fa61ceb823e5e942c707f"}
dev.nubank/docopt {:mvn/version "0.6.1-fix7"}}
dev.nubank/docopt {:mvn/version "0.6.1-fix7"}
org.babashka/http-server {:mvn/version "0.1.12"}}
:tasks
{;; setup
:requires ([babashka.classpath :as cp]
Expand Down Expand Up @@ -47,6 +48,8 @@
:org.babashka/cli {:coerce {:nses [:symbol]
:patterns [:string]
:vars [:symbol]}}}
test-server {:doc "Static server to support tests (automatically lanched by tests that need it)"
:task test-server/-main}
test:bb {:doc "Runs tests under Babashka [--help]"
:task test/test-bb}
test-doc {:doc "test code blocks in user guide"
Expand Down
4 changes: 2 additions & 2 deletions doc/01-user-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1075,12 +1075,12 @@ An exception will be thrown if the local file is not found.
(def file-input {:tag :input :type :file})
;; upload a file from your system to the first file input
(def my-file "env/test/resources/html/drag-n-drop/images/document.png")
(def my-file "env/test/resources/static/drag-n-drop/images/document.png")
(e/upload-file driver file-input my-file)
;; or pass a native Java File object:
(require '[clojure.java.io :as io])
(def my-file (io/file "env/test/resources/html/drag-n-drop/images/document.png"))
(def my-file (io/file "env/test/resources/static/drag-n-drop/images/document.png"))
(e/upload-file driver file-input my-file)
----

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
35 changes: 35 additions & 0 deletions script/test_server.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(ns test-server
(:require [babashka.cli :as cli]
[babashka.http-server :as server]
[lread.status-line :as status]))

(def cli-spec {:help {:desc "This usage help" :alias :h}
:port {:ref "<port>"
:desc "Expose server on this port"
:coerce :int
:default 8888
:alias :p}
:dir {:ref "<dir>"
:desc "Serve static assets from this dir"
:default "./env/test/resources/static"
:alias :d}})

(defn- usage-help []
(status/line :head "Usage help")
(status/line :detail (cli/format-opts {:spec cli-spec :order [:port :dir :help]})) )

(defn- usage-fail [msg]
(status/line :error msg)
(usage-help)
(System/exit 1))

(defn -main [& args]
(let [opts (cli/parse-opts args {:spec cli-spec
:restrict true
:error-fn (fn [{:keys [msg]}]
(usage-fail msg))})]
(if (:help opts)
(usage-help)
(do
(status/line :detail "Test server static dir: %s" (:dir opts))
(server/exec opts)))))
154 changes: 66 additions & 88 deletions test/etaoin/api_test.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
(ns etaoin.api-test
(:require
[babashka.fs :as fs]
[babashka.process :as p]
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.java.shell :as shell]
[clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]]
[etaoin.api :as e]
[etaoin.impl.util :as util]
[etaoin.impl.client :as client]
[etaoin.test-report :as test-report]
[slingshot.slingshot :refer [try+]]))
[slingshot.slingshot :refer [try+]])
(:import [java.net ServerSocket]))

(defn numeric? [val]
(or (instance? Double val)
Expand Down Expand Up @@ -49,9 +52,18 @@

(def ^:dynamic *driver*)

(defn- find-available-port []
(with-open [sock (ServerSocket. 0)]
(.getLocalPort sock)))

(def ^:dynamic *test-server-port* nil)

(defn test-server-url [path]
(format "http://localhost:%d/%s" *test-server-port* path))

;; tests failed in safari 13.1.1 https://bugs.webkit.org/show_bug.cgi?id=202589 use STP newest
(defn fixture-browsers [f]
(let [url (-> "html/test.html" io/resource str)]
(let [url (test-server-url "test.html")]
(doseq [type drivers
:let [opts (get default-opts type {})]]
(e/with-driver type opts driver
Expand All @@ -70,9 +82,31 @@
(println "Testing with browsers:" drivers)
(f))

(defn test-server [f]
(binding [*test-server-port* (find-available-port)]
(let [proc (p/process {:out :inherit :err :inherit}
"bb test-server --port" *test-server-port*)]
(let [deadline (+ (System/currentTimeMillis) 15000)
test-url (test-server-url "test.html") ]
(loop []
(let [resp (try (client/http-request {:method :get :url test-url})
(catch Throwable _ :not-ready))]
(when (= :not-ready resp)
(if (< (System/currentTimeMillis) deadline)
(do
(println "- waiting for test-server to be ready at" test-url)
(Thread/sleep 1000)
(recur))
(throw (ex-info "Timed out waiting for ready test server" {}))))))
(println "Test server ready"))
(f)
(p/destroy proc)
@proc)))

(use-fixtures
:once
report-browsers)
report-browsers
test-server)

(deftest test-visible
(doto *driver*
Expand Down Expand Up @@ -268,7 +302,7 @@
(deftest test-url
(doto *driver*
(-> e/get-url
(str/ends-with? "/resources/html/test.html")
(str/ends-with? "/test.html")
is)))

(deftest test-css-props
Expand Down Expand Up @@ -409,7 +443,7 @@

(deftest test-drag-n-drop
(is 1)
(let [url (-> "html/drag-n-drop/index.html" io/resource str)
(let [url (test-server-url "drag-n-drop/index.html")
doc {:class :document}
trash {:xpath "//div[contains(@class, 'trash')]"}]
(doto *driver*
Expand Down Expand Up @@ -510,85 +544,32 @@

(deftest test-cookies
(testing "getting all cookies"
(let [cookies (e/get-cookies *driver*)]
(e/when-safari *driver*
;; Safari Webdriver v16.4 added sameSite, we'll ignore it for now
(let [cookies (map #(dissoc % :sameSite) cookies)]
(is (= cookies
[{:domain ".^filecookies^"
:secure false
:httpOnly false
:value "test1"
:path "/"
:name "cookie1"}
{:domain ".^filecookies^"
:secure false
:httpOnly false
:value "test2"
:path "/"
:name "cookie2"}]))))
(e/when-chrome *driver*
(is (= cookies [])))
(e/when-firefox *driver*
;; Firefox Webdriver added sameSite, we'll ignore it for now
(let [cookies (map #(dissoc % :sameSite) cookies)]
(is (= cookies [{:name "cookie1",
:value "test1",
:path "/",
:domain "",
:secure false,
:httpOnly false}
{:name "cookie2",
:value "test2",
:path "/",
:domain "",
:secure false,
:httpOnly false}]))))
(e/when-phantom *driver*
(is (= cookies [{:domain "",
:httponly false,
:name "cookie2",
:path "/",
:secure false,
:value "test2"}
{:domain "",
:httponly false,
:name "cookie1",
:path "/",
:secure false,
:value "test1"}])))))
(let [cookies (e/get-cookies *driver*)
sorted-cookies (->> cookies
(map #(dissoc % :sameSite)) ;; varies, maybe we don't care about this one
(sort-by :name) ;; order varies we don't care
)]
(is (= sorted-cookies [{:domain "localhost"
:httpOnly false
:name "cookie1"
:path "/"
:secure false
:value "test1"}
{:domain "localhost"
:httpOnly false
:name "cookie2"
:path "/"
:secure false
:value "test2"}]))))
(testing "getting a cookie"
(let [cookie (e/get-cookie *driver* :cookie2)]
(e/when-safari *driver*
;; Safari Webdriver v16.4 added sameSite, we'll ignore it for now
(let [cookie (dissoc cookie :sameSite)]
(is (= cookie
{:domain ".^filecookies^"
:secure false
:httpOnly false
:value "test2"
:path "/"
:name "cookie2"}))))
(e/when-chrome *driver*
(is (nil? cookie)))
(e/when-firefox *driver*
;; Firefox Webdriver added sameSite, we'll ignore it for now
(let [cookie (dissoc cookie :sameSite)]
(is (= cookie
{:name "cookie2"
:value "test2"
:path "/"
:domain ""
:secure false
:httpOnly false}))))
(e/when-phantom *driver*
(is (= cookie
{:domain ""
:httponly false
:name "cookie2"
:path "/"
:secure false
:value "test2"})))))
(let [cookie (e/get-cookie *driver* :cookie2)
cookie (dissoc cookie :sameSite)]
(is (= cookie {:domain "localhost"
:httpOnly false
:name "cookie2"
:path "/"
:secure false
:value "test2"}))))
(testing "deleting a cookie"
(e/when-not-phantom
*driver*
Expand Down Expand Up @@ -653,10 +634,7 @@
:bar [true nil "Hello"]})))))

(deftest test-add-script
(let [js-url (-> "js/inject.js" io/resource
fs/file .toURI .toURL ;; little extra dance here for bb on Windows,
;; otherwise slash after file: is ommitted and therefore invalid
str)]
(let [js-url (test-server-url "js/inject.js")]
(testing "adding a script"
(e/add-script *driver* js-url)
(e/wait 1)
Expand Down Expand Up @@ -712,7 +690,7 @@
["div" "b" "p" "span"])))))

(deftest test-query-tree
(let [url (-> "html/test2.html" io/resource str)
(let [url (test-server-url "test2.html")
_ (e/go *driver* url)
all-div (e/query-tree *driver* {:tag :div})
all-li (e/query-tree *driver* {:tag :li})
Expand Down
Loading

0 comments on commit 38a9466

Please sign in to comment.