watir 6.15.1 → 6.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -2
  3. data/.travis.yml +2 -0
  4. data/CHANGES.md +13 -0
  5. data/Rakefile +6 -0
  6. data/lib/watir.rb +1 -0
  7. data/lib/watir/browser.rb +4 -1
  8. data/lib/watir/element_collection.rb +27 -17
  9. data/lib/watir/elements/element.rb +41 -14
  10. data/lib/watir/elements/iframe.rb +3 -1
  11. data/lib/watir/elements/radio.rb +7 -2
  12. data/lib/watir/elements/select.rb +1 -0
  13. data/lib/watir/locators.rb +21 -21
  14. data/lib/watir/locators/button/matcher.rb +40 -0
  15. data/lib/watir/locators/cell/selector_builder.rb +3 -0
  16. data/lib/watir/locators/element/locator.rb +29 -172
  17. data/lib/watir/locators/element/matcher.rb +127 -0
  18. data/lib/watir/locators/element/selector_builder.rb +69 -23
  19. data/lib/watir/locators/element/selector_builder/xpath.rb +3 -10
  20. data/lib/watir/locators/row/selector_builder.rb +5 -5
  21. data/lib/watir/locators/text_area/selector_builder.rb +0 -14
  22. data/lib/watir/locators/text_area/selector_builder/xpath.rb +2 -2
  23. data/lib/watir/locators/text_field/matcher.rb +38 -0
  24. data/lib/watir/radio_set.rb +28 -31
  25. data/lib/watir/scroll.rb +69 -0
  26. data/lib/watir/version.rb +1 -1
  27. data/spec/locator_spec_helper.rb +58 -14
  28. data/spec/unit/element_locator_spec.rb +46 -591
  29. data/spec/unit/match_elements/button_spec.rb +80 -0
  30. data/spec/unit/match_elements/element_spec.rb +368 -0
  31. data/spec/unit/match_elements/text_field_spec.rb +79 -0
  32. data/spec/unit/selector_builder/anchor_spec.rb +51 -0
  33. data/spec/unit/selector_builder/button_spec.rb +206 -0
  34. data/spec/unit/selector_builder/cell_spec.rb +63 -0
  35. data/spec/unit/selector_builder/element_spec.rb +744 -0
  36. data/spec/unit/selector_builder/row_spec.rb +111 -0
  37. data/spec/unit/selector_builder/text_field_spec.rb +189 -0
  38. data/spec/unit/selector_builder/textarea_spec.rb +25 -0
  39. data/spec/watirspec/browser_spec.rb +7 -8
  40. data/spec/watirspec/element_hidden_spec.rb +1 -2
  41. data/spec/watirspec/elements/element_spec.rb +52 -16
  42. data/spec/watirspec/elements/iframe_spec.rb +1 -1
  43. data/spec/watirspec/elements/select_list_spec.rb +1 -1
  44. data/spec/watirspec/html/obscured.html +3 -1
  45. data/spec/watirspec/html/scroll.html +32 -0
  46. data/spec/watirspec/relaxed_locate_spec.rb +6 -1
  47. data/spec/watirspec/scroll_spec.rb +106 -0
  48. data/spec/watirspec/support/rspec_matchers.rb +2 -0
  49. data/spec/watirspec/wait_spec.rb +1 -1
  50. data/watir.gemspec +2 -4
  51. metadata +36 -33
  52. data/lib/watir/locators/button/locator.rb +0 -32
  53. data/lib/watir/locators/button/validator.rb +0 -17
  54. data/lib/watir/locators/cell/locator.rb +0 -13
  55. data/lib/watir/locators/element/validator.rb +0 -11
  56. data/lib/watir/locators/row/locator.rb +0 -13
  57. data/lib/watir/locators/text_field/locator.rb +0 -31
  58. data/lib/watir/locators/text_field/validator.rb +0 -13
  59. data/spec/unit/anchor_locator_spec.rb +0 -68
  60. data/spec/watirspec/selector_builder/button_spec.rb +0 -250
  61. data/spec/watirspec/selector_builder/cell_spec.rb +0 -92
  62. data/spec/watirspec/selector_builder/element_spec.rb +0 -628
  63. data/spec/watirspec/selector_builder/row_spec.rb +0 -148
  64. data/spec/watirspec/selector_builder/text_spec.rb +0 -199
