Skip to content
This repository
Browse code

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

…ng in Taxi API, fixes #44
  • Loading branch information...
commit c0fbacd9f7e2899560ec49aa2c79ce9683f43e01 1 parent 512866e
Daniel Gregoire authored February 17, 2012
42  src/clj_webdriver/cookie.clj
... ...
@@ -0,0 +1,42 @@
4  src/clj_webdriver/core.clj
@@ -13,7 +13,7 @@
13 13
 ;; WebDriver API.
14 14
 ;;
15 15
 (ns clj-webdriver.core
16  
-  (:use [clj-webdriver driver element util window-handle options])
  16
+  (:use [clj-webdriver driver element util window-handle options cookie])
17 17
   (:require [clj-webdriver.js.browserbot :as browserbot-js]
18 18
             [clj-webdriver.cache :as cache]
19 19
             [clojure.java.io :as io]
@@ -21,7 +21,7 @@
21 21
             [clojure.tools.logging :as log])
22 22
   (:import [clj_webdriver.driver Driver]
23 23
            [clj_webdriver.element Element]
24  
-           [org.openqa.selenium By WebDriver WebElement Cookie
  24
+           [org.openqa.selenium By WebDriver WebElement
25 25
                                 OutputType NoSuchElementException Keys]
26 26
            [org.openqa.selenium.firefox FirefoxDriver]
27 27
            [org.openqa.selenium.ie InternetExplorerDriver]
30  src/clj_webdriver/core_driver.clj
@@ -147,7 +147,8 @@
147 147
                                    (.window (.switchTo (:driver handle)) (:handle handle))
148 148
                                    driver)
149 149
      (number? handle)            (do
150  
-                                   driver(switch-to-window driver (nth (window-handles driver) handle)))
  150
+                                   (switch-to-window driver (nth (window-handles driver) handle))
  151
+                                   driver)
151 152
      (nil? handle)               (throw (RuntimeException. "No window can be found"))
152 153
      :else                       (do
153 154
                                    (.window (.switchTo (:webdriver driver)) handle)
@@ -169,18 +170,29 @@
169 170
 
170 171
   ;; Options Interface (cookies)
171 172
   IOptions
172  
-  (add-cookie [driver cookie]
173  
-    (.addCookie (.manage (:webdriver driver)) cookie))
  173
+  (add-cookie [driver cookie-spec]
  174
+    (.addCookie (.manage (:webdriver driver)) (:cookie (init-cookie cookie-spec)))
  175
+    driver)
  176
+  
174 177
   (delete-cookie-named [driver cookie-name]
175  
-    (.deleteCookieNamed (.manage (:webdriver driver)) cookie-name))
176  
-  (delete-cookie [driver cookie]
177  
-    (.deleteCookie (.manage (:webdriver driver)) cookie))
  178
+    (.deleteCookieNamed (.manage (:webdriver driver)) cookie-name)
  179
+    driver)
  180
+  
  181
+  (delete-cookie [driver cookie-spec]
  182
+    (.deleteCookie (.manage (:webdriver driver)) (:cookie (init-cookie cookie-spec)))
  183
+    driver)
  184
+  
178 185
   (delete-all-cookies [driver]
179  
-    (.deleteAllCookies (.manage (:webdriver driver))))
  186
+    (.deleteAllCookies (.manage (:webdriver driver)))
  187
+    driver)
  188
+  
180 189
   (cookies [driver]
181  
-    (into #{} (.getCookies (.manage (:webdriver driver)))))
  190
+    (into #{} (map #(init-cookie {:cookie %})
  191
+                   (.getCookies (.manage (:webdriver driver))))))
  192
+  
182 193
   (cookie-named [driver cookie-name]
183  
-    (.getCookieNamed (.manage (:webdriver driver)) cookie-name))
  194
+    (let [cookie-obj (.getCookieNamed (.manage (:webdriver driver)) cookie-name)]
  195
+      (init-cookie {:cookie cookie-obj})))
184 196
 
185 197
 
186 198
   ;; Find Functions
26  src/clj_webdriver/options.clj
... ...
@@ -1,27 +1,11 @@
1  
-(ns clj-webdriver.options
2  
-  (:import org.openqa.selenium.Cookie))
  1
+(ns clj-webdriver.options)
3 2
 
