~ubuntu-branches/ubuntu/wily/ruby-xpath/wily-proposed

« back to all changes in this revision

Viewing changes to lib/xpath/html.rb

  • Committer: Package Import Robot
  • Author(s): Markus Tornow
  • Date: 2012-12-08 22:57:18 UTC
  • Revision ID: package-import@ubuntu.com-20121208225718-6n9jpw8w8la9x2hr
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module XPath
 
2
  module HTML
 
3
    include XPath
 
4
    extend self
 
5
 
 
6
    # Match an `a` link element.
 
7
    #
 
8
    # @param [String] locator
 
9
    #   Text, id, title, or image alt attribute of the link
 
10
    #
 
11
    def link(locator)
 
12
      locator = locator.to_s
 
13
      link = descendant(:a)[attr(:href)]
 
14
      link[attr(:id).equals(locator) | string.n.contains(locator) | attr(:title).contains(locator) | descendant(:img)[attr(:alt).contains(locator)]]
 
15
    end
 
16
 
 
17
    # Match a `submit`, `image`, or `button` element.
 
18
    #
 
19
    # @param [String] locator
 
20
    #   Value, title, id, or image alt attribute of the button
 
21
    #
 
22
    def button(locator)
 
23
      locator = locator.to_s
 
24
      button = descendant(:input)[~attr(:disabled)][attr(:type).one_of('submit', 'reset', 'image', 'button')][attr(:id).equals(locator) | attr(:value).contains(locator) | attr(:title).contains(locator)]
 
25
      button += descendant(:button)[~attr(:disabled)][attr(:id).equals(locator) | attr(:value).contains(locator) | string.n.contains(locator) | attr(:title).contains(locator)]
 
26
      button += descendant(:input)[~attr(:disabled)][attr(:type).equals('image')][attr(:alt).contains(locator)]
 
27
    end
 
28
 
 
29
 
 
30
    # Match anything returned by either {#link} or {#button}.
 
31
    #
 
32
    # @param [String] locator
 
33
    #   Text, id, title, or image alt attribute of the link or button
 
34
    #
 
35
    def link_or_button(locator)
 
36
      link(locator) + button(locator)
 
37
    end
 
38
 
 
39
 
 
40
    # Match any `fieldset` element.
 
41
    #
 
42
    # @param [String] locator
 
43
    #   Legend or id of the fieldset
 
44
    #
 
45
    def fieldset(locator)
 
46
      locator = locator.to_s
 
47
      descendant(:fieldset)[attr(:id).equals(locator) | child(:legend)[string.n.contains(locator)]]
 
48
    end
 
49
 
 
50
 
 
51
    # Match any `input`, `textarea`, or `select` element that doesn't have a
 
52
    # type of `submit`, `image`, or `hidden`.
 
53
    #
 
54
    # @param [String] locator
 
55
    #   Label, id, or name of field to match
 
56
    #
 
57
    def field(locator)
 
58
      locator = locator.to_s
 
59
      xpath = descendant(:input, :textarea, :select)[~attr(:type).one_of('submit', 'image', 'hidden')]
 
60
      xpath = locate_field(xpath, locator)
 
61
      xpath
 
62
    end
 
63
 
 
64
 
 
65
    # Match any `input` or `textarea` element that can be filled with text.
 
66
    # This excludes any inputs with a type of `submit`, `image`, `radio`,
 
67
    # `checkbox`, `hidden`, or `file`.
 
68
    #
 
69
    # @param [String] locator
 
70
    #   Label, id, or name of field to match
 
71
    #
 
72
    def fillable_field(locator)
 
73
      locator = locator.to_s
 
74
      xpath = descendant(:input, :textarea)[~attr(:type).one_of('submit', 'image', 'radio', 'checkbox', 'hidden', 'file')]
 
75
      xpath = locate_field(xpath, locator)
 
76
      xpath
 
77
    end
 
78
 
 
79
 
 
80
    # Match any `select` element.
 
81
    #
 
82
    # @param [String] locator
 
83
    #   Label, id, or name of the field to match
 
84
    #
 
85
    def select(locator)
 
86
      locator = locator.to_s
 
87
      locate_field(descendant(:select), locator)
 
88
    end
 
89
 
 
90
 
 
91
    # Match any `input` element of type `checkbox`.
 
92
    #
 
93
    # @param [String] locator
 
94
    #   Label, id, or name of the checkbox to match
 
95
    #
 
96
    def checkbox(locator)
 
97
      locator = locator.to_s
 
98
      locate_field(descendant(:input)[attr(:type).equals('checkbox')], locator)
 
99
    end
 
100
 
 
101
 
 
102
    # Match any `input` element of type `radio`.
 
103
    #
 
104
    # @param [String] locator
 
105
    #   Label, id, or name of the radio button to match
 
106
    #
 
107
    def radio_button(locator)
 
108
      locator = locator.to_s
 
109
      locate_field(descendant(:input)[attr(:type).equals('radio')], locator)
 
110
    end
 
111
 
 
112
 
 
113
    # Match any `input` element of type `file`.
 
114
    #
 
115
    # @param [String] locator
 
116
    #   Label, id, or name of the file field to match
 
117
    #
 
118
    def file_field(locator)
 
119
      locator = locator.to_s
 
120
      locate_field(descendant(:input)[attr(:type).equals('file')], locator)
 
121
    end
 
122
 
 
123
 
 
124
    # Match an `optgroup` element.
 
125
    #
 
126
    # @param [String] name
 
127
    #   Label for the option group
 
128
    #
 
129
    def optgroup(locator)
 
130
      locator = locator.to_s
 
131
      descendant(:optgroup)[attr(:label).contains(locator)]
 
132
    end
 
133
 
 
134
 
 
135
    # Match an `option` element.
 
136
    #
 
137
    # @param [String] name
 
138
    #   Visible text of the option
 
139
    #
 
140
    def option(locator)
 
141
      locator = locator.to_s
 
142
      descendant(:option)[string.n.equals(locator)]
 
143
    end
 
144
 
 
145
 
 
146
    # Match any `table` element.
 
147
    #
 
148
    # @param [String] locator
 
149
    #   Caption or id of the table to match
 
150
    # @option options [Array] :rows
 
151
    #   Content of each cell in each row to match
 
152
    #
 
153
    def table(locator)
 
154
      locator = locator.to_s
 
155
      descendant(:table)[attr(:id).equals(locator) | descendant(:caption).contains(locator)]
 
156
    end
 
157
 
 
158
    # Match any 'dd' element.
 
159
    #
 
160
    # @param [String] locator
 
161
    #   Id of the 'dd' element or text from preciding 'dt' element content
 
162
    def definition_description(locator)
 
163
      locator = locator.to_s
 
164
      descendant(:dd)[attr(:id).equals(locator) | previous_sibling(:dt)[string.n.equals(locator)] ]
 
165
    end
 
166
 
 
167
  protected
 
168
 
 
169
    def locate_field(xpath, locator)
 
170
      locate_field = xpath[attr(:id).equals(locator) | attr(:name).equals(locator) | attr(:placeholder).equals(locator) | attr(:id).equals(anywhere(:label)[string.n.contains(locator)].attr(:for))]
 
171
      locate_field += descendant(:label)[string.n.contains(locator)].descendant(xpath)
 
172
      locate_field[~attr(:disabled)]
 
173
    end
 
174
  end
 
175
end