This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
core_by.clj
99 lines (87 loc) · 3.64 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
(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-class-name
"Used when finding elements. Returns `By/className` of `expr`"
[expr]
(let [expr (str expr)]
(if (re-find #"\s" expr)
(by-xpath (str "//*"
"[@class='"
expr
"']"))
(By/className (name expr)))))
(defn by-css-selector
"Used when finding elements. Returns `By/cssSelector` of `expr`"
[expr]
(By/cssSelector 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-not (re-find #"\s" value)
(by-class-name value)
(by-xpath (str "//"
(name tag)
"[@class='"
value
"']")))
(= :id attr) (by-id value)
(= :name attr) (by-name value)
(= :tag attr) (by-tag value)
(= :text attr) (by-xpath (str "//"
(name tag)
"[text()"
"=\"" value "\"]"))
:else (by-xpath (str "//" ; anywhere in DOM
(name tag) ; tag from kw
"[@" (name attr) ; attr from kw
"='" value "']"))))) ; ="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-xpath (str "//" ; anywhere in DOM
(name tag) ; tag from kw
"[contains(@" ; xpath "contains" function
(name attr) ; attr from kw
",'" value "')]")))) ; ,'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-xpath (str "//" ; anywhere in DOM
(name tag) ; tag from kw
"[starts-with(@" ; xpath "starts-with" function
(name attr) ; attr from kw
",'" value "')]")))) ; ,'value')]
;; I can't add more functions like `by-attr-ends` or `by-attr-matches` (regex) due
;; to lack of uniform XPath support in WebDriver