@@ -0,0 +1,80 @@
1
+ require_relative '../unit_helper'
2
+
3
+ describe Watir::Locators::Button::Matcher do
4
+ include LocatorSpecHelper
5
+
6
+ let(:query_scope) { @query_scope || double(Watir::Browser) }
7
+ let(:matcher) { described_class.new(query_scope, @selector) }
8
+
9
+ describe '#match' do
10
+ it 'value attribute matches value' do
11
+ elements = [wd_element(text: 'foo', attributes: {value: 'foo'}),
12
+ wd_element(text: 'bar', attributes: {value: 'bar'}),
13
+ wd_element(text: '', attributes: {value: 'foobar'})]
14
+ values_to_match = {value: 'foobar'}
15
+
16
+ expect(matcher.match(elements, values_to_match, :all)).to eq [elements[2]]
17
+ end
18
+
19
+ it 'value attribute matches text' do
20
+ elements = [wd_element(text: 'foo', attributes: {value: 'foo'}),
21
+ wd_element(text: 'bar', attributes: {value: 'bar'}),
22
+ wd_element(text: 'foobar')]
23
+ values_to_match = {value: 'foobar'}
24
+
25
+ expect(elements[2]).not_to receive(:attribute)
26
+
27
+ expect {
28
+ expect(matcher.match(elements, values_to_match, :all)).to eq [elements[2]]
29
+ }.to have_deprecated_value_button
30
+ end
31
+
32
+ it 'returns empty array if neither value nor text match' do
33
+ elements = [wd_element(text: 'foo', attributes: {value: 'foo'}),
34
+ wd_element(text: 'bar', attributes: {value: 'bar'}),
35
+ wd_element(text: '', attributes: {value: 'foobar'})]
36
+ values_to_match = {value: 'nope'}
37
+
38
+ expect(matcher.match(elements, values_to_match, :all)).to eq []
39
+ end
40
+
41
+ it 'does not evaluate other parameters if value locator is not satisfied' do
42
+ elements = [wd_element(text: 'foo', attributes: {value: 'foo'}),
43
+ wd_element(text: 'bar', attributes: {value: 'bar'}),
44
+ wd_element(text: '', attributes: {value: 'foobar'})]
45
+ values_to_match = {value: 'nope', visible: true}
46
+ [0..2].each { |idx| expect(elements[idx]).not_to receive(:displayed?) }
47
+
48
+ expect(matcher.match(elements, values_to_match, :all)).to eq []
49
+ end
50
+
51
+ it 'does not calculate value if not passed in' do
52
+ elements = [wd_element(displayed?: true, text: 'foo', attributes: {value: 'foo'}),
53
+ wd_element(displayed?: true, text: 'bar', attributes: {value: 'bar'})]
54
+ values_to_match = {visible: false}
55
+
56
+ expect(elements[0]).not_to receive(:text)
57
+ expect(elements[0]).not_to receive(:text)
58
+ expect(elements[1]).not_to receive(:attribute)
59
+ expect(elements[1]).not_to receive(:attribute)
60
+
61
+ expect(matcher.match(elements, values_to_match, :all)).to eq []
62
+ end
63
+
64
+ it 'returns empty array if element is not an input or button element' do
65
+ elements = [wd_element(tag_name: 'wrong', text: 'foob', attributes: {value: 'foo'}),
66
+ wd_element(tag_name: 'wrong', text: 'bar', attributes: {value: 'bar'})]
67
+ values_to_match = {tag_name: 'button', value: 'foo'}
68
+
69
+ expect(matcher.match(elements, values_to_match, :all)).to eq []
70
+ end
71
+
72
+ it 'returns empty array if element is an input element with wrong type' do
73
+ elements = [wd_element(tag_name: 'input', text: 'foob', attributes: {value: 'foo', type: 'radio'}),
74
+ wd_element(tag_name: 'input', text: 'bar', attributes: {value: 'bar', type: 'radio'})]
75
+ values_to_match = {tag_name: 'button', value: 'foo'}
76
+
77
+ expect(matcher.match(elements, values_to_match, :all)).to eq []
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,368 @@
1
+ require_relative '../unit_helper'
2
+
3
+ describe Watir::Locators::Element::Matcher do
4
+ include LocatorSpecHelper
5
+
6
+ let(:query_scope) { @query_scope || browser }
7
+ let(:values_to_match) { @values_to_match || {} }
8
+ let(:matcher) { described_class.new(query_scope) }
9
+
10
+ describe '#match' do
11
+ context 'a label element' do
12
+ it 'returns elements with for / id pairs' do
13
+ input_wds = [wd_element(tag_name: 'input', attributes: {id: 'foob_id'}),
14
+ wd_element(tag_name: 'input', attributes: {id: 'bfoo_id'}),
15
+ wd_element(tag_name: 'input', attributes: {id: 'foo_id'})]
16
+
17
+ label_wds = [wd_element(tag_name: 'label', text: 'foob'),
18
+ wd_element(tag_name: 'label', text: 'bfoo'),
19
+ wd_element(tag_name: 'label', text: 'foo')]
20
+
21
+ labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: 'foob_id'),
22
+ element(watir_element: Watir::Label, wd: label_wds[1], for: 'bfoo_id'),
23
+ element(watir_element: Watir::Label, wd: label_wds[2], for: 'foo_id')]
24
+
25
+ # Only the Watir::Label matching the text provided will have wd called
26
+ expect(labels[0]).not_to receive(:for)
27
+ expect(labels[1]).not_to receive(:for)
28
+
29
+ allow(query_scope).to receive(:labels).and_return(labels)
30
+ allow(matcher).to receive(:deprecate_text_regexp).exactly(3).times
31
+ allow_any_instance_of(Watir::Input).to receive(:wd).and_return(input_wds[2], input_wds[2])
32
+
33
+ values_to_match = {label_element: 'foo'}
34
+
35
+ expect(matcher.match(input_wds, values_to_match, :all)).to eq [input_wds[2]]
36
+ end
37
+
38
+ it 'returns elements without for / id pairs' do
39
+ input_wds = [wd_element(tag_name: 'input'),
40
+ wd_element(tag_name: 'input'),
41
+ wd_element(tag_name: 'input')]
42
+
43
+ inputs = [element(watir_element: Watir::Input, wd: input_wds[0]),
44
+ element(watir_element: Watir::Input, wd: input_wds[1]),
45
+ element(watir_element: Watir::Input, wd: input_wds[2])]
46
+
47
+ label_wds = [wd_element(tag_name: 'label', text: 'foob'),
48
+ wd_element(tag_name: 'label', text: 'Foo'),
49
+ wd_element(tag_name: 'label', text: 'foo')]
50
+
51
+ labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: '', input: inputs[0]),
52
+ element(watir_element: Watir::Label, wd: label_wds[1], for: '', input: inputs[1]),
53
+ element(watir_element: Watir::Label, wd: label_wds[2], for: '', input: inputs[2])]
54
+
55
+ # Only the Watir::Label matching the text provided will have wd called
56
+ expect(labels[0]).not_to receive(:for)
57
+ expect(labels[1]).not_to receive(:for)
58
+
59
+ allow(query_scope).to receive(:labels).and_return(labels)
60
+ allow(matcher).to receive(:deprecate_text_regexp).exactly(3).times
61
+
62
+ values_to_match = {label_element: 'foo'}
63
+
64
+ expect(matcher.match(input_wds, values_to_match, :all)).to eq [input_wds[2]]
65
+ end
66
+
67
+ it 'returns elements with multiple matching label text but first missing corresponding element' do
68
+ input_wds = [wd_element(tag_name: 'input'),
69
+ wd_element(tag_name: 'input')]
70
+
71
+ inputs = [element(watir_element: Watir::Input, wd: wd_element(tag_name: 'input')),
72
+ element(watir_element: Watir::Input, wd: input_wds[1])]
73
+
74
+ label_wds = [wd_element(tag_name: 'label', text: 'foo'),
75
+ wd_element(tag_name: 'label', text: 'foo')]
76
+
77
+ labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: '', input: inputs[0]),
78
+ element(watir_element: Watir::Label, wd: label_wds[1], for: '', input: inputs[1])]
79
+
80
+ allow(query_scope).to receive(:labels).and_return(labels)
81
+ allow(matcher).to receive(:deprecate_text_regexp).exactly(2).times
82
+
83
+ values_to_match = {label_element: 'foo'}
84
+
85
+ expect(matcher.match(input_wds, values_to_match, :all)).to eq [input_wds[1]]
86
+ end
87
+
88
+ it 'returns empty Array if no label element matches' do
89
+ input_wds = [wd_element(tag_name: 'input', attributes: {id: 'foo'}),
90
+ wd_element(tag_name: 'input', attributes: {id: 'bfoo'})]
91
+
92
+ label_wds = [wd_element(tag_name: 'label', text: 'Not this'),
93
+ wd_element(tag_name: 'label', text: 'Or This')]
94
+
95
+ labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: 'foo'),
96
+ element(watir_element: Watir::Label, wd: label_wds[1], for: 'bfoo')]
97
+
98
+ allow(query_scope).to receive(:labels).and_return(labels)
99
+ allow(matcher).to receive(:deprecate_text_regexp).exactly(2).times
100
+
101
+ values_to_match = {label_element: 'foo'}
102
+
103
+ expect(matcher.match(input_wds, values_to_match, :all)).to eq []
104
+ end
105
+
106
+ it 'returns empty Array if matching label elements do not have an corresponding input element' do
107
+ input_wds = [wd_element(tag_name: 'input'),
108
+ wd_element(tag_name: 'input')]
109
+
110
+ inputs = [element(watir_element: Watir::Input, wd: wd_element(tag_name: 'input')),
111
+ element(watir_element: Watir::Input, wd: wd_element(tag_name: 'input'))]
112
+
113
+ label_wds = [wd_element(tag_name: 'label', text: 'foob'),
114
+ wd_element(tag_name: 'label', text: 'foo')]
115
+
116
+ labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: '', input: inputs[0]),
117
+ element(watir_element: Watir::Label, wd: label_wds[1], for: '', input: inputs[1])]
118
+
119
+ allow(query_scope).to receive(:labels).and_return(labels)
120
+ allow(matcher).to receive(:deprecate_text_regexp).exactly(2).times
121
+
122
+ values_to_match = {label_element: 'foo'}
123
+
124
+ expect(matcher.match(input_wds, values_to_match, :all)).to eq []
125
+ end
126
+ end
127
+
128
+ context 'when locating one element' do
129
+ before { @filter = :first }
130
+
131
+ it 'by tag name' do
132
+ elements = [wd_element(tag_name: 'div'),
133
+ wd_element(tag_name: 'span')]
134
+ @values_to_match = {tag_name: 'span'}
135
+
136
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
137
+ end
138
+
139
+ it 'by attribute' do
140
+ elements = [wd_element(attributes: {id: 'foo'}),
141
+ wd_element(attributes: {id: 'bar'}),
142
+ wd_element(attributes: {id: 'foobar'})]
143
+ @values_to_match = {id: 'foobar'}
144
+
145
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[2]
146
+ end
147
+
148
+ it 'by class array' do
149
+ elements = [wd_element(attributes: {class: 'foob bar'}),
150
+ wd_element(attributes: {class: 'bar'}),
151
+ wd_element(attributes: {class: 'bar foo'})]
152
+ @values_to_match = {class: %w[foo bar]}
153
+
154
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[2]
155
+ end
156
+
157
+ it 'by positive index' do
158
+ elements = [wd_element(tag_name: 'div'),
159
+ wd_element(tag_name: 'span'),
160
+ wd_element(tag_name: 'div')]
161
+
162
+ @values_to_match = {tag_name: 'div', index: 1}
163
+
164
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[2]
165
+ end
166
+
167
+ it 'by negative index' do
168
+ elements = [wd_element(tag_name: 'div'),
169
+ wd_element(tag_name: 'span'),
170
+ wd_element(tag_name: 'div')]
171
+
172
+ @values_to_match = {tag_name: 'div', index: -1}
173
+
174
+ expect(matcher.match(elements.dup, values_to_match, @filter)).to eq elements[2]
175
+ end
176
+
177
+ it 'by visibility true' do
178
+ elements = [wd_element(tag_name: 'div', "displayed?": false),
179
+ wd_element(tag_name: 'span', "displayed?": true)]
180
+ @values_to_match = {visible: true}
181
+
182
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
183
+ end
184
+
185
+ it 'by visibility false' do
186
+ elements = [wd_element(tag_name: 'div', "displayed?": true),
187
+ wd_element(tag_name: 'span', "displayed?": false)]
188
+ @values_to_match = {visible: false}
189
+
190
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
191
+ end
192
+
193
+ it 'by text' do
194
+ elements = [wd_element(text: 'foo'),
195
+ wd_element(text: 'Foob')]
196
+ @values_to_match = {text: 'Foob'}
197
+
198
+ allow(matcher).to receive(:deprecate_text_regexp).exactly(3).times
199
+
200
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
201
+ end
202
+
203
+ it 'by visible text' do
204
+ elements = [wd_element(text: 'foo'),
205
+ wd_element(text: 'Foob')]
206
+ @values_to_match = {visible_text: /Foo|Bar/}
207
+
208
+ allow(elements[0]).to receive(:text).and_return 'foo'
209
+ allow(elements[1]).to receive(:text).and_return 'Foob'
210
+
211
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
212
+ end
213
+
214
+ it 'by href' do
215
+ elements = [wd_element(tag_name: 'div', attributes: {href: 'froo.com'}),
216
+ wd_element(tag_name: 'span', attributes: {href: 'bar.com'})]
217
+ @values_to_match = {href: /foo|bar/}
218
+
219
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
220
+ end
221
+
222
+ it "returns nil if found element doesn't match the selector tag_name" do
223
+ elements = [wd_element(tag_name: 'div')]
224
+
225
+ @values_to_match = {tag_name: 'span'}
226
+
227
+ expect(matcher.match(elements, values_to_match, @filter)).to eq nil
228
+ end
229
+ end
230
+
231
+ context 'when locating collection' do
232
+ before { @filter = :all }
233
+
234
+ it 'by tag name' do
235
+ elements = [wd_element(tag_name: 'div'),
236
+ wd_element(tag_name: 'span'),
237
+ wd_element(tag_name: 'div')]
238
+ @values_to_match = {tag_name: 'div'}
239
+
240
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
241
+ end
242
+
243
+ it 'by attribute' do
244
+ elements = [wd_element(attributes: {foo: 'foo'}),
245
+ wd_element(attributes: {foo: 'bar'}),
246
+ wd_element(attributes: {foo: 'foo'})]
247
+ @values_to_match = {foo: 'foo'}
248
+
249
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
250
+ end
251
+
252
+ it 'by single class' do
253
+ elements = [wd_element(attributes: {class: 'foo bar cool'}),
254
+ wd_element(attributes: {class: 'foob bar'}),
255
+ wd_element(attributes: {class: 'bar foo foobar'})]
256
+ @values_to_match = {class: 'foo'}
257
+
258
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
259
+ end
260
+
261
+ it 'by class array' do
262
+ elements = [wd_element(attributes: {class: 'foo bar cool'}),
263
+ wd_element(attributes: {class: 'foob bar'}),
264
+ wd_element(attributes: {class: 'bar foo foobar'})]
265
+ @values_to_match = {class: %w[foo bar]}
266
+
267
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
268
+ end
269
+
270
+ it 'by visibility true' do
271
+ elements = [wd_element(tag_name: 'div'),
272
+ wd_element(tag_name: 'span'),
273
+ wd_element(tag_name: 'div')]
274
+ @values_to_match = {visible: true}
275
+
276
+ allow(elements[0]).to receive(:displayed?).and_return false
277
+ allow(elements[1]).to receive(:displayed?).and_return true
278
+ allow(elements[2]).to receive(:displayed?).and_return true
279
+
280
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
281
+ end
282
+
283
+ it 'by visibility false' do
284
+ elements = [wd_element("displayed?": false),
285
+ wd_element("displayed?": true),
286
+ wd_element("displayed?": false)]
287
+ @values_to_match = {visible: false}
288
+
289
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
290
+ end
291
+
292
+ it 'by text' do
293
+ elements = [wd_element(text: 'foo'),
294
+ wd_element(text: 'Foob'),
295
+ wd_element(text: 'bBarb')]
296
+ @values_to_match = {text: /Foo|Bar/}
297
+
298
+ allow(matcher).to receive(:deprecate_text_regexp).exactly(3).times
299
+
300
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
301
+ end
302
+
303
+ it 'by visible text' do
304
+ elements = [wd_element(text: 'foo'),
305
+ wd_element(text: 'Foob'),
306
+ wd_element(text: 'bBarb')]
307
+ @values_to_match = {visible_text: /Foo|Bar/}
308
+
309
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
310
+ end
311
+
312
+ it 'by href' do
313
+ elements = [wd_element(attributes: {href: 'froo.com'}),
314
+ wd_element(attributes: {href: 'bar.com'}),
315
+ wd_element(attributes: {href: 'foobar.com'})]
316
+ @values_to_match = {href: /foo|bar/}
317
+
318
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
319
+ end
320
+
321
+ it 'returns empty Array if found no element matches the selector tag_name' do
322
+ elements = [wd_element(tag_name: 'div')]
323
+
324
+ @values_to_match = {tag_name: 'span'}
325
+
326
+ expect(matcher.match(elements, values_to_match, @filter)).to eq []
327
+ end
328
+ end
329
+
330
+ context 'when matching Regular Expressions' do
331
+ it 'with tag_name' do
332
+ elements = [wd_element(tag_name: 'div'),
333
+ wd_element(tag_name: 'span'),
334
+ wd_element(tag_name: 'div')]
335
+ @values_to_match = {tag_name: /d|q/}
336
+
337
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
338
+ end
339
+
340
+ it 'with single class' do
341
+ elements = [wd_element(attributes: {class: 'foo bar cool'}),
342
+ wd_element(attributes: {class: 'foob bar'}),
343
+ wd_element(attributes: {class: 'bar foo foobar'})]
344
+ @values_to_match = {class: /foob|q/}
345
+
346
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
347
+ end
348
+
349
+ it 'with multiple classes' do
350
+ elements = [wd_element(attributes: {class: 'foo bar cool'}),
351
+ wd_element(attributes: {class: 'foob bar'}),
352
+ wd_element(attributes: {class: 'bar foo foobar'})]
353
+ @values_to_match = {class: [/foob/, /bar/]}
354
+
355
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
356
+ end
357
+
358
+ it 'with attributes' do
359
+ elements = [wd_element(attributes: {foo: 'foo'}),
360
+ wd_element(attributes: {foo: 'bar'}),
361
+ wd_element(attributes: {foo: 'foo'})]
362
+ @values_to_match = {foo: /fo/}
363
+
364
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
365
+ end
366
+ end
367
+ end
368
+ end
@@ -0,0 +1,79 @@
1
+ require_relative '../unit_helper'
2
+
3
+ describe Watir::Locators::TextField::Matcher do
4
+ include LocatorSpecHelper
5
+
6
+ let(:query_scope) { @query_scope || double(Watir::Browser) }
7
+ let(:matcher) { described_class.new(query_scope, @selector) }
8
+
9
+ describe '#match?' do
10
+ context 'when input element' do
11
+ it 'converts text to value' do
12
+ elements = [wd_element(tag_name: 'input', attributes: {value: 'foo'}),
13
+ wd_element(tag_name: 'input', attributes: {value: 'Foob'})]
14
+ values_to_match = {text: 'Foob'}
15
+
16
+ expect(elements[0]).not_to receive(:text)
17
+ expect(elements[1]).not_to receive(:text)
18
+
19
+ expect(matcher.match(elements, values_to_match, :all)).to eq [elements[1]]
20
+ end
21
+
22
+ it 'converts label to value' do
23
+ elements = [wd_element(tag_name: 'input', attributes: {value: 'foo'}),
24
+ wd_element(tag_name: 'input', attributes: {value: 'Foob'})]
25
+ values_to_match = {label: 'Foob'}
26
+
27
+ expect(elements[0]).not_to receive(:attribute).with(values_to_match)
28
+ expect(elements[1]).not_to receive(:attribute).with(values_to_match)
29
+
30
+ expect(matcher.match(elements, values_to_match, :all)).to eq [elements[1]]
31
+ end
32
+
33
+ it 'converts visible_text to value' do
34
+ elements = [wd_element(tag_name: 'input', attributes: {value: 'foo'}),
35
+ wd_element(tag_name: 'input', attributes: {value: 'Foob'})]
36
+ values_to_match = {visible_text: 'Foob'}
37
+
38
+ expect(elements[0]).not_to receive(:attribute).with(values_to_match)
39
+ expect(elements[1]).not_to receive(:attribute).with(values_to_match)
40
+
41
+ expect(matcher.match(elements, values_to_match, :all)).to eq [elements[1]]
42
+ end
43
+ end
44
+
45
+ context 'when label element' do
46
+ it 'converts value to text' do
47
+ elements = [wd_element(tag_name: 'label', text: 'foo'),
48
+ wd_element(tag_name: 'label', text: 'Foob')]
49
+ values_to_match = {value: 'Foob'}
50
+
51
+ expect(elements[0]).not_to receive(:attribute).with(values_to_match)
52
+ expect(elements[1]).not_to receive(:attribute).with(values_to_match)
53
+ allow(matcher).to receive(:deprecate_text_regexp).exactly(2).times
54
+
55
+ expect(matcher.match(elements, values_to_match, :all)).to eq [elements[1]]
56
+ end
57
+
58
+ it 'converts label to text' do
59
+ elements = [wd_element(tag_name: 'label', text: 'foo'),
60
+ wd_element(tag_name: 'label', text: 'Foob')]
61
+ values_to_match = {label: 'Foob'}
62
+
63
+ expect(elements[0]).not_to receive(:attribute).with(values_to_match)
64
+ expect(elements[1]).not_to receive(:attribute).with(values_to_match)
65
+ allow(matcher).to receive(:deprecate_text_regexp).exactly(2).times
66
+
67
+ expect(matcher.match(elements, values_to_match, :all)).to eq [elements[1]]
68
+ end
69
+ end
70
+
71
+ it 'returns empty array if element is not an input' do
72
+ elements = [wd_element(tag_name: 'wrong', text: 'foob', attributes: {value: 'foo'}),
73
+ wd_element(tag_name: 'wrong', text: 'bar', attributes: {value: 'bar'})]
74
+ values_to_match = {tag_name: 'input', value: 'foo'}
75
+
76
+ expect(matcher.match(elements, values_to_match, :all)).to eq []
77
+ end
78
+ end
79
+ end