Browse files

Update signature for new-driver and related functions to take *-spec …

…maps instead of individual parameters
  • Loading branch information...
1 parent d794ded commit 13030c45028933e82eff69d5753380dec5fbabd0 @semperos semperos committed Jan 4, 2012
Showing with 46 additions and 43 deletions.
  1. +12 −9 src/clj_webdriver/cache.clj
  2. +21 −20 src/clj_webdriver/core.clj
  3. +12 −11 src/clj_webdriver/driver.clj
  4. +0 −2 src/clj_webdriver/util.clj
  5. +1 −1 src/clj_webdriver/wait.clj
@@ -45,7 +45,8 @@
(ns clj-webdriver.cache
- (:use [clj-webdriver.element :only [is-element?]])
+ (:use [clj-webdriver.element :only [is-element?]]
+ [clj-webdriver.driver :only [get-cache]])
(:require [ :as log])
(:import clj_webdriver.driver.Driver))
@@ -81,7 +82,7 @@
(vector? raw-query) {:query raw-query}
(keyword? raw-query) {:query [raw-query]}
:else raw-query)]
- (contains? @(:element-cache driver) query)))
+ (contains? @(get-cache driver) query)))
;; here the query needs the same normalizing as occurs
;; in the cacheable? function below
@@ -91,36 +92,38 @@
(keyword? raw-query) {:query [raw-query]}
:else raw-query)]
(log/info (str "Inserting " query " entry into cache."))
- (swap! (:element-cache driver) assoc query value)))
+ (swap! (get-cache driver) assoc query value)))
(retrieve [driver raw-query]
(let [query (cond
(vector? raw-query) {:query raw-query}
(keyword? raw-query) {:query [raw-query]}
:else raw-query)]
(log/info (str "Retrieving " query " entry from cache."))
- (get @(:element-cache driver) query)))
+ (get @(get-cache driver) query)))
(cache-url [driver]
- (get @(:element-cache driver) :url))
+ (get @(get-cache driver) :url))
(set-cache-url [driver url]
- (swap! (:element-cache driver) assoc :url url))
+ (swap! (get-cache driver) assoc :url url))
(delete [driver raw-query]
(let [query (cond
(vector? raw-query) {:query raw-query}
(keyword? raw-query) {:query [raw-query]}
:else raw-query)]
- (swap! (:element-cache driver) dissoc query)))
+ (swap! (get-cache driver) dissoc query)))
(when (cache-enabled? driver)
- (reset! (:element-cache driver) {:url (current-url driver)})))
+ (reset!
+ (get-cache driver)
+ {:url (current-url driver)})))
([driver seed-value]
(when (cache-enabled? driver)
- (reset! (:element-cache driver) seed-value))))
+ (reset! (get-cache driver) seed-value))))
(cacheable? [driver raw-query]
;; normalize query
@@ -39,23 +39,27 @@
:htmlunit HtmlUnitDriver})
(defn new-webdriver*
- "Instantiate a new WebDriver instance given a browser type. If an additional profile object or string is passed in, Firefox will be started with the given profile instead of the default."
- ([browser]
- (.newInstance (webdriver-drivers (keyword browser))))
- ([browser profile]
- {:pre [(= browser :firefox)]}
- (FirefoxDriver. profile)))
+ "Return a Selenium-WebDriver WebDriver instance, optionally configured to leverage a custom FirefoxProfile."
+ ([browser-spec]
+ (let [{:keys [browser profile] :or {browser :firefox
+ profile nil}} browser-spec]
+ (if-not profile
+ (.newInstance (webdriver-drivers (keyword browser)))
+ (FirefoxDriver. profile)))))
(defn new-driver
- "Create new Driver given a browser type. If an additional profile object or string is passed in, Firefox will be started with the given profile instead of the default.
- This is the preferred method for starting up a browser, as it leverages clj-webdriver-specific functionality not available with vanilla WebDriver instances. You can always access the underlying WebDriver instance with the :webdriver key of your Driver record."
- ([browser]
- (init-driver (new-webdriver* browser)))
- ([browser cache-spec]
- (init-driver (new-webdriver* browser) cache-spec))
- ([browser cache-spec cache-args]
- (init-driver (new-webdriver* browser) cache-spec cache-args)))
+ "Start a new Driver instance. The `browser-spec` can include `:browser`, `:profile`, and `:cache-spec` keys.
+ The `:browser` can be one of `:firefox`, `:ie`, `:chrome` or `:htmlunit`.
+ The `:profile` should be an instance of FirefoxProfile you wish to use.
+ The `:cache-spec` can contain `:strategy`, `:args`, `:include` and/or `:exclude keys. See documentation on caching for more details."
+ ([browser-spec]
+ (let [{:keys [browser profile cache-spec] :or {browser :firefox
+ profile nil
+ cache-spec {}}} browser-spec]
+ (init-driver {:webdriver (new-webdriver* {:browser browser
+ :profile profile})
+ :cache-spec cache-spec}))))
;;; Protocols for API ;;;
(defprotocol IDriver
@@ -155,11 +159,8 @@
(defn start
"Shortcut to instantiate a driver, navigate to a URL, and return the driver for further use"
- ([browser url] (start browser url :driver))
- ([browser url driver-type]
- (let [driver (if (= :webdriver driver-type)
- (new-webdriver* browser)
- (new-driver browser))]
+ ([browser-spec url]
+ (let [driver (new-driver browser-spec)]
(get-url driver url)
@@ -1,12 +1,13 @@
(ns clj-webdriver.driver
(require [clojure.core.cache :as cache]))
-(defrecord Driver [webdriver cache-spec element-cache middlewares])
+(defrecord Driver [webdriver cache-spec])
(defn- init-cache
"Initialize cache based on given strategy"
- (when-not (nil? cache-spec)
+ (when (and (map? cache-spec)
+ (not (empty? cache-spec)))
(let [strategy-legend {:basic cache/->BasicCache,
:fifo cache/->FIFOCache,
:lru cache/->LRUCache,
@@ -22,16 +23,16 @@
webdriver - WebDriver instance
cache-spec - map with keys :strategy, :args, :include and :exclude"
- ([] (Driver. nil nil nil nil))
- ([webdriver] (Driver. webdriver nil nil nil))
- ([webdriver cache-spec]
- (Driver. webdriver cache-spec (init-cache cache-spec) nil))
- ([webdriver cache-spec element-cache]
- (Driver. webdriver cache-spec element-cache nil))
- ([webdriver cache-spec element-cache middlewares]
- (Driver. webdriver cache-spec element-cache middlewares)))
+ ([driver-spec]
+ (let [{:keys [webdriver cache-spec]} driver-spec]
+ (Driver. webdriver
+ (assoc cache-spec :cache (init-cache cache-spec))))))
(defn is-driver?
"Function to check class of a Driver, to prevent needing to import it"
- (= (class driver) Driver))
+ (= (class driver) Driver))
+(defn get-cache
+ [driver]
+ (get-in driver [:cache-spec :cache]))
@@ -86,8 +86,6 @@
`(when (not (or (nil? ~obj) (empty? ~obj)))
-;; #clj_webdriver.driver.Driver{:webdriver #<Title: Ministache, URL: http://localhost:5744/, Browser: firefox, Version: 7.0.1, JS Enabled: true, Native Events Enabled: false, Object: org.openqa.selenium.firefox.FirefoxDriver@576a13d5>, :cache-spec {:strategy :basic, :args [], :include [#<core$fn__3522 clj_webdriver.core$fn__3522@42e87d99>]}, :element-cache #<Atom@4005f23d: {}>, :middlewares nil}
;; from Clojure's core.clj
(defn pr-on
{:private true
@@ -27,5 +27,5 @@
([driver pred timeout interval]
(let [wait (WebDriverWait. (:webdriver driver) (/ timeout 1000) interval)]
(.until wait (proxy [ExpectedCondition] []
- (apply [d] (pred (init-driver d)))))
+ (apply [d] (pred (init-driver {:webdriver d})))))

0 comments on commit 13030c4

Please sign in to comment.