-
Notifications
You must be signed in to change notification settings - Fork 93
/
driver.clj
518 lines (425 loc) · 12.2 KB
/
driver.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
(ns ^:no-doc etaoin.impl.driver
"Some utilities to work with driver's data structure.
Links for development:
Firefox command line flags:
/Applications/Firefox.app/Contents/MacOS/firefox-bin --help
Chrome binary path:
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome
Chrome CLI args:
https://peter.sh/experiments/chromium-command-line-switches/
Chrome capabilities:
https://sites.google.com/a/chromium.org/chromedriver/capabilities
Firefox capabilities:
https://github.com/mozilla/geckodriver/#firefox-capabilities
Firefox profiles:
https://support.mozilla.org/en-US/kb/profiles-where-firefox-stores-user-data
Safari endpoints
https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/WebDriverEndpointDoc/Commands/Commands.html
Edge capabilities and endpoints
https://docs.microsoft.com/en-us/microsoft-edge/webdriver
JSON Wire protocol (obsolete)
https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol
Selenium Python source code for Firefox
https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/firefox/options.py
"
(:require
[babashka.fs :as fs]
[clojure.string :as string]
[clojure.tools.logging :as log]
[etaoin.impl.util :refer [deep-merge defmethods]]))
(set! *warn-on-reflection* true)
(defn dispatch-driver
[driver & _]
(:type driver))
(defn append-args
[args extra]
(concat args extra))
(defn prepend-args
[args extra]
(concat extra args))
(defn set-path
"Sets path to the driver's binary file."
[driver path]
(update driver :args prepend-args [path]))
(defn set-args
[driver args]
(update driver :args append-args args))
(defn get-args
[driver]
(or (:args driver) []))
;;
;; Port
;;
(defmulti set-port
"Updates driver's map with the given port added to the args."
{:arglists '([driver port])}
dispatch-driver)
(defmethods set-port
[:firefox :safari]
[driver port]
(set-args driver ["--port" port]))
(defmethods set-port
[:chrome :edge]
[driver port]
(set-args driver [(str "--port=" port)]))
(defmethod set-port
:phantom
[driver port]
(set-args driver ["--webdriver" port]))
;;
;; capabilities
;;
(defn set-capabilities
[driver caps]
(if caps
(update driver :capabilities deep-merge caps)
driver))
(defn set-load-strategy
[driver strategy]
(assoc-in driver [:capabilities :pageLoadStrategy] strategy))
;;
;; options utils
;;
(defmulti options-name dispatch-driver) ;; todo nil default
(defmethod options-name
:firefox
[_driver]
:moz:firefoxOptions)
(defmethod options-name
:chrome
[_driver]
:chromeOptions)
(defmethod options-name
:safari
[_driver]
:safariOptions)
(defmethod options-name
:edge
[_driver]
:edgeOptions)
(defmethod options-name
:opera
[_driver]
:operaOptions)
(defn set-options-args
"Adds command line arguments for a browser binary (not a driver)."
[driver args]
(update-in driver
[:capabilities (options-name driver) :args]
append-args (map str args)))
;;
;; Profiles
;;
(defmulti set-profile dispatch-driver)
(defmethod set-profile
:default
[driver _profile]
(log/infof "This driver doesn't support setting a profile.")
driver)
(defmethod set-profile
:chrome
;; Chrome adds the trailing `/Default` part to the profile path.
;; To prevent duplication, let's clear the given path manually.
[driver ^String profile]
(let [profile (fs/file profile)
profile (if (= "Default" (fs/file-name profile))
(fs/parent profile)
profile)
user-data-dir (str (fs/parent profile))
profile-dir (str (fs/file-name profile))]
(set-options-args driver [(format "--user-data-dir=%s" user-data-dir)
(format "--profile-directory=%s" profile-dir)])))
(defmethod set-profile
:firefox
;; When setting a custom profile, geckodriver cannot
;; connect to the browser. The issue
;; https://github.com/mozilla/geckodriver/issues/1058
;; says to specify a marionette port manually.
[driver profile]
(-> driver
(set-options-args ["-profile" profile])
((fn [driver]
(if (some #(= "--marionette-port" %) (get-args driver))
driver
(set-args driver ["--marionette-port" 2828]))))))
;;
;; window size
;;
(defmulti set-window-size
"Adds browser's command line arguments for setting initial window size."
{:arglists '([driver w h])}
dispatch-driver)
(defmethod set-window-size
:default
[driver _w _h]
(log/infof "This driver doesn't support setting window size.")
driver)
(defmethod set-window-size
:chrome
[driver w h]
(set-options-args driver [(format "--window-size=%s,%s" w h)]))
(defmethod set-window-size
:firefox
[driver w h]
(set-options-args driver ["-width" w "-height" h]))
;;
;; initial URL
;;
(defmulti set-url
"Sets the default URL that the browser should open by default."
{:arglists '([driver url])}
dispatch-driver)
(defmethod set-url
:default
[driver _url]
(log/infof "This driver doesn't support setting initial URL.")
driver)
(defmethod set-url
:firefox
[driver url]
(set-options-args driver ["--new-window" url]))
;; Don't know why but Chrome ignores all the --new-window, --app
;; or --google-base-url parameters when starting.
;;
;; headless feature
;;
(defmulti set-headless
{:arglists '([driver])}
dispatch-driver)
(defmethod set-headless
:default
[driver]
(log/infof "This driver doesn't support setting headless mode.")
driver)
(defmethods set-headless
[:edge :chrome :firefox]
[driver]
(-> driver
(assoc :headless true)
(set-options-args ["--headless"])))
(defmulti is-headless?
{:arglists '([driver])}
dispatch-driver)
(defmethod is-headless?
:default
[driver]
(if-let [args (get-in driver [:capabilities (options-name driver) :args])]
(contains? (set args) "--headless")
(:headless driver)))
(defmethod is-headless?
:phantom
[_driver]
true)
;;
;; HTTP proxy
;;
(defn proxy->w3c
[proxy]
(let [{:keys [http ssl ftp socks pac-url bypass]} proxy]
(cond-> nil
(or ssl http
ftp socks) (assoc :proxyType "manual")
pac-url (assoc :proxyType "pac"
:proxyAutoconfigUrl pac-url)
http (assoc :httpProxy http)
ssl (assoc :sslProxy ssl)
ftp (assoc :ftpProxy ftp)
socks (assoc :socksProxy (:host socks)
:socksVersion (or (:version socks) 5))
bypass (assoc :noProxy bypass))))
(defn set-proxy
[driver proxy]
(let [proxy-w3c (proxy->w3c proxy)]
(set-capabilities driver {:proxy proxy-w3c})))
;;
;; Custom preferences
;;
(defmulti set-prefs
{:arglists '([driver prefs])}
dispatch-driver)
(defmethod set-prefs
:default
[driver _prefs]
(log/infof "This driver doesn't support setting preferences.")
driver)
(defmethods set-prefs
[:firefox :chrome]
[driver prefs]
(update-in driver
[:capabilities (options-name driver) :prefs]
merge prefs))
;;
;; Download folder
;;
(defn- add-trailing-slash
^String [^String path]
(let [sep java.io.File/separator]
(if (string/ends-with? path sep)
path
(str path sep))))
(defmulti set-download-dir
{:arglists '([driver path])}
dispatch-driver)
(defmethod set-download-dir
:default
[driver _path]
(log/infof "This driver doesn't support setting a download directory.")
driver)
;; https://github.com/rshf/chromedriver/issues/338
;; trailing slash is mandatory for Chrome
(defmethod set-download-dir
:chrome
[driver path]
(set-prefs driver {:download.default_directory (add-trailing-slash path)
:download.prompt_for_download false}))
(def ^{:private true
:doc "A set of content types that should be downloaded without asking a user."}
ff-content-types
#{"application/gzip"
"application/json"
"application/msword"
"application/octet-stream"
"application/pdf"
"application/rtf"
"application/vnd.ms-excel"
"application/vnd.ms-powerpoint"
"application/vnd.openxmlformats-officedocument.presentationml.presentation"
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
"application/x-7z-compressed"
"application/x-rar-compressed"
"application/x-shockwave-flash"
"application/x-tar"
"application/zip"
"audio/flac"
"audio/mpeg"
"audio/ogg"
"image/svg+xml"
"text/csv"
"text/javascript"
"text/plain"
"text/xml"
"video/mp4"
"video/mpeg"
"video/ogg"
"video/quicktime"
"video/webm"
"video/x-flv"
"video/x-msvideo"})
(defmethod set-download-dir
:firefox
[driver path]
(set-prefs driver {:browser.download.dir path
:browser.download.folderList 2
:browser.download.useDownloadDir true
:browser.helperApps.neverAsk.saveToDisk
(string/join ";" ff-content-types)}))
;;
;; binary path
;;
(defmulti set-binary
{:arglists '([driver binary])}
dispatch-driver)
(defmethod set-binary
:default
[driver binary]
(assoc-in driver
[:capabilities (options-name driver) :binary]
binary))
;;
;; logging
;;
(defn- remap-log-level
"Mapping from a human-friendly log level to a system one."
[level]
(case level
(nil
:off
:none) "OFF"
:debug "DEBUG"
:info "INFO"
(:warn
:warning) "WARNING"
(:err
:error
:severe
:crit
:critical) "SEVERE"
:all "ALL"
(assert false (format "Logging level %s is unsupported." level))))
;;
;; https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities#loggingpreferences-json-object
;;
(defmulti set-browser-log-level
"Sets browser logging level."
{:arglists '([driver binary])}
dispatch-driver)
(defmethod set-browser-log-level
:default
[driver level]
(assoc-in driver
[:capabilities :loggingPrefs :browser]
(remap-log-level level)))
;; http://chromedriver.chromium.org/capabilities
;; http://chromedriver.chromium.org/logging/performance-log
(defn set-perf-logging
"
categories example:
[:browser :devtools :devtools.timeline]
"
[driver & [{:keys [level network? page? categories interval]
:or {level :all
network? true
page? false
categories [:devtools.network]
interval 1000}}]]
(update driver :capabilities
(fn [capabilities]
(-> capabilities
(assoc-in [:loggingPrefs :performance]
(remap-log-level level))
(assoc-in [(options-name driver) :perfLoggingPrefs]
{:enableNetwork network?
:enablePage page?
:traceCategories (string/join "," (map name categories))
:bufferUsageReportingInterval interval})))))
(defmulti set-driver-log-level
dispatch-driver)
(defmethod set-driver-log-level
:default
[driver _]
(log/infof "The log level setting is not implemented for this driver.")
driver)
(defmethods set-driver-log-level
[:chrome :edge]
[driver log-level]
(set-args driver [(format "--log-level=%s" log-level)]))
(defmethod set-driver-log-level
:firefox
[driver log-level]
(set-args driver ["--log" log-level]))
(defmethod set-driver-log-level
:phantom
[driver log-level]
(set-args driver [(format "--webdriver-loglevel=%s" log-level)]))
;;
;; User-Agent
;; https://stackoverflow.com/questions/29916054/
;;
(defmulti set-user-agent
"Set User-Agent header for the driver."
{:arglists '([driver user-agent])}
dispatch-driver)
(defmethods set-user-agent
[:chrome :edge]
[driver user-agent]
(set-options-args driver [(str "--user-agent=" user-agent)]))
(defmethods set-user-agent
[:firefox]
[driver user-agent]
(set-prefs driver {:general.useragent.override user-agent}))
(defmethods set-user-agent
[:default]
[driver _user-agent]
(log/infof "This driver doesn't support setting a user-agent.")
driver)