Browse files

Make Element implement IFn, so (an-element :attr) returns value of th…

…at attr
  • Loading branch information...
1 parent 7241522 commit cce35760e1be772e138e086a70cd2fb449c5d7dc @semperos committed Apr 9, 2013
Showing with 63 additions and 22 deletions.
  1. +2 −17 src/clj_webdriver/core_element.clj
  2. +31 −2 src/clj_webdriver/element.clj
  3. +30 −3 src/clj_webdriver/util.clj
View
19 src/clj_webdriver/core_element.clj
@@ -21,22 +21,7 @@
;; Element action basics
IElement
(attribute [element attr]
- (if (= attr :text)
- (text element)
- (let [attr (name attr)
- boolean-attrs ["async", "autofocus", "autoplay", "checked", "compact", "complete",
- "controls", "declare", "defaultchecked", "defaultselected", "defer",
- "disabled", "draggable", "ended", "formnovalidate", "hidden",
- "indeterminate", "iscontenteditable", "ismap", "itemscope", "loop",
- "multiple", "muted", "nohref", "noresize", "noshade", "novalidate",
- "nowrap", "open", "paused", "pubdate", "readonly", "required",
- "reversed", "scoped", "seamless", "seeking", "selected", "spellcheck",
- "truespeed", "willvalidate"]
- webdriver-result (.getAttribute (:webelement element) (name attr))]
- (if (some #{attr} boolean-attrs)
- (when (= webdriver-result "true")
- attr)
- webdriver-result))))
+ (element-attribute element attr))
(click [element]
(.click (:webelement element))
@@ -104,7 +89,7 @@
(.getTagName (:webelement element)))
(text [element]
- (.getText (:webelement element)))
+ (element-text element))
(value [element]
(.getAttribute (:webelement element) "value"))
View
33 src/clj_webdriver/element.clj
@@ -1,6 +1,35 @@
-(ns clj-webdriver.element)
+(ns clj-webdriver.element
+ (:require [clj-webdriver.util :as util]))
-(defrecord Element [webelement])
+(defn- element-attributes
+ [el & attributes]
+ (map #(util/element-attribute el %1) attributes))
+
+(defrecord Element [webelement]
+ clojure.lang.IFn
+ (invoke [self prop-name] (util/element-attribute self prop-name))
+ (invoke [self prop-1 prop-2] (element-attributes self prop-1 prop-2))
+ (invoke [self prop-1 prop-2 prop-3] (element-attributes self prop-1 prop-2 prop-3))
+ (invoke [self prop-1 prop-2 prop-3 prop-4] (element-attributes self prop-1 prop-2 prop-3 prop-4))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14 prop-15] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14 prop-15))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14 prop-15 prop-16] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14 prop-15 prop-16))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14 prop-15 prop-16 prop-17] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14 prop-15 prop-16 prop-17))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14 prop-15 prop-16 prop-17 prop-18] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14 prop-15 prop-16 prop-17 prop-18))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14 prop-15 prop-16 prop-17 prop-18 prop-19] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14 prop-15 prop-16 prop-17 prop-18 prop-19))
+ (invoke [self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14 prop-15 prop-16 prop-17 prop-18 prop-19 prop-20] (element-attributes self prop-1 prop-2 prop-3 prop-4 prop-5 prop-6 prop-7 prop-8 prop-9 prop-10 prop-11 prop-12 prop-13 prop-14 prop-15 prop-16 prop-17 prop-18 prop-19 prop-20))
+ (applyTo [self prop-names]
+ (for [prop-name prop-names]
+ (util/element-attribute self prop-name))))
(defn init-element
"Initialize an Element record"
View
33 src/clj_webdriver/util.clj
@@ -2,7 +2,7 @@
(:require [clojure.string :as str]
[clojure.java.io :as io]
[clojure.walk :as walk]
- [clj-webdriver.cache :as cache]
+ ;; [clj-webdriver.cache :as cache]
clj-webdriver.driver)
(:import clj_webdriver.driver.Driver
[org.openqa.selenium WebDriver WebElement NoSuchElementException]
@@ -236,7 +236,7 @@
;; isn't interrupted at every page load with exceptions due to
;; missing WebElement's from the WebDriver's default cache
(defmethod print-method clj_webdriver.driver.Driver [r, ^Writer w]
- (cache/check-status r)
+ ;; (cache/check-status r)
(print-meta r w)
(.write w "#")
(.write w (.getName (class r)))
@@ -340,4 +340,31 @@
(defn throw-nse
([] (throw-nse ""))
([msg]
- (throw (NoSuchElementException. (str msg "\n" "When an element cannot be found in clj-webdriver, nil is returned. You've just tried to perform an action on an element that returned as nil for the search query you used. Please verify the query used to locate this element; it is not on the current page.")))))
+ (throw (NoSuchElementException. (str msg "\n" "When an element cannot be found in clj-webdriver, nil is returned. You've just tried to perform an action on an element that returned as nil for the search query you used. Please verify the query used to locate this element; it is not on the current page.")))))
+
+;;
+;; These are definitions needed across multiple files
+;;
+
+(defn element-text
+ [element]
+ (.getText (:webelement element)))
+
+(defn element-attribute
+ [element attr]
+ (if (= attr :text)
+ (element-text element)
+ (let [attr (name attr)
+ boolean-attrs ["async", "autofocus", "autoplay", "checked", "compact", "complete",
+ "controls", "declare", "defaultchecked", "defaultselected", "defer",
+ "disabled", "draggable", "ended", "formnovalidate", "hidden",
+ "indeterminate", "iscontenteditable", "ismap", "itemscope", "loop",
+ "multiple", "muted", "nohref", "noresize", "noshade", "novalidate",
+ "nowrap", "open", "paused", "pubdate", "readonly", "required",
+ "reversed", "scoped", "seamless", "seeking", "selected", "spellcheck",
+ "truespeed", "willvalidate"]
+ webdriver-result (.getAttribute (:webelement element) (name attr))]
+ (if (some #{attr} boolean-attrs)
+ (when (= webdriver-result "true")
+ attr)
+ webdriver-result))))

0 comments on commit cce3576

Please sign in to comment.