Skip to content
Browse files

Reformat cond statements to be (1) more readable and (2) properly ind…

…ented
  • Loading branch information...
1 parent 40f72b2 commit e9260ca51a4c534823eedb8719d3240c0e9ce67b @semperos committed Feb 10, 2013
Showing with 65 additions and 46 deletions.
  1. +53 −34 src/clj_webdriver/core_driver.clj
  2. +12 −12 src/clj_webdriver/core_element.clj
View
87 src/clj_webdriver/core_driver.clj
@@ -27,12 +27,15 @@
(let [this-handle (window-handle* (:webdriver driver))
idx (.indexOf handles this-handle)]
(cond
- (zero? idx) (do ; if first window, switch to next
- (.close (:webdriver driver))
- (switch-to-window driver (nth handles (inc idx))))
- :else (do ; otherwise, switch back one window
- (.close (:webdriver driver))
- (switch-to-window driver (nth handles (dec idx)))))
+ (zero? idx)
+ (do ; if first window, switch to next
+ (.close (:webdriver driver))
+ (switch-to-window driver (nth handles (inc idx))))
+
+ :else
+ (do ; otherwise, switch back one window
+ (.close (:webdriver driver))
+ (switch-to-window driver (nth handles (dec idx)))))
(cache/seed driver {}))
(do
(.close (:webdriver driver))
@@ -125,19 +128,28 @@
(switch-to-window [driver window]
(cond
- (string? window) (do
- (.window (.switchTo (:webdriver driver)) window)
- driver)
- (win/window? window) (do
- (.window (.switchTo (:driver window)) (:handle window))
- driver)
- (number? window) (do
- (switch-to-window driver (nth (windows driver) window))
- driver)
- (nil? window) (throw (RuntimeException. "No window can be found"))
- :else (do
- (.window (.switchTo (:webdriver driver)) window)
- driver)))
+ (string? window)
+ (do
+ (.window (.switchTo (:webdriver driver)) window)
+ driver)
+
+ (win/window? window)
+ (do
+ (.window (.switchTo (:driver window)) (:handle window))
+ driver)
+
+ (number? window)
+ (do
+ (switch-to-window driver (nth (windows driver) window))
+ driver)
+
+ (nil? window)
+ (throw (RuntimeException. "No window can be found"))
+
+ :else
+ (do
+ (.window (.switchTo (:webdriver driver)) window)
+ driver)))
(switch-to-other-window [driver]
(if (not= (count (windows driver)) 2)
@@ -158,23 +170,23 @@
(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)
driver)
-
+
(delete-cookie [driver cookie-spec]
(.deleteCookie (.manage (:webdriver driver)) (:cookie (init-cookie cookie-spec)))
driver)
-
+
(delete-all-cookies [driver]
(.deleteAllCookies (.manage (:webdriver driver)))
driver)
-
+
(cookies [driver]
(set (map #(init-cookie {:cookie %})
(.getCookies (.manage (:webdriver driver))))))
-
+
(cookie-named [driver cookie-name]
(let [cookie-obj (.getCookieNamed (.manage (:webdriver driver)) cookie-name)]
(init-cookie {:cookie cookie-obj})))
@@ -186,7 +198,7 @@
(alert-obj [driver]
(-> driver :webdriver .switchTo .alert))
-
+
(alert-text [driver]
(-> driver :webdriver .switchTo .alert .getText))
@@ -196,7 +208,7 @@
(dismiss [driver]
(-> driver :webdriver .switchTo .alert .dismiss))
-
+
;; Find Functions
IFind
(find-element-by [driver by-value]
@@ -307,7 +319,7 @@
([driver element]
(let [act (:actions driver)]
(.perform (.doubleClick act (:webelement element))))))
-
+
(drag-and-drop
[driver element-a element-b]
(let [act (:actions driver)]
@@ -436,11 +448,18 @@
(cond
;; Accept by-clauses
(not (or (vector? attr-val)
- (map? attr-val))) (find-elements-by driver attr-val)
- ;; Accept vectors for hierarchical queries
- (vector? attr-val) (find-by-hierarchy driver attr-val)
- ;; Build XPath dynamically
- :else (find-elements-by driver (by-query (build-query attr-val))))
+ (map? attr-val)))
+ (find-elements-by driver attr-val)
+
+ ;; Accept vectors for hierarchical queries
+ (vector? attr-val)
+ (find-by-hierarchy driver attr-val)
+
+ ;; Build XPath dynamically
+ :else
+ (find-elements-by driver (by-query (build-query attr-val))))
(catch org.openqa.selenium.NoSuchElementException e
- ;; NoSuchElementException caught here, so we can have functions like `exist?`
- (lazy-seq [(init-element nil)])))))
+ ;; NoSuchElementException caught here to mimic Clojure behavior like
+ ;; (get {:foo "bar"} :baz) since the page can be thought of as a kind of associative
+ ;; data structure with unique selectors as keys and HTML elements as values
+ (lazy-seq nil)))))
View
24 src/clj_webdriver/core_element.clj
@@ -37,12 +37,12 @@
(when (= webdriver-result "true")
attr)
webdriver-result))))
-
+
(click [element]
(.click (:webelement element))
(cache/set-status :check)
nil)
-
+
(css-value [element property]
(.getCssValue (:webelement element) property))
@@ -99,13 +99,13 @@
(let [rect-a (rectangle element-a)
rect-b (rectangle element-b)]
(.intersects rect-a rect-b)))
-
+
(tag [element]
(.getTagName (:webelement element)))
(text [element]
(.getText (:webelement element)))
-
+
(value [element]
(.getAttribute (:webelement element) "value"))
@@ -114,42 +114,42 @@
(xpath [element]
(browserbot (.getWrappedDriver (:webelement element)) "getXPath" (:webelement element) []))
-
+
IFormElement
(deselect [element]
(if (.isSelected (:webelement element))
(toggle (:webelement element))
element))
-
+
(enabled? [element]
(.isEnabled (:webelement element)))
-
+
(input-text [element s]
(.sendKeys (:webelement element) (into-array CharSequence (list s)))
element)
-
+
(submit [element]
(.submit (:webelement element))
(cache/set-status :flush)
nil)
-
+
(clear [element]
(.clear (:webelement element))
element)
-
+
(select [element]
(if-not (.isSelected (:webelement element))
(.click (:webelement element))
element))
-
+
(selected? [element]
(.isSelected (:webelement element)))
(send-keys [element s]
(.sendKeys (:webelement element) (into-array CharSequence (list s)))
element)
-
+
(toggle [element]
(.click (:webelement element))
element)

0 comments on commit e9260ca

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