Skip to content

Commit

Permalink
ci: fix for safari
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. Switch our api tests to hit a local http server
instead.

A side benefit is that cookie tests for chrome are more realistic in
that they actually return cookies.
  • Loading branch information
lread committed Mar 16, 2024
1 parent 17f2f23 commit e6739b1
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 20 deletions.
3 changes: 3 additions & 0 deletions bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
: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)"
:extra-deps {org.babashka/http-server {:mvn/version "0.1.12"}}
: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 "Static dir: %s" (:dir opts))
(server/exec opts)))))
62 changes: 46 additions & 16 deletions test/etaoin/api_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(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]
Expand Down Expand Up @@ -49,9 +50,14 @@

(def ^:dynamic *driver*)

(def test-server-port 8888)

(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 +76,16 @@
(println "Testing with browsers:" drivers)
(f))

(defn test-server [f]
(let [proc (p/process "bb test-server")]
(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 +281,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 +422,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 @@ -528,30 +541,43 @@
:path "/"
:name "cookie2"}]))))
(e/when-chrome *driver*
(is (= cookies [])))
(is (= cookies [{:domain "localhost"
:httpOnly false
:name "cookie2"
:path "/"
:sameSite "Lax"
:secure false
:value "test2"}
{:domain "localhost"
:httpOnly false
:name "cookie1"
:path "/"
:sameSite "Lax"
:secure false
:value "test1"}])))
(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 "",
:domain "localhost",
:secure false,
:httpOnly false}
{:name "cookie2",
:value "test2",
:path "/",
:domain "",
:domain "localhost",
:secure false,
:httpOnly false}]))))
(e/when-phantom *driver*
(is (= cookies [{:domain "",
(is (= cookies [{:domain "localhost",
:httponly false,
:name "cookie2",
:path "/",
:secure false,
:value "test2"}
{:domain "",
{:domain "localhost",
:httponly false,
:name "cookie1",
:path "/",
Expand All @@ -570,15 +596,22 @@
:path "/"
:name "cookie2"}))))
(e/when-chrome *driver*
(is (nil? cookie)))
(is (= cookie
{:domain "localhost"
:httpOnly false
:name "cookie2"
:path "/"
:sameSite "Lax"
:secure false
:value "test2"})))
(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 ""
:domain "localhost"
:secure false
:httpOnly false}))))
(e/when-phantom *driver*
Expand Down Expand Up @@ -653,10 +686,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 +742,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
2 changes: 1 addition & 1 deletion test/etaoin/api_with_driver_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
(defn testing-driver? [type]
(some #{type} api-test/drivers))

(def test-page (-> "html/test.html" io/resource str))
(def test-page (-> "static/test.html" io/resource str))
(def my-agent "my agent")

(deftest with-driver-tests
Expand Down
2 changes: 1 addition & 1 deletion test/etaoin/ide_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
(get-default-drivers)))

(defn fixture-browser [f]
(let [base-url (-> "html" io/resource str)
(let [base-url (-> "static" io/resource str)
test-file-path (-> "ide/test.side" io/resource str)]
(doseq [type drivers]
(e/with-driver type {:args ["--no-sandbox"]} driver
Expand Down

0 comments on commit e6739b1

Please sign in to comment.