From bb68422d04cab0bf364b534babdc53f914a280a1 Mon Sep 17 00:00:00 2001 From: Henry Widd Date: Mon, 15 Jan 2024 11:05:50 +0000 Subject: [PATCH] try using chrome devtools --- .github/workflows/tests.yaml | 2 +- deps.edn | 3 +- src/com/widdindustries/tiado_cljs2.clj | 217 +++++++++++++++++-------- 3 files changed, 148 insertions(+), 74 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 473120c..0d9fef4 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -24,7 +24,7 @@ jobs: # run: make test-clj - name: Install Chrome - uses: browser-actions/setup-chrome@latest + uses: browser-actions/setup-chrome@97 - name: Run example app tests run: cd examples/a-cljs-app && make test-cljs-shadow && cd - diff --git a/deps.edn b/deps.edn index af7f4ad..7538eea 100644 --- a/deps.edn +++ b/deps.edn @@ -7,6 +7,7 @@ lambdaisland/kaocha {:mvn/version "1.63.998"} lambdaisland/funnel {:mvn/version "1.4.71"} binaryage/devtools {:mvn/version "1.0.6"} - babashka/process {:mvn/version "0.3.11"}} + babashka/process {:mvn/version "0.3.11"} + clj-chrome-devtools/clj-chrome-devtools {:mvn/version "20220405"}} :aliases {:dev {:extra-paths ["test"]}}} diff --git a/src/com/widdindustries/tiado_cljs2.clj b/src/com/widdindustries/tiado_cljs2.clj index 0bca0a4..44cad92 100644 --- a/src/com/widdindustries/tiado_cljs2.clj +++ b/src/com/widdindustries/tiado_cljs2.clj @@ -5,6 +5,15 @@ [shadow.cljs.devtools.server :as server] [shadow.cljs.devtools.server.npm-deps :as npm-deps] [shadow.cljs.build-report :as build-report] + [clj-chrome-devtools.automation :as cdp-automation] + [clj-chrome-devtools.automation.fixture :as cdp-fixture] + [clj-chrome-devtools.automation.launcher :as cdp-launcher] + [clj-chrome-devtools.commands.browser] + [clj-chrome-devtools.commands.console :as console] + [clj-chrome-devtools.core :as cdp-core] + [clj-chrome-devtools.events :as events] + [clj-chrome-devtools.impl.connection :as cdp-connection] + [clojure.core.async :as async] ;[clojure.java.shell :as sh] [babashka.process :as process] [lambdaisland.funnel :as funnel] @@ -31,7 +40,7 @@ nil (when-let [server (funnel/start-server (funnel/websocket-server {:state (atom {}) - :port funnel-port}))] + :port funnel-port}))] (funnel/init-logging 1 nil) (alter-var-root #'funnel-server (constantly server))))) @@ -43,7 +52,7 @@ {:root "classpath:public" :use-index-files true}} })) -(defn npm-i +(defn npm-i "the call to shadow `npm-deps/main` installs deps.cljs dependencies iff they are not already in package.json. If the version in package.json is different to deps.cljs, the package.json version remains @@ -82,8 +91,8 @@ (io/resource))) (defn browser-app-config [& {:keys [watch-dir asset-path] - :or {watch-dir "web-target/public" - asset-path "/cljs-out"}}] + :or {watch-dir "web-target/public" + asset-path "/cljs-out"}}] {:build-id :app-dev :target :browser :output-dir (str watch-dir asset-path) @@ -92,7 +101,7 @@ :preloads (cond-> [] (cljs-ns-available? 'devtools.preload) (conj 'devtools.preload))} - :asset-path asset-path}) + :asset-path asset-path}) (defn clean-dir [d] (process/check (process/process ["rm" "-rf" d]))) @@ -135,21 +144,21 @@ (api/repl build-name))) (defn browser-test-config [] - {:build-id :browser-test-build - :target :browser-test + {:build-id :browser-test-build + :target :browser-test :compiler-options {:data-readers true} - :runner-ns 'kaocha.cljs2.shadow-runner - :test-dir "web-target/public/browser-test" - :asset-path "/browser-test/js" - :build-options {} - :closure-defines {'lambdaisland.funnel-client/FUNNEL_URI funnel-uri} - :devtools {:preloads (cond-> ['lambdaisland.chui.remote ] - (cljs-ns-available? 'devtools.preload) - (conj 'devtools.preload))}}) + :runner-ns 'kaocha.cljs2.shadow-runner + :test-dir "web-target/public/browser-test" + :asset-path "/browser-test/js" + :build-options {} + :closure-defines {'lambdaisland.funnel-client/FUNNEL_URI funnel-uri} + :devtools {:preloads (cond-> ['lambdaisland.chui.remote] + (cljs-ns-available? 'devtools.preload) + (conj 'devtools.preload))}}) (def compile-fns - {:watch watch - :once api/compile* + {:watch watch + :once api/compile* :release api/release*}) (defn browser-test-build [compile-mode opts] @@ -167,43 +176,118 @@ (browser-test-config) opts) (println "for tests, open " test-url)) -(defn run-tests [] - (let [result-fut - (future - (kaocha.repl/run - :browser-tests - #:kaocha{:tests - [#:kaocha.testable{:id :browser-tests - :type :kaocha.type/cljs2}]})) - result (deref result-fut 10000 ::timeout)] - (if (= ::timeout result) - (throw (Exception. "timeout waiting for test result")) - result))) - -(def ^:dynamic *chrome-command* - (case (System/getProperty "os.name") - "Mac OS X" "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" - "chrome")) - -(defn chrome-headless-proc [url] +(defn with-chrome-session [f] + #_(let [make-ws-delegate cdp-connection/make-ws-client] + (with-redefs [cdp-connection/make-ws-client #(make-ws-delegate opts) cdp-launcher/launch-chrome launch-chrome])) + (let [fixture (cdp-fixture/create-chrome-fixture {:headless? true + :no-sandbox? true + ;:remote-debugging-port 0 + ;"--remote-debugging-socket-fd=0" + ;"--no-sandbox" "--enable-logging=stderr" "--v=1" + })] + (fixture + (fn [] + (println "chrome launched") + (let [{:keys [connection] :as automation} @cdp-automation/current-automation] + (def automation automation) + (cdp-core/set-current-connection! connection) + (f automation)))))) + +(defn log-console-messages [f] + (console/enable) + (let [stop (events/listen :console :message-added + (fn [v] + (when-let [m (some->> v :params :message)] + (println "console" m))))] + (try (f) + (finally + (println "Unregistering console logging") + (stop))))) + +(defn run-tests-headless [_suite-name] + ;(chrome-headless/kill-all-chromes) + (chrome-fixture + (fn [session] + (try + #_(clj-chrome-devtools.commands.browser/grant-permissions + ;this might be useful if tests needed to use the real clipboard + {:origin shadow-local + :permissions ["clipboardReadWrite", "clipboardSanitizedWrite"]}) + (log-console-messages + (fn [] + (println "connecting to " test-url) + (cdp-automation/to session test-url) + ;(Thread/sleep 50000) + (println "running tests") ;suite-name + (let [kaocha-options (if true ; (is-gitlab?) + {:kaocha/plugins [:kaocha.plugin/junit-xml] + :koocha.plugin.junit-xal/target-file (str (name _suite-name) ".xml")} + {})] + (try + (kaocha.repl/run + :browser-tests + #:kaocha{:tests + [#:kaocha.testable{:id :browser-tests + :type :kaocha.type/cljs2}]}) + (catch Exception e + (println (.getMessage e))))))) + (catch Throwable t + (println " problem with chrome" (.getMessage t)))))) + ) + +(def *chrome-command* (cdp-launcher/find-chrome-binary)) + +(defn chrome-headless-proc [] (process/process [*chrome-command* "--disable-gpu" - "--remote-debugging-socket-fd=0" - "--headless" - "--remote-debugging-port=0" - "--no-sandbox" "--enable-logging=stderr" "--v=1" - url] + "--remote-debugging-socket-fd=0" + "--headless" + "--remote-debugging-port=53354" + "--no-sandbox" "--enable-logging=stderr" "--v=1" + ] {:out *out* :err *out*})) -(defn compile-and-run-tests-headless* [] - (let [proc (chrome-headless-proc test-url)] - (Thread/sleep 2000) - (try - (run-tests) - (finally - ;(println "killing pid " (.pid chrome)) - (process/destroy proc) - )))) +(comment + (start-server) + (browser-test-build :once {}) + (run-tests-headless nil) + (defonce chrome-fixture (cdp-fixture/create-chrome-fixture {:url-to-open (str shadow-local)})) + (def a (clj-chrome-devtools.automation.launcher/launch-automation + {:headless? true + :no-sandbox? true + :idle-timeout 0 + ;:url-to-open (str shadow-local) + })) + (def c (clj-chrome-devtools.core/connect "localhost" 53354)) + (def x (chrome-headless-proc)) + (process/destroy x) + ) + +#_(def ^:dynamic *chrome-command* + (case (System/getProperty "os.name") + "Mac OS X" "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" + "chrome")) + +#_(defn chrome-headless-proc [url] + (process/process [*chrome-command* "--disable-gpu" + "--remote-debugging-socket-fd=0" + "--headless" + "--remote-debugging-port=0" + "--no-sandbox" "--enable-logging=stderr" "--v=1" + url] + {:out *out* + :err *out*})) + +#_(defn compile-and-run-tests-headless* [] + (let [proc (chrome-headless-proc test-url)] + (Thread/sleep 2000) + (try + + + (finally + ;(println "killing pid " (.pid chrome)) + (process/destroy proc) + )))) (defn kaocha-exit-if-fail [result] (if (or (some pos? ((juxt :kaocha.result/error :kaocha.result/fail :kaocha.result/pending) @@ -213,46 +297,35 @@ (defn tests-ci-shadow [{:keys [compile-mode]}] (start-server) + ;(browser-test-build :once {}) (browser-test-build compile-mode {}) (try - (kaocha-exit-if-fail (compile-and-run-tests-headless*)) - (catch Exception e + (kaocha-exit-if-fail (run-tests-headless nil)) + (catch Exception e (println e) (System/exit 1)))) (defn build-report [build file-name] (build-report/generate build {:report-file file-name - :inline true})) + :inline true})) (defn show-npm-deps [] (clojure.pprint/print-table (npm-deps/get-deps-from-classpath))) -(comment - +(comment + + (tests-ci-shadow {:compile-mode :once}) + server/stop! - + (defn dev-http [] (-> @shadow.cljs.devtools.server.runtime/instance-ref :dev-http deref :servers first)) - + (-> (dev-http)) (-> (dev-http) :instance :handler-state :managers second ;first (.getResource "index.html")) - ) - - - - - - - - - - - - - - + ) \ No newline at end of file