Skip to content

Commit ed8cb08

Browse files
Move webdriver component to test utils
1 parent 14844dc commit ed8cb08

File tree

5 files changed

+41
-45
lines changed

5 files changed

+41
-45
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ _TODO: add features!_
4343

4444
The template generates a Clojure project with the following structure:
4545

46-
4746
```
4847
├── .clj-kondo/ # Clojure linting configuration
4948
├── .github/ # GitHub Actions workflows and configurations
50-
├── .kamal/ # Kamal secrets template (_only used if you use Kamal_)
51-
├── config/ # Kamal deployment configuration (_only used if you use Kamal_)
52-
├── db/ # Database files directory (_only used if you use SQLite_)
49+
├── .kamal/ # Kamal secrets template (only used if you use Kamal)
50+
├── config/ # Kamal deployment configuration (only used if you use Kamal)
51+
├── db/ # Database empty directory (only used if you use SQLite)
5352
├── dev/ # Development configuration directory
5453
│ └── user.clj # User-specific development configuration
5554
├── resources/ # Static resources and configuration files
@@ -58,7 +57,7 @@ The template generates a Clojure project with the following structure:
5857
│ ├── config.edn # Main configuration file for the application
5958
│ ├── config.dev.edn # Development-specific configuration
6059
│ ├── config.e2e.edn # Test-specific configuration for end-to-end testing
61-
│ └── logback.xml/ # Logging configuration file
60+
│ └── logback.xml # Logging configuration file
6261
├── src/ # Source code directory
6362
│ └── {{name}} # Main namespace directory
6463
│ ├── core.clj # Application entry point
@@ -70,8 +69,7 @@ The template generates a Clojure project with the following structure:
7069
├── test/ # Test files directory
7170
│ └── {{name}} # Test namespace directory
7271
│ ├── home_test.clj # Example test for home page
73-
│ ├── test_utils.clj # Test utilities
74-
│ └── webdriver.clj # Webdriver system component to be used in `config.e2e.edn`
72+
│ └── test_utils.clj # Test utilities
7573
├── .cljfmt.edn # Formatting configuration
7674
├── .gitignore # Git ignore rules
7775
├── .mise.toml # mise-en-place configuration with system dependencies
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#merge [#include "config.edn"
2-
{:{{main/ns}}.webdriver/webdriver {:server #ig/ref :{{main/ns}}.server/server}}]
2+
{:{{main/ns}}.test-utils/webdriver {:server #ig/ref :{{main/ns}}.server/server}}]

resources/io/github/abogoyavlensky/clojure_stack_lite/test/home_test.clj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
[integrant-extras.tests :as ig-extras]
55
[reitit-extras.tests :as reitit-extras]
66
[{{main/ns}}.server :as-alias server]
7-
[{{main/ns}}.test-utils :as test-utils]
8-
[{{main/ns}}.webdriver :as-alias webdriver]))
7+
[{{main/ns}}.test-utils :as test-utils]))
98

109
(use-fixtures :once
1110
(ig-extras/with-system "config.e2e.edn"))
@@ -15,7 +14,7 @@
1514

1615
(deftest test-home-page-loads-correctly
1716
(testing "Home page loads and displays correctly"
18-
(let [driver (get-in ig-extras/*test-system* [::webdriver/webdriver :driver])
17+
(let [driver (get-in ig-extras/*test-system* [::test-utils/webdriver :driver])
1918
server (::server/server ig-extras/*test-system*)
2019
url (reitit-extras/get-server-url server :container)]
2120

resources/io/github/abogoyavlensky/clojure_stack_lite/test/test_utils.clj

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
(ns {{main/ns}}.test-utils
2-
(:require [integrant-extras.tests :as ig-extras]
3-
[{{main/ns}}.db :as db]))
2+
(:require [clojure.tools.logging :as log]
3+
[etaoin.api :as etaoin]
4+
[integrant-extras.tests :as ig-extras]
5+
[integrant.core :as ig]
6+
[{{main/ns}}.db :as db])
7+
(:import [org.testcontainers Testcontainers]
8+
[org.testcontainers.containers GenericContainer]))
49

510
(defn- all-tables
611
[db]
@@ -18,3 +23,29 @@
1823
:when (not= :schema_version table)]
1924
(db/exec! db {:delete-from table}))
2025
(f)))
26+
27+
(def ^:private WEBDRIVER-PORT 4444)
28+
(def ^:private WEBDRIVER-IMAGE "selenium/standalone-chromium:131.0")
29+
30+
(defmethod ig/init-key ::webdriver
31+
[_ {:keys [server]}]
32+
(log/info (str "[DB] Starting webdriver..."))
33+
(let [server-port (.getLocalPort (first (.getConnectors server)))
34+
; Expose port from local machine to container
35+
_ (Testcontainers/exposeHostPorts (int-array [server-port]))
36+
; Start the webdriver container
37+
container (doto (GenericContainer. WEBDRIVER-IMAGE)
38+
(.withExposedPorts (into-array Integer [(int WEBDRIVER-PORT)]))
39+
(.withReuse true)
40+
(.start))
41+
driver (etaoin/chrome-headless {:port (.getMappedPort container WEBDRIVER-PORT)
42+
:host (.getHost container)
43+
:args ["--no-sandbox"]})]
44+
{:container container
45+
:driver driver}))
46+
47+
(defmethod ig/halt-key! ::webdriver
48+
[_ {:keys [driver]}]
49+
(log/info (str "[DB] Closing webdriver..."))
50+
; Do not stop the container to be able to reuse it
51+
(etaoin/quit driver))

resources/io/github/abogoyavlensky/clojure_stack_lite/test/webdriver.clj

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)