Kaynağa Gözat

Basic example using browser with test ns and test runner cleanly split

Gabriel Horner 3 yıl önce
ebeveyn
işleme
56b58566be

+ 1 - 0
e2e-tests/.gitignore

@@ -0,0 +1 @@
+yarn.lock

+ 22 - 0
e2e-tests/README.md

@@ -0,0 +1,22 @@
+## Description
+
+This demonstrates running playwright tests with https://github.com/babashka/nbb.
+
+_Note_: This only runs example browser tests. There is more work to run a basic electron test
+
+## Setup
+Install dependencies: `yarn install`
+
+## Usage
+
+Run tests: `yarn nbb -m test-runner`
+
+Run headless tests: `CI=1 yarn nbb -m test-runner`
+
+`nbb` supports repl driven development. See `yarn nbb -h` for different ways to
+start a repl. See links below for examples of repl usage.
+
+
+## Links
+* https://github.com/nextjournal/clerk/pull/97/files
+* https://github.com/babashka/nbb/tree/main/examples/playwright

+ 6 - 0
e2e-tests/package.json

@@ -0,0 +1,6 @@
+{
+  "dependencies": {
+    "playwright": "^1.19.2",
+    "nbb": "^0.2.9"
+  }
+}

+ 56 - 0
e2e-tests/srs_test.cljs

@@ -0,0 +1,56 @@
+(ns srs-test
+  (:require [clojure.test :as t :refer [deftest is async use-fixtures]]
+            ["playwright$default" :refer [chromium]]
+            [clojure.string :as str]
+            [promesa.core :as p]))
+
+;; TODO: Setup helpers and an electron test using fixture.fs for examples
+;; See https://github.com/babashka/nbb#what-does-default-mean for syntax
+;; https://shadow-cljs.github.io/docs/UsersGuide.html#_using_npm_packages may also
+;; help but be warned we aren't using shadow-cljs here
+
+;; Move to test-helper when this works for electron
+(def browser (atom nil))
+(def headless (boolean (.-CI js/process.env)))
+(defn launch-browser []
+  (p/->> (.launch chromium #js {:headless headless})
+         (reset! browser)))
+(def close-browser true)
+
+;; Tests
+(use-fixtures :once
+  {:before
+   (fn []
+     (async done
+            (->
+             (launch-browser)
+             (.catch js/console.log)
+             (.finally done))))
+   :after
+   (fn []
+     (async done
+            (if close-browser
+              (p/do
+                (.close @browser)
+                (done))
+              (done))))})
+
+;; Example test copied from https://github.com/babashka/nbb/blob/main/examples/playwright/example.cljs
+(deftest example-test
+  (async
+   done
+   (-> (p/let [page (.newPage @browser)
+            _ (.goto page "https://clojure.org" #js{:waitUntil "networkidle"})
+            _ (-> (.screenshot page #js{:path "screenshot.png"})
+                  (.catch #(js/console.log %)))
+            content (.content page)
+            ;; uncomment to save content to variable for inspection
+            ;; _ (def c content)
+            ;; uncomment to pause execution to inspect state in browser
+            ;; _ (pause)
+            ]
+           (is (str/includes? content "clojure")))
+       (.catch (fn [err]
+                 (js/console.log err)
+                 (is false)))
+       (.finally done))))

+ 30 - 0
e2e-tests/test_runner.cljs

@@ -0,0 +1,30 @@
+(ns test-runner
+  "Originally copied from https://github.com/nextjournal/clerk/blob/6a7690caf436b3e5eb210ea06fb31bb9f7ba3387/ui_tests/playwright_tests.cljs"
+  {:clj-kondo/config '{:skip-comments false}}
+  (:require [clojure.test :as t]
+            [srs-test]))
+
+(defmethod t/report [:cljs.test/default :begin-test-var] [m]
+  (println "===" (-> m :var meta :name))
+  (println))
+
+(defn print-summary []
+  (t/report (assoc (:report-counters (t/get-current-env)) :type :summary)))
+
+(defmethod t/report [:cljs.test/default :end-test-vars] [_]
+  (let [env (t/get-current-env)
+        counters (:report-counters env)
+        failures (:fail counters)
+        errors (:error counters)]
+    (when (or (pos? failures)
+              (pos? errors))
+      (set! (.-exitCode js/process) 1))
+    (print-summary)))
+
+(defn get-test-vars [nss]
+  (->> (ns-publics nss)
+       vals
+       (filter (comp :test meta))))
+
+(defn -main [& _args]
+  (t/test-vars (get-test-vars 'srs-test)))