Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simplify find-* functions by allowing only one parameter attr-val on …

…top of the necessary driver
  • Loading branch information...
commit c4ceffd7bf3bf4cf64a3f5e16908e02d2b3b2fcb 1 parent 8bbb375
@semperos semperos authored
View
10 src/clj_webdriver/core.clj
@@ -100,12 +100,8 @@
(find-semantic-buttons-by-regex [driver attr-val] "Semantic buttons are things that look or behave like buttons but do not necessarily consist of a `<button>` tag")
(find-checkables-by-text [driver attr-val] "Finding the 'text' of a radio or checkbox is complex. Handle it here.")
(find-table-cells [driver attr-val] "Given a WebDriver `driver` and a vector `attr-val`, find the correct")
- (find-them
- [driver attr-val]
- [driver tag attr-val] "Find all elements that match the tag/attr-val query")
- (find-it
- [driver attr-val]
- [driver tag attr-val] "Call (first (find-them args))"))
+ (find-them [driver attr-val] "Find all elements that match the tag/attr-val query")
+ (find-it [driver attr-val] "Call (first (find-them args))"))
(defprotocol IElement
"Basic actions on elements"
@@ -121,7 +117,7 @@
(location [element] "Given an element object, return its location as a map of its x/y coordinates")
(location-once-visible [element] "Given an element object, return its location on the screen once it is scrolled into view as a map of its x/y coordinates. The window will scroll as much as possible until the element hits the top of the page; thus even visible elements will be scrolled until they reach that point.")
(present? [element] "Returns true if the element exists and is visible")
- (tag-name [element] "Retrieve the name of the HTML tag of the given element object")
+ (tag [element] "Retrieve the name of the HTML tag of the given element object (returned as a keyword)")
(text [element] "Retrieve the content, or inner HTML, of a given element object")
(value [element] "Retrieve the `value` attribute of the given element object")
(visible? [element] "Returns true if the given element object is visible/displayed")
View
12 src/clj_webdriver/core_by.clj
@@ -3,7 +3,7 @@
(defn by-id
"Used when finding elements. Returns `By/id` of `expr`"
[expr]
- (By/id expr))
+ (By/id (name expr)))
(defn by-link-text
"Used when finding elements. Returns `By/linkText` of `expr`"
@@ -18,12 +18,12 @@
(defn by-name
"Used when finding elements. Returns `By/name` of `expr`"
[expr]
- (By/name expr))
+ (By/name (name expr)))
-(defn by-tag-name
+(defn by-tag
"Used when finding elements. Returns `By/tagName` of `expr`"
[expr]
- (By/tagName expr))
+ (By/tagName (name expr)))
(defn by-xpath
"Used when finding elements. Returns `By/xpath` of `expr`"
@@ -33,7 +33,7 @@
(defn by-class-name
"Used when finding elements. Returns `By/className` of `expr`"
[expr]
- (By/className expr))
+ (By/className (name expr)))
(defn by-css-selector
"Used when finding elements. Returns `By/cssSelector` of `expr`"
@@ -51,7 +51,7 @@
(= :class attr) (by-class-name value)
(= :id attr) (by-id value)
(= :name attr) (by-name value)
- (= :tag attr) (by-tag-name value)
+ (= :tag attr) (by-tag value)
(= :text attr) (by-xpath (str "//"
(name tag)
"[text()"
View
73 src/clj_webdriver/core_driver.clj
@@ -183,7 +183,7 @@
(remove
#(let [[k v] %] (= java.util.regex.Pattern (class v)))
attr-val))
- elements (find-them driver tag attr-vals-without-regex)]
+ elements (find-them driver (assoc attr-vals-without-regex :tag tag))]
(filter-elements-by-regex elements attr-val))))
(find-windows [driver attr-val]
@@ -250,15 +250,14 @@
;; NoSuchElementException caught so that `exists?` will behave as expected
(find-them
([driver attr-val]
+ (when (keyword? driver) ; I keep forgetting to pass in the WebDriver instance while testing
+ (throw (IllegalArgumentException.
+ (str "The first parameter to find-them must be an instance of WebDriver."))))
(if (and (map? attr-val)
(empty? attr-val))
nil
(try
(cond
- (= attr-val :button*) (find-them driver :button* nil)
- (keyword? attr-val) (find-elements
- driver
- (by-tag-name (name attr-val))) ; supplied just :tag
(vector? attr-val) (cond
(some #{:row :col} attr-val) (find-table-cells driver attr-val)
(query-with-ancestry-has-regex? attr-val) (if (query-with-ancestry-has-regex? (drop-last 2 attr-val))
@@ -269,37 +268,30 @@
(find-elements driver (by-xpath (str (build-xpath-with-ancestry attr-val) "//*")))
(last attr-val)))
:else (find-elements driver (by-xpath (build-xpath-with-ancestry attr-val)))) ; supplied vector of queries in hierarchy
- (map? attr-val) (find-them driver :* attr-val))
- (catch org.openqa.selenium.NoSuchElementException e
- (lazy-seq (init-element nil))))))
- ([driver tag attr-val]
- (when (keyword? driver) ; I keep forgetting to pass in the WebDriver instance while testing
- (throw (IllegalArgumentException.
- (str "The first parameter to find-them must be an instance of WebDriver."))))
- (try
- (cond
+ (= (keys attr-val) '(:tag)) (find-elements
+ driver
+ (by-tag (:tag attr-val)))
+ (and (not (contains? attr-val :tag))
+ (not (contains? attr-val :xpath))
+ (not (contains? attr-val :css))) (find-them driver (assoc attr-val :tag :*))
(and (> (count attr-val) 1)
- (contains? attr-val :xpath)) (find-them driver :* {:xpath (:xpath attr-val)})
+ (contains? attr-val :xpath)) (find-them driver {:xpath (:xpath attr-val)})
(and (> (count attr-val) 1)
- (contains? attr-val :css)) (find-them driver :* {:css (:css attr-val)})
- (contains? attr-val :tag-name) (find-them driver
- (-> (:tag-name attr-val)
- .toLowerCase
- keyword)
- (dissoc attr-val :tag-name))
- (contains? attr-val :index) (find-elements driver (by-xpath (build-xpath tag attr-val)))
- (= tag :radio) (find-them driver :input (assoc attr-val :type "radio"))
- (= tag :checkbox) (find-them driver :input (assoc attr-val :type "checkbox"))
- (= tag :textfield) (find-them driver :input (assoc attr-val :type "text"))
- (= tag :password) (find-them driver :input (assoc attr-val :type "password"))
- (= tag :filefield) (find-them driver :input (assoc attr-val :type "file"))
- (and (= tag :input)
+ (contains? attr-val :css)) (find-them driver {:css (:css attr-val)})
+
+ (contains? attr-val :index) (find-elements driver (by-xpath (build-xpath (:tag attr-val) attr-val)))
+ (= (:tag attr-val) :radio) (find-them driver (assoc attr-val :tag :input :type "radio"))
+ (= (:tag attr-val) :checkbox) (find-them driver (assoc attr-val :tag :input :type "checkbox"))
+ (= (:tag attr-val) :textfield) (find-them driver (assoc attr-val :tag :input :type "text"))
+ (= (:tag attr-val) :password) (find-them driver (assoc attr-val :tag :input :type "password"))
+ (= (:tag attr-val) :filefield) (find-them driver (assoc attr-val :tag :input :type "file"))
+ (and (= (:tag attr-val) :input)
(contains? attr-val :type)
(or (= "radio" (:type attr-val))
(= "checkbox" (:type attr-val)))
(or (contains? attr-val :text)
(contains? attr-val :label))) (find-checkables-by-text driver attr-val)
- (= tag :button*) (if (contains-regex? attr-val)
+ (= (:tag attr-val) :button*) (if (contains-regex? attr-val)
(find-semantic-buttons-by-regex driver attr-val)
(find-semantic-buttons driver attr-val))
(= 1 (count attr-val)) (let [entry (first attr-val)
@@ -308,12 +300,12 @@
(cond
(= :xpath attr) (find-elements driver (by-xpath value))
(= :css attr) (find-elements driver (by-css-selector value))
- (= java.util.regex.Pattern (class value)) (find-elements-by-regex-alone driver tag attr-val)
- :else (find-elements driver (by-attr= tag attr value))))
- (contains-regex? attr-val) (find-elements-by-regex driver tag attr-val)
- :else (find-elements driver (by-xpath (build-xpath tag attr-val))))
- (catch org.openqa.selenium.NoSuchElementException e
- (lazy-seq (init-element nil))))))
+ (= java.util.regex.Pattern (class value)) (find-elements-by-regex-alone driver (:tag attr-val) attr-val)
+ :else (find-elements driver (by-attr= (:tag attr-val) attr value))))
+ (contains-regex? attr-val) (find-elements-by-regex driver (:tag attr-val) attr-val)
+ :else (find-elements driver (by-xpath (build-xpath (:tag attr-val) attr-val))))
+ (catch org.openqa.selenium.NoSuchElementException e
+ (lazy-seq (init-element nil)))))))
(find-it
([driver attr-val]
@@ -327,17 +319,6 @@
(do
(cache/insert driver attr-val el)
el)
- el))))
- ([driver tag attr-val]
- (if (and (cache/cache-enabled? driver) (cache/in-cache? driver [tag attr-val]))
- (cache/retrieve driver [tag attr-val])
- (let [el (first (find-them driver tag attr-val))]
- (if (and (cache/cache-enabled? driver)
- (exists? el)
- (cache/cacheable? driver el))
- (do
- (cache/insert driver [tag attr-val] el)
- el)
el))))))
View
9 src/clj_webdriver/util.clj
@@ -22,10 +22,11 @@
"Given a tag and a map of attribute-value pairs, generate XPath"
[tag attr-val]
(when-not (contains-regex? attr-val)
- (str "//"
- (name tag)
- (when-not (empty? attr-val)
- (build-xpath-attrs attr-val)))))
+ (let [attr-val (dissoc attr-val :tag)]
+ (str "//"
+ (name tag)
+ (when-not (empty? attr-val)
+ (build-xpath-attrs attr-val))))))
(defn build-xpath-with-ancestry
"Given a vector of queries in hierarchical order, create XPath.
Please sign in to comment.
Something went wrong with that request. Please try again.