Permalink
Browse files

Build out Cookie record and accompanying handling, make sure is worki…

…ng in Taxi API, fixes #44
  • Loading branch information...
1 parent 512866e commit c0fbacd9f7e2899560ec49aa2c79ce9683f43e01 @semperos committed Feb 17, 2012
View
42 src/clj_webdriver/cookie.clj
@@ -0,0 +1,42 @@
+(ns clj-webdriver.cookie)
+
+(defrecord Cookie [cookie name value path expiry domain secure?])
+
+(defn new-cookie*
+ ([name value] (org.openqa.selenium.Cookie. name value))
+ ([name value path] (org.openqa.selenium.Cookie. name value path))
+ ([name value path expiry] (org.openqa.selenium.Cookie. name value path expiry))
+ ([name value domain path expiry] (org.openqa.selenium.Cookie. name value domain path expiry))
+ ([name value domain path expiry secure?] (org.openqa.selenium.Cookie. name value domain path expiry secure?)))
+
+(defn- nn
+ [& sexprs]
+ (not (some nil? sexprs)))
+
+(defn init-cookie
+ "Instantiate a Cookie record. Keys can be: `:name`, `:value`, `:domain`, `:path`, `:expiry`, or `:secure?`. The keys `:name` and `:value` are not optional."
+ [{:keys [cookie name value domain path expiry secure?] :or {cookie nil domain nil path nil expiry nil secure? nil} :as cookie-spec}]
+ {:pre [(or cookie
+ (nn (:name cookie-spec) (:value cookie-spec)))]}
+ (let [cookie-obj (cond
+ (nn cookie) cookie
+ (nn domain path expiry secure?) (new-cookie* name value domain path expiry secure?)
+ (nn domain path expiry) (new-cookie* name value domain path expiry)
+ (nn path expiry) (new-cookie* name value path expiry)
+ (nn path) (new-cookie* name value path)
+ :else (new-cookie* name value))
+ {:keys [name value path domain expiry secure?]} (if cookie
+ {:name (.getName cookie)
+ :value (.getValue cookie)
+ :path (.getPath cookie)
+ :domain (.getDomain cookie)
+ :expiry (.getExpiry cookie)
+ :secure? (.isSecure cookie)}
+ {:name name
+ :value value
+ :path path
+ :domain domain
+ :expiry expiry
+ :secure? secure?})]
+ (Cookie. cookie-obj name value path expiry domain secure?)))
+
View
4 src/clj_webdriver/core.clj
@@ -13,15 +13,15 @@
;; WebDriver API.
;;
(ns clj-webdriver.core
- (:use [clj-webdriver driver element util window-handle options])
+ (:use [clj-webdriver driver element util window-handle options cookie])
(:require [clj-webdriver.js.browserbot :as browserbot-js]
[clj-webdriver.cache :as cache]
[clojure.java.io :as io]
[clojure.string :as string]
[clojure.tools.logging :as log])
(:import [clj_webdriver.driver Driver]
[clj_webdriver.element Element]
- [org.openqa.selenium By WebDriver WebElement Cookie
+ [org.openqa.selenium By WebDriver WebElement
OutputType NoSuchElementException Keys]
[org.openqa.selenium.firefox FirefoxDriver]
[org.openqa.selenium.ie InternetExplorerDriver]
View
30 src/clj_webdriver/core_driver.clj
@@ -147,7 +147,8 @@
(.window (.switchTo (:driver handle)) (:handle handle))
driver)
(number? handle) (do
- driver(switch-to-window driver (nth (window-handles driver) handle)))
+ (switch-to-window driver (nth (window-handles driver) handle))
+ driver)
(nil? handle) (throw (RuntimeException. "No window can be found"))
:else (do
(.window (.switchTo (:webdriver driver)) handle)
@@ -169,18 +170,29 @@
;; Options Interface (cookies)
IOptions
- (add-cookie [driver cookie]
- (.addCookie (.manage (:webdriver driver)) cookie))
+ (add-cookie [driver cookie-spec]
+ (.addCookie (.manage (:webdriver driver)) (:cookie (init-cookie cookie-spec)))
+ driver)
+
(delete-cookie-named [driver cookie-name]
- (.deleteCookieNamed (.manage (:webdriver driver)) cookie-name))
- (delete-cookie [driver cookie]
- (.deleteCookie (.manage (:webdriver driver)) cookie))
+ (.deleteCookieNamed (.manage (:webdriver driver)) cookie-name)
+ driver)
+
+ (delete-cookie [driver cookie-spec]
+ (.deleteCookie (.manage (:webdriver driver)) (:cookie (init-cookie cookie-spec)))
+ driver)
+
(delete-all-cookies [driver]
- (.deleteAllCookies (.manage (:webdriver driver))))
+ (.deleteAllCookies (.manage (:webdriver driver)))
+ driver)
+
(cookies [driver]
- (into #{} (.getCookies (.manage (:webdriver driver)))))
+ (into #{} (map #(init-cookie {:cookie %})
+ (.getCookies (.manage (:webdriver driver))))))
+
(cookie-named [driver cookie-name]
- (.getCookieNamed (.manage (:webdriver driver)) cookie-name))
+ (let [cookie-obj (.getCookieNamed (.manage (:webdriver driver)) cookie-name)]
+ (init-cookie {:cookie cookie-obj})))
;; Find Functions
View
26 src/clj_webdriver/options.clj
@@ -1,27 +1,11 @@
-(ns clj-webdriver.options
- (:import org.openqa.selenium.Cookie))
+(ns clj-webdriver.options)
(defprotocol IOptions
"Options interface, including cookie and timeout handling"
- (add-cookie [driver cookie] "Add a new cookie to the browser session")
- (delete-cookie-named [driver cookie] "Delete a cookie given its name")
- (delete-cookie [driver cookie] "Delete a cookie given a cookie instance")
+ (add-cookie [driver cookie-spec] "Add a new cookie to the browser session")
+ (delete-cookie-named [driver cookie-name] "Delete a cookie given its name")
+ (delete-cookie [driver cookie-spec] "Delete a cookie given a cookie instance")
(delete-all-cookies [driver] "Delete all cookies defined in the current session")
(cookies [driver] "Retrieve a set of cookies defined in the current session")
- (cookie-named [driver name] "Retrieve a cookie object given its name"))
+ (cookie-named [driver cookie-name] "Retrieve a cookie object given its name"))
-(defn new-cookie
- "Create a new cookie instance"
- ([name value] (new-cookie name value "/" nil))
- ([name value path] (new-cookie name value path nil))
- ([name value path date] (new Cookie name value path date)))
-
-(defn cookie-name
- "Retrieve the name of a particular cookie"
- [cookie]
- (.getName cookie))
-
-(defn cookie-value
- "Retrieve the value of a particular cookie"
- [cookie]
- (.getValue cookie))
View
39 src/clj_webdriver/taxi.clj
@@ -91,9 +91,11 @@
(core/to *driver* url))
(defn back
- "Navigate back in the browser history."
- []
- (core/back *driver*))
+ "Navigate back in the browser history, optionally `n` times."
+ ([] (back 1))
+ ([n]
+ (dotimes [m n]
+ (core/back *driver*))))
(defn close
"Close the browser. If multiple windows are open, this only closes the active window."
@@ -107,8 +109,10 @@
(defn forward
"Navigate forward in the browser history."
- []
- (core/forward *driver*))
+ ([] (forward 1))
+ ([n]
+ (dotimes [m n]
+ (core/forward *driver*))))
(defn get-url
"Navigate the browser to `url`."
@@ -184,32 +188,34 @@
[]
(core/switch-to-active *driver*))
+;; TODO: test coverage
(defn add-cookie
"Add the given `cookie` to the browser session."
[cookie]
(options/add-cookie *driver* cookie))
-(defn delete-cookie-named
- "Delete the cookie with name `cookie-name` from the browser session."
- [cookie-name]
- (options/delete-cookie-named *driver* cookie-name))
-
+;; TODO: test coverage
(defn delete-cookie
- "Delete the given `cookie` from the browser session."
- [cookie]
- (options/delete-cookie *driver* cookie))
+ "Provided the name of a cookie or a Cookie record itself, delete it from the browser session."
+ [name-or-obj]
+ (if (string? name-or-obj)
+ (options/delete-cookie-named *driver* name-or-obj)
+ (options/delete-cookie *driver* name-or-obj)))
+;; TODO: test coverage
(defn delete-all-cookies
"Delete all cookies from the browser session."
[]
(options/delete-all-cookies *driver*))
+;; TODO: test coverage
(defn cookies
"Return all cookies in the browser session."
[]
(options/cookies *driver*))
-(defn cookie-named
+;; TODO: test coverage
+(defn cookie
"Return the cookie with name `cookie-name`."
[cookie-name]
(options/cookie-named *driver* cookie-name))
@@ -588,4 +594,7 @@
[& query-action-maps]
(doseq [entry query-action-maps
[k v] entry]
- (quick-fill* k v true)))
+ (quick-fill* k v true)))
+
+
+;; IDEA: Go 2 pages back/forward

0 comments on commit c0fbacd

Please sign in to comment.