This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
driver.clj
45 lines (40 loc) · 1.77 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
(ns clj-webdriver.driver
(:require [clojure.core.cache :as cache])
(:import org.openqa.selenium.interactions.Actions))
(defrecord Driver [webdriver capabilities cache-spec actions])
(defn- init-cache
"Initialize cache based on given strategy"
([cache-spec]
(when (and (map? cache-spec)
(not (empty? cache-spec)))
(let [strategy-legend {:basic cache/basic-cache-factory,
:fifo cache/fifo-cache-factory,
:lru cache/lru-cache-factory,
:lirs cache/lirs-cache-factory,
:ttl cache/ttl-cache-factory,
:lu cache/lu-cache-factory}]
(atom (apply
(get strategy-legend (:strategy cache-spec))
(:args cache-spec)))))))
(defn init-driver
"Constructor for Driver records. Accepts either an existing WebDriver instance, or a `driver-spec` map with the following keys:
webdriver - WebDriver instance
cache-spec - map with keys :strategy, :args, :include and :exclude, used to setup caching rules"
([] (init-driver {}))
([driver-spec]
(let [wd-class (Class/forName "org.openqa.selenium.WebDriver")
uppers (supers (.getClass driver-spec))]
(if (some #{wd-class} uppers)
(Driver. driver-spec
nil
nil
(Actions. driver-spec))
(let [{:keys [webdriver capabilities cache-spec]} driver-spec]
(Driver. webdriver
capabilities
(assoc cache-spec :cache (init-cache cache-spec))
(Actions. webdriver)))))))
(defn driver?
"Function to check class of a Driver, to prevent needing to import it"
[driver]
(= (class driver) Driver))