4 3
 (defprotocol IOptions
5 4
   "Options interface, including cookie and timeout handling"
6  
-  (add-cookie [driver cookie] "Add a new cookie to the browser session")
7  
-  (delete-cookie-named [driver cookie] "Delete a cookie given its name")
8  
-  (delete-cookie [driver cookie] "Delete a cookie given a cookie instance")
  5
+  (add-cookie [driver cookie-spec] "Add a new cookie to the browser session")
  6
+  (delete-cookie-named [driver cookie-name] "Delete a cookie given its name")
  7
+  (delete-cookie [driver cookie-spec] "Delete a cookie given a cookie instance")
9 8
   (delete-all-cookies [driver] "Delete all cookies defined in the current session")
10 9
   (cookies [driver] "Retrieve a set of cookies defined in the current session")
11  
-  (cookie-named [driver name] "Retrieve a cookie object given its name"))
  10
+  (cookie-named [driver cookie-name] "Retrieve a cookie object given its name"))
12 11
 
13  
-(defn new-cookie
14  
-  "Create a new cookie instance"
15  
-  ([name value] (new-cookie name value "/" nil))
16  
-  ([name value path] (new-cookie name value path nil))
17  
-  ([name value path date] (new Cookie name value path date)))
18  
-
19  
-(defn cookie-name
20  
-  "Retrieve the name of a particular cookie"
21  
-  [cookie]
22  
-  (.getName cookie))
23  
-
24  
-(defn cookie-value
25  
-  "Retrieve the value of a particular cookie"
26  
-  [cookie]
27  
-  (.getValue cookie))
39  src/clj_webdriver/taxi.clj
@@ -91,9 +91,11 @@
91 91
   (core/to *driver* url))
92 92
 
93 93
 (defn back
94  
-  "Navigate back in the browser history."
95  
-  []
96  
-  (core/back *driver*))
  94
+  "Navigate back in the browser history, optionally `n` times."
  95
+  ([] (back 1))
  96
+  ([n]
  97
+     (dotimes [m n]
  98
+       (core/back *driver*))))
97 99
 
98 100
 (defn close
99 101
   "Close the browser. If multiple windows are open, this only closes the active window."
@@ -107,8 +109,10 @@
107 109
 
108 110
 (defn forward
109 111
   "Navigate forward in the browser history."
110  
-  []
111  
-  (core/forward *driver*))
  112
+  ([] (forward 1))
  113
+  ([n]
  114
+     (dotimes [m n]
  115
+       (core/forward *driver*))))
112 116
 
113 117
 (defn get-url
114 118
   "Navigate the browser to `url`."
@@ -184,32 +188,34 @@
184 188
   []
185 189
   (core/switch-to-active *driver*))
186 190
 
  191
+;; TODO: test coverage
187 192
 (defn add-cookie
188 193
   "Add the given `cookie` to the browser session."
189 194
   [cookie]
190 195
   (options/add-cookie *driver* cookie))
191 196
 
192  
-(defn delete-cookie-named
193  
-  "Delete the cookie with name `cookie-name` from the browser session."
194  
-  [cookie-name]
195  
-  (options/delete-cookie-named *driver* cookie-name))
196  
-
  197
+;; TODO: test coverage
197 198
 (defn delete-cookie
198  
-  "Delete the given `cookie` from the browser session."
199  
-  [cookie]
200  
-  (options/delete-cookie *driver* cookie))
  199
+  "Provided the name of a cookie or a Cookie record itself, delete it from the browser session."
  200
+  [name-or-obj]
  201
+  (if (string? name-or-obj)
  202
+    (options/delete-cookie-named *driver* name-or-obj)
  203
+    (options/delete-cookie *driver* name-or-obj)))
201 204
 
  205
+;; TODO: test coverage
202 206
 (defn delete-all-cookies
203 207
   "Delete all cookies from the browser session."
204 208
   []
205 209
   (options/delete-all-cookies *driver*))
206 210
 
  211
+;; TODO: test coverage
207 212
 (defn cookies
208 213
   "Return all cookies in the browser session."
209 214
   []
210 215
   (options/cookies *driver*))
211 216
 
212  
-(defn cookie-named
  217
+;; TODO: test coverage
  218
+(defn cookie
213 219
   "Return the cookie with name `cookie-name`."
214 220
   [cookie-name]
215 221
   (options/cookie-named *driver* cookie-name))
@@ -588,4 +594,7 @@
588 594
   [& query-action-maps]
589 595
   (doseq [entry query-action-maps
590 596
           [k v] entry]
591  
-    (quick-fill* k v true)))
  597
+    (quick-fill* k v true)))
  598
+
  599
+
  600
+;; IDEA: Go 2 pages back/forward

0 notes on commit c0fbacd

Please sign in to comment.
Something went wrong with that request. Please try again.