forked from semperos/clj-webdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_by.clj
117 lines (102 loc) · 4.13 KB
/
core_by.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
;; ## Core by-* Functions ##
;;
;; These functions are low-level equivalents for the
;; `ByFoo` classes that make up the Java API, with a few
;; notable exceptions that provide more flexible matching
;; (see the `by-attr*` functions at the bottom)
(in-ns 'clj-webdriver.core)
(defn by-id
"Used when finding elements. Returns `By/id` of `expr`"
[expr]
(By/id (name expr)))
(defn by-link-text
"Used when finding elements. Returns `By/linkText` of `expr`"
[expr]
(By/linkText expr))
(defn by-partial-link-text
"Used when finding elements. Returns `By/partialLinkText` of `expr`"
[expr]
(By/partialLinkText expr))
(defn by-name
"Used when finding elements. Returns `By/name` of `expr`"
[expr]
(By/name (name expr)))
(defn by-tag
"Used when finding elements. Returns `By/tagName` of `expr`"
[expr]
(By/tagName (name expr)))
(defn by-xpath
"Used when finding elements. Returns `By/xpath` of `expr`"
[expr]
(By/xpath expr))
(defn by-css
"Used when finding elements. Returns `By/cssSelector` of `expr`"
[expr]
(By/cssSelector expr))
(def by-css-selector by-css)
(defn by-query
"Given a map with either an `:xpath` or `:css` key, return the respective by-* function (`by-xpath` or `by-css`) using the value for that key."
[{:keys [xpath css] :as m}]
(cond
xpath (by-xpath (:xpath m))
css (by-css (:css m))
:else (throw (IllegalArgumentException. "You must provide either an `:xpath` or `:css` entry."))))
(defn by-class-name
"Used when finding elements. Returns `By/className` of `expr`"
[expr]
(let [expr (str expr)]
(if (re-find #"\s" expr)
(let [classes (string/split expr #"\s+")
class-query (string/join "." classes)]
(by-css (str "*." class-query)))
(By/className (name expr)))))
;; Inspired by the `attr=`, `attr-contains` in Christophe Grand's enlive
(defn by-attr=
"Use `value` of arbitrary attribute `attr` to find an element. You can optionally specify the tag.
For example: `(by-attr= :id \"element-id\")`
`(by-attr= :div :class \"content\")`"
([attr value] (by-attr= :* attr value)) ; default to * any tag
([tag attr value]
(cond
(= :class attr) (if (re-find #"\s" value)
(let [classes (string/split value #"\s+")
class-query (string/join "." classes)]
(by-css (str (name tag) class-query)))
(by-class-name value))
(= :id attr) (by-id value)
(= :name attr) (by-name value)
(= :tag attr) (by-tag value)
(= :text attr) (if (= tag :a)
(by-link-text value)
(by-xpath (str "//"
(name tag)
"[text()"
"=\"" value "\"]")))
:else (by-css (str (name tag)
"[" (name attr) "='" value "']")))))
(defn by-attr-contains
"Match if `value` is contained in the value of `attr`. You can optionally specify the tag.
For example: `(by-attr-contains :class \"navigation\")`
`(by-attr-contains :ul :class \"tags\")`"
([attr value] (by-attr-contains :* attr value)) ; default to * any tag
([tag attr value]
(by-css (str (name tag)
"[" (name attr) "*='" value "']"))))
(defn by-attr-starts
"Match if `value` is at the beginning of the value of `attr`. You can optionally specify the tag."
([attr value] (by-attr-starts :* attr value))
([tag attr value]
(by-css (str (name tag)
"[" (name attr) "^='" value "']"))))
(defn by-attr-ends
"Match if `value` is at the end of the value of `attr`. You can optionally specify the tag."
([attr value] (by-attr-ends :* attr value))
([tag attr value]
(by-css (str (name tag)
"[" (name attr) "$='" value "']"))))
(defn by-has-attr
"Match if the element has the attribute `attr`, regardless of its value. You can optionally specify the tag."
([attr] (by-has-attr :* attr))
([tag attr]
(by-css (str (name tag)
"[" (name attr) "]"))))