-
Notifications
You must be signed in to change notification settings - Fork 93
/
main.clj
133 lines (99 loc) · 3.29 KB
/
main.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
(ns etaoin.ide.main
"
Provide an CLI entry point for running IDE files.
Example:
```shell
clojure -M -m etaoin.ide.main -d firefox -p '{:port 8888}' -f /path/to/script.side
```
See the [User Guide](/doc/01-user-guide.adoc#selenium-ide-cli) for more info.
"
(:gen-class)
(:require
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.tools.cli :as cli]
[etaoin.api :as api]
[etaoin.ide.flow :as flow]
[etaoin.impl.util :as util]))
(set! *warn-on-reflection* true)
(def ^:private browsers-set
#{:chrome :safari :firefox :edge :phantom})
(defn- str->vec
[string]
(str/split string #","))
(def ^:private cli-options
[["-d" "--driver-name name" "The name of driver. The default is `chrome`"
:default :chrome
:parse-fn keyword
:validate [browsers-set
(str "Must be one of the list items - " (str/join ", " (map name browsers-set)))]]
["-p" "--params params" "Parameters for the driver represented as an EDN string, e.g. '{:port 8080}'"
:default {}
:parse-fn read-string]
["-f" "--file path" "Path to an IDE file on disk"]
["-r" "--resource path" "Path to an IDE resource"]
[nil "--test-ids ids" "Comma-separeted test ID(s)"
:parse-fn str->vec]
[nil "--suite-ids ids" "Comma-separeted suite ID(s)"
:parse-fn str->vec]
[nil "--test-names names" "Comma-separeted test name(s)"
:parse-fn str->vec]
[nil "--suite-names names" "Comma-separeted suite name(s)"
:parse-fn str->vec]
[nil "--base-url url" "Base URL for tests"]
["-h" "--help"]])
(def ^:private help
"
This is a CLI interface for running Selenium IDE files.
Usage examples:
;; from clojure
clojure -M -m etaoin.ide.main -d firefox -p '{:port 8888}' -r ide/test.side
;; from a jar
java -cp .../poject.jar -m etaoin.ide.main -d firefox -p '{:port 8888}' -f ide/test.side
Options:")
(defn ^:private usage [options-summary]
(->> [help options-summary]
(str/join \newline)))
(defn ^:private error-msg [errors]
(str "The following errors occurred while parsing your command:\n\n"
(str/join \newline errors)))
(def ^:private opt-fields
[:base-url
:test-ids
:test-names
:suite-ids
:suite-names])
(defn- run-script
"
Run a Selenium IDE file. The `source` is something
that might be `slurp`ed.
"
[source {:keys [driver-name params] :as options}]
(let [opt (select-keys options opt-fields)]
(api/with-driver driver-name params driver
(flow/run-ide-script driver source opt))))
(defn -main
"The main CLI entrypoint.
See [Selenium IDE CLI docs](/doc/01-user-guide.adoc#selenium-ide-cli)"
[& args]
(let [{:keys [errors summary options]}
(cli/parse-opts args cli-options)
{:keys [help file resource]}
options]
(cond
errors
(util/exit 1 (error-msg errors))
help
(util/exit 0 (usage summary))
file
(let [ide-file (io/file file)]
(when-not (and (.exists ide-file)
(not (.isDirectory ide-file)))
(util/exit 1 "The IDE file not found"))
(run-script ide-file options))
resource
(if-let [r (io/resource resource)]
(run-script r options)
(util/exit 1 "Resource not found"))
:else
(util/exit 1 "Specify the path to the ide file: `--file` or `--resource`"))))