Skip to content

Commit 38a9466

Browse files
authored
ci: fix for safari (#562)
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
1 parent 17f2f23 commit 38a9466

16 files changed

+333
-314
lines changed

bb.edn

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
:deps {doric/doric {:mvn/version "0.9.0"}
44
lread/status-line {:git/url "https://github.com/lread/status-line.git"
55
:sha "cf44c15f30ea3867227fa61ceb823e5e942c707f"}
6-
dev.nubank/docopt {:mvn/version "0.6.1-fix7"}}
6+
dev.nubank/docopt {:mvn/version "0.6.1-fix7"}
7+
org.babashka/http-server {:mvn/version "0.1.12"}}
78
:tasks
89
{;; setup
910
:requires ([babashka.classpath :as cp]
@@ -47,6 +48,8 @@
4748
:org.babashka/cli {:coerce {:nses [:symbol]
4849
:patterns [:string]
4950
:vars [:symbol]}}}
51+
test-server {:doc "Static server to support tests (automatically lanched by tests that need it)"
52+
:task test-server/-main}
5053
test:bb {:doc "Runs tests under Babashka [--help]"
5154
:task test/test-bb}
5255
test-doc {:doc "test code blocks in user guide"

doc/01-user-guide.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1075,12 +1075,12 @@ An exception will be thrown if the local file is not found.
10751075
(def file-input {:tag :input :type :file})
10761076
10771077
;; upload a file from your system to the first file input
1078-
(def my-file "env/test/resources/html/drag-n-drop/images/document.png")
1078+
(def my-file "env/test/resources/static/drag-n-drop/images/document.png")
10791079
(e/upload-file driver file-input my-file)
10801080
10811081
;; or pass a native Java File object:
10821082
(require '[clojure.java.io :as io])
1083-
(def my-file (io/file "env/test/resources/html/drag-n-drop/images/document.png"))
1083+
(def my-file (io/file "env/test/resources/static/drag-n-drop/images/document.png"))
10841084
(e/upload-file driver file-input my-file)
10851085
----
10861086

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

script/test_server.clj

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(ns test-server
2+
(:require [babashka.cli :as cli]
3+
[babashka.http-server :as server]
4+
[lread.status-line :as status]))
5+
6+
(def cli-spec {:help {:desc "This usage help" :alias :h}
7+
:port {:ref "<port>"
8+
:desc "Expose server on this port"
9+
:coerce :int
10+
:default 8888
11+
:alias :p}
12+
:dir {:ref "<dir>"
13+
:desc "Serve static assets from this dir"
14+
:default "./env/test/resources/static"
15+
:alias :d}})
16+
17+
(defn- usage-help []
18+
(status/line :head "Usage help")
19+
(status/line :detail (cli/format-opts {:spec cli-spec :order [:port :dir :help]})) )
20+
21+
(defn- usage-fail [msg]
22+
(status/line :error msg)
23+
(usage-help)
24+
(System/exit 1))
25+
26+
(defn -main [& args]
27+
(let [opts (cli/parse-opts args {:spec cli-spec
28+
:restrict true
29+
:error-fn (fn [{:keys [msg]}]
30+
(usage-fail msg))})]
31+
(if (:help opts)
32+
(usage-help)
33+
(do
34+
(status/line :detail "Test server static dir: %s" (:dir opts))
35+
(server/exec opts)))))

test/etaoin/api_test.clj

+66-88
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
(ns etaoin.api-test
22
(:require
33
[babashka.fs :as fs]
4+
[babashka.process :as p]
45
[clojure.edn :as edn]
56
[clojure.java.io :as io]
67
[clojure.java.shell :as shell]
78
[clojure.string :as str]
89
[clojure.test :refer [deftest is testing use-fixtures]]
910
[etaoin.api :as e]
1011
[etaoin.impl.util :as util]
12+
[etaoin.impl.client :as client]
1113
[etaoin.test-report :as test-report]
12-
[slingshot.slingshot :refer [try+]]))
14+
[slingshot.slingshot :refer [try+]])
15+
(:import [java.net ServerSocket]))
1316

1417
(defn numeric? [val]
1518
(or (instance? Double val)
@@ -49,9 +52,18 @@
4952

5053
(def ^:dynamic *driver*)
5154

55+
(defn- find-available-port []
56+
(with-open [sock (ServerSocket. 0)]
57+
(.getLocalPort sock)))
58+
59+
(def ^:dynamic *test-server-port* nil)
60+
61+
(defn test-server-url [path]
62+
(format "http://localhost:%d/%s" *test-server-port* path))
63+
5264
;; tests failed in safari 13.1.1 https://bugs.webkit.org/show_bug.cgi?id=202589 use STP newest
5365
(defn fixture-browsers [f]
54-
(let [url (-> "html/test.html" io/resource str)]
66+
(let [url (test-server-url "test.html")]
5567
(doseq [type drivers
5668
:let [opts (get default-opts type {})]]
5769
(e/with-driver type opts driver
@@ -70,9 +82,31 @@
7082
(println "Testing with browsers:" drivers)
7183
(f))
7284

85+
(defn test-server [f]
86+
(binding [*test-server-port* (find-available-port)]
87+
(let [proc (p/process {:out :inherit :err :inherit}
88+
"bb test-server --port" *test-server-port*)]
89+
(let [deadline (+ (System/currentTimeMillis) 15000)
90+
test-url (test-server-url "test.html") ]
91+
(loop []
92+
(let [resp (try (client/http-request {:method :get :url test-url})
93+
(catch Throwable _ :not-ready))]
94+
(when (= :not-ready resp)
95+
(if (< (System/currentTimeMillis) deadline)
96+
(do
97+
(println "- waiting for test-server to be ready at" test-url)
98+
(Thread/sleep 1000)
99+
(recur))
100+
(throw (ex-info "Timed out waiting for ready test server" {}))))))
101+
(println "Test server ready"))
102+
(f)
103+
(p/destroy proc)
104+
@proc)))
105+
73106
(use-fixtures
74107
:once
75-
report-browsers)
108+
report-browsers
109+
test-server)
76110

77111
(deftest test-visible
78112
(doto *driver*
@@ -268,7 +302,7 @@
268302
(deftest test-url
269303
(doto *driver*
270304
(-> e/get-url
271-
(str/ends-with? "/resources/html/test.html")
305+
(str/ends-with? "/test.html")
272306
is)))
273307

274308
(deftest test-css-props
@@ -409,7 +443,7 @@
409443

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

511545
(deftest test-cookies
512546
(testing "getting all cookies"
513-
(let [cookies (e/get-cookies *driver*)]
514-
(e/when-safari *driver*
515-
;; Safari Webdriver v16.4 added sameSite, we'll ignore it for now
516-
(let [cookies (map #(dissoc % :sameSite) cookies)]
517-
(is (= cookies
518-
[{:domain ".^filecookies^"
519-
:secure false
520-
:httpOnly false
521-
:value "test1"
522-
:path "/"
523-
:name "cookie1"}
524-
{:domain ".^filecookies^"
525-
:secure false
526-
:httpOnly false
527-
:value "test2"
528-
:path "/"
529-
:name "cookie2"}]))))
530-
(e/when-chrome *driver*
531-
(is (= cookies [])))
532-
(e/when-firefox *driver*
533-
;; Firefox Webdriver added sameSite, we'll ignore it for now
534-
(let [cookies (map #(dissoc % :sameSite) cookies)]
535-
(is (= cookies [{:name "cookie1",
536-
:value "test1",
537-
:path "/",
538-
:domain "",
539-
:secure false,
540-
:httpOnly false}
541-
{:name "cookie2",
542-
:value "test2",
543-
:path "/",
544-
:domain "",
545-
:secure false,
546-
:httpOnly false}]))))
547-
(e/when-phantom *driver*
548-
(is (= cookies [{:domain "",
549-
:httponly false,
550-
:name "cookie2",
551-
:path "/",
552-
:secure false,
553-
:value "test2"}
554-
{:domain "",
555-
:httponly false,
556-
:name "cookie1",
557-
:path "/",
558-
:secure false,
559-
:value "test1"}])))))
547+
(let [cookies (e/get-cookies *driver*)
548+
sorted-cookies (->> cookies
549+
(map #(dissoc % :sameSite)) ;; varies, maybe we don't care about this one
550+
(sort-by :name) ;; order varies we don't care
551+
)]
552+
(is (= sorted-cookies [{:domain "localhost"
553+
:httpOnly false
554+
:name "cookie1"
555+
:path "/"
556+
:secure false
557+
:value "test1"}
558+
{:domain "localhost"
559+
:httpOnly false
560+
:name "cookie2"
561+
:path "/"
562+
:secure false
563+
:value "test2"}]))))
560564
(testing "getting a cookie"
561-
(let [cookie (e/get-cookie *driver* :cookie2)]
562-
(e/when-safari *driver*
563-
;; Safari Webdriver v16.4 added sameSite, we'll ignore it for now
564-
(let [cookie (dissoc cookie :sameSite)]
565-
(is (= cookie
566-
{:domain ".^filecookies^"
567-
:secure false
568-
:httpOnly false
569-
:value "test2"
570-
:path "/"
571-
:name "cookie2"}))))
572-
(e/when-chrome *driver*
573-
(is (nil? cookie)))
574-
(e/when-firefox *driver*
575-
;; Firefox Webdriver added sameSite, we'll ignore it for now
576-
(let [cookie (dissoc cookie :sameSite)]
577-
(is (= cookie
578-
{:name "cookie2"
579-
:value "test2"
580-
:path "/"
581-
:domain ""
582-
:secure false
583-
:httpOnly false}))))
584-
(e/when-phantom *driver*
585-
(is (= cookie
586-
{:domain ""
587-
:httponly false
588-
:name "cookie2"
589-
:path "/"
590-
:secure false
591-
:value "test2"})))))
565+
(let [cookie (e/get-cookie *driver* :cookie2)
566+
cookie (dissoc cookie :sameSite)]
567+
(is (= cookie {:domain "localhost"
568+
:httpOnly false
569+
:name "cookie2"
570+
:path "/"
571+
:secure false
572+
:value "test2"}))))
592573
(testing "deleting a cookie"
593574
(e/when-not-phantom
594575
*driver*
@@ -653,10 +634,7 @@
653634
:bar [true nil "Hello"]})))))
654635

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

714692
(deftest test-query-tree
715-
(let [url (-> "html/test2.html" io/resource str)
693+
(let [url (test-server-url "test2.html")
716694
_ (e/go *driver* url)
717695
all-div (e/query-tree *driver* {:tag :div})
718696
all-li (e/query-tree *driver* {:tag :li})

0 commit comments

Comments
 (0)