watir 7.2.0 → 7.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/chrome.yml +1 -1
  3. data/.github/workflows/edge.yml +1 -1
  4. data/.github/workflows/firefox.yml +1 -1
  5. data/.github/workflows/ie.yml +1 -1
  6. data/.github/workflows/safari.yml +1 -1
  7. data/.github/workflows/unit.yml +1 -1
  8. data/.rubocop.yml +25 -12
  9. data/.rubocop_todo.yml +8 -1
  10. data/CHANGES.md +6 -1
  11. data/LICENSE +2 -2
  12. data/lib/watir/capabilities.rb +1 -1
  13. data/lib/watir/locators/anchor/selector_builder.rb +3 -0
  14. data/lib/watir/locators/element/matcher.rb +2 -1
  15. data/lib/watir/locators/element/selector_builder/regexp_disassembler.rb +2 -1
  16. data/lib/watir/locators/text_field/matcher.rb +0 -4
  17. data/lib/watir/version.rb +1 -1
  18. data/spec/locator_spec_helper.rb +0 -8
  19. data/spec/unit/capabilities_spec.rb +553 -561
  20. data/spec/unit/element_locator_spec.rb +89 -78
  21. data/spec/unit/match_elements/button_spec.rb +69 -62
  22. data/spec/unit/match_elements/element_spec.rb +303 -309
  23. data/spec/unit/match_elements/text_field_spec.rb +76 -73
  24. data/spec/unit/selector_builder/anchor_spec.rb +39 -33
  25. data/spec/unit/selector_builder/button_spec.rb +209 -202
  26. data/spec/unit/selector_builder/cell_spec.rb +48 -42
  27. data/spec/unit/selector_builder/element_spec.rb +634 -627
  28. data/spec/unit/selector_builder/row_spec.rb +119 -113
  29. data/spec/unit/selector_builder/text_field_spec.rb +195 -188
  30. data/spec/unit/selector_builder/textarea_spec.rb +22 -14
  31. data/spec/unit/wait_spec.rb +85 -81
  32. data/spec/watirspec/adjacent_spec.rb +249 -247
  33. data/spec/watirspec/after_hooks_spec.rb +161 -159
  34. data/spec/watirspec/alert_spec.rb +61 -58
  35. data/spec/watirspec/browser_spec.rb +409 -409
  36. data/spec/watirspec/capabilities_spec.rb +89 -99
  37. data/spec/watirspec/cookies_spec.rb +120 -118
  38. data/spec/watirspec/drag_and_drop_spec.rb +26 -24
  39. data/spec/watirspec/element_hidden_spec.rb +66 -60
  40. data/spec/watirspec/elements/area_spec.rb +49 -47
  41. data/spec/watirspec/elements/areas_spec.rb +27 -25
  42. data/spec/watirspec/elements/button_spec.rb +253 -251
  43. data/spec/watirspec/elements/buttons_spec.rb +36 -34
  44. data/spec/watirspec/elements/checkbox_spec.rb +48 -36
  45. data/spec/watirspec/elements/checkboxes_spec.rb +29 -27
  46. data/spec/watirspec/elements/collections_spec.rb +126 -119
  47. data/spec/watirspec/elements/date_field_spec.rb +185 -183
  48. data/spec/watirspec/elements/date_fields_spec.rb +29 -27
  49. data/spec/watirspec/elements/date_time_field_spec.rb +197 -195
  50. data/spec/watirspec/elements/date_time_fields_spec.rb +30 -28
  51. data/spec/watirspec/elements/dd_spec.rb +87 -85
  52. data/spec/watirspec/elements/dds_spec.rb +27 -25
  53. data/spec/watirspec/elements/del_spec.rb +106 -104
  54. data/spec/watirspec/elements/dels_spec.rb +26 -24
  55. data/spec/watirspec/elements/div_spec.rb +225 -221
  56. data/spec/watirspec/elements/divs_spec.rb +37 -35
  57. data/spec/watirspec/elements/dl_spec.rb +110 -108
  58. data/spec/watirspec/elements/dls_spec.rb +28 -26
  59. data/spec/watirspec/elements/dt_spec.rb +86 -84
  60. data/spec/watirspec/elements/dts_spec.rb +27 -25
  61. data/spec/watirspec/elements/element_spec.rb +794 -775
  62. data/spec/watirspec/elements/elements_spec.rb +40 -38
  63. data/spec/watirspec/elements/em_spec.rb +62 -60
  64. data/spec/watirspec/elements/ems_spec.rb +28 -26
  65. data/spec/watirspec/elements/filefield_spec.rb +116 -114
  66. data/spec/watirspec/elements/filefields_spec.rb +28 -26
  67. data/spec/watirspec/elements/font_spec.rb +20 -18
  68. data/spec/watirspec/elements/form_spec.rb +51 -49
  69. data/spec/watirspec/elements/forms_spec.rb +29 -27
  70. data/spec/watirspec/elements/frame_spec.rb +89 -87
  71. data/spec/watirspec/elements/frames_spec.rb +27 -25
  72. data/spec/watirspec/elements/hidden_spec.rb +81 -79
  73. data/spec/watirspec/elements/hiddens_spec.rb +28 -26
  74. data/spec/watirspec/elements/hn_spec.rb +59 -57
  75. data/spec/watirspec/elements/hns_spec.rb +26 -24
  76. data/spec/watirspec/elements/iframe_spec.rb +195 -191
  77. data/spec/watirspec/elements/iframes_spec.rb +31 -29
  78. data/spec/watirspec/elements/image_spec.rb +148 -146
  79. data/spec/watirspec/elements/images_spec.rb +26 -24
  80. data/spec/watirspec/elements/input_spec.rb +9 -7
  81. data/spec/watirspec/elements/ins_spec.rb +106 -104
  82. data/spec/watirspec/elements/inses_spec.rb +26 -24
  83. data/spec/watirspec/elements/label_spec.rb +56 -54
  84. data/spec/watirspec/elements/labels_spec.rb +26 -24
  85. data/spec/watirspec/elements/li_spec.rb +76 -74
  86. data/spec/watirspec/elements/link_spec.rb +173 -177
  87. data/spec/watirspec/elements/links_spec.rb +45 -43
  88. data/spec/watirspec/elements/lis_spec.rb +27 -25
  89. data/spec/watirspec/elements/list_spec.rb +44 -42
  90. data/spec/watirspec/elements/map_spec.rb +57 -55
  91. data/spec/watirspec/elements/maps_spec.rb +27 -25
  92. data/spec/watirspec/elements/meta_spec.rb +16 -14
  93. data/spec/watirspec/elements/metas_spec.rb +26 -24
  94. data/spec/watirspec/elements/ol_spec.rb +56 -65
  95. data/spec/watirspec/elements/ols_spec.rb +26 -24
  96. data/spec/watirspec/elements/option_spec.rb +91 -89
  97. data/spec/watirspec/elements/p_spec.rb +81 -79
  98. data/spec/watirspec/elements/pre_spec.rb +77 -75
  99. data/spec/watirspec/elements/pres_spec.rb +26 -24
  100. data/spec/watirspec/elements/ps_spec.rb +26 -24
  101. data/spec/watirspec/elements/radio_spec.rb +251 -249
  102. data/spec/watirspec/elements/radios_spec.rb +28 -26
  103. data/spec/watirspec/elements/select_list_spec.rb +537 -526
  104. data/spec/watirspec/elements/select_lists_spec.rb +32 -30
  105. data/spec/watirspec/elements/span_spec.rb +112 -110
  106. data/spec/watirspec/elements/spans_spec.rb +26 -24
  107. data/spec/watirspec/elements/strong_spec.rb +58 -58
  108. data/spec/watirspec/elements/strongs_spec.rb +28 -26
  109. data/spec/watirspec/elements/table_spec.rb +192 -139
  110. data/spec/watirspec/elements/tables_spec.rb +28 -26
  111. data/spec/watirspec/elements/tbody_spec.rb +74 -72
  112. data/spec/watirspec/elements/tbodys_spec.rb +40 -38
  113. data/spec/watirspec/elements/td_spec.rb +66 -64
  114. data/spec/watirspec/elements/tds_spec.rb +41 -39
  115. data/spec/watirspec/elements/text_field_spec.rb +206 -204
  116. data/spec/watirspec/elements/text_fields_spec.rb +30 -28
  117. data/spec/watirspec/elements/textarea_spec.rb +20 -18
  118. data/spec/watirspec/elements/textareas_spec.rb +18 -16
  119. data/spec/watirspec/elements/tfoot_spec.rb +65 -63
  120. data/spec/watirspec/elements/tfoots_spec.rb +42 -40
  121. data/spec/watirspec/elements/thead_spec.rb +65 -63
  122. data/spec/watirspec/elements/theads_spec.rb +42 -40
  123. data/spec/watirspec/elements/tr_spec.rb +48 -46
  124. data/spec/watirspec/elements/trs_spec.rb +42 -40
  125. data/spec/watirspec/elements/ul_spec.rb +56 -54
  126. data/spec/watirspec/elements/uls_spec.rb +26 -24
  127. data/spec/watirspec/radio_set_spec.rb +269 -267
  128. data/spec/watirspec/screenshot_spec.rb +20 -18
  129. data/spec/watirspec/scroll_spec.rb +152 -148
  130. data/spec/watirspec/shadow_root_spec.rb +86 -83
  131. data/spec/watirspec/user_editable_spec.rb +200 -198
  132. data/spec/watirspec/wait_spec.rb +289 -295
  133. data/spec/watirspec/window_switching_spec.rb +422 -434
  134. data/spec/watirspec_helper.rb +1 -23
  135. data/watir.gemspec +5 -4
  136. metadata +44 -33
  137. data/spec/watirspec/attributes_spec.rb +0 -18
  138. data/spec/watirspec/elements/table_nesting_spec.rb +0 -51
  139. data/spec/watirspec/special_chars_spec.rb +0 -23
@@ -2,428 +2,422 @@
2
2
 
3
3
  require_relative '../unit_helper'
4
4
 
5
- describe Watir::Locators::Element::Matcher do
6
- include LocatorSpecHelper
5
+ module Watir
6
+ module Locators
7
+ class Element
8
+ describe Matcher do
9
+ include LocatorSpecHelper
7
10
 
8
- let(:query_scope) { @query_scope || browser }
9
- let(:values_to_match) { @values_to_match || {} }
10
- let(:matcher) { described_class.new(query_scope, values_to_match) }
11
+ let(:query_scope) { @query_scope || browser }
12
+ let(:values_to_match) { @values_to_match || {} }
13
+ let(:matcher) { described_class.new(query_scope, values_to_match) }
11
14
 
12
- describe '#match' do
13
- context 'a label element' do
14
- it 'returns elements with for / id pairs' do
15
- input_wds = [wd_element(tag_name: 'input', attributes: {id: 'foob_id'}),
16
- wd_element(tag_name: 'input', attributes: {id: 'bfoo_id'}),
17
- wd_element(tag_name: 'input', attributes: {id: 'foo_id'})]
15
+ describe '#match' do
16
+ context 'when a label element' do
17
+ it 'returns elements with for / id pairs' do
18
+ input_wds = [wd_element(tag_name: 'input', attributes: {id: 'foob_id'}),
19
+ wd_element(tag_name: 'input', attributes: {id: 'bfoo_id'}),
20
+ wd_element(tag_name: 'input', attributes: {id: 'foo_id'})]
18
21
 
19
- label_wds = [wd_element(tag_name: 'label'),
20
- wd_element(tag_name: 'label'),
21
- wd_element(tag_name: 'label')]
22
+ label_wds = [wd_element(tag_name: 'label'),
23
+ wd_element(tag_name: 'label'),
24
+ wd_element(tag_name: 'label')]
22
25
 
23
- allow(browser).to receive(:execute_script).and_return('foob', 'Foo', 'foo')
26
+ allow(browser).to receive(:execute_script).and_return('foob', 'Foo', 'foo')
24
27
 
25
- labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: 'foob_id'),
26
- element(watir_element: Watir::Label, wd: label_wds[1], for: 'bfoo_id'),
27
- element(watir_element: Watir::Label, wd: label_wds[2], for: 'foo_id')]
28
+ labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: 'foob_id'),
29
+ element(watir_element: Watir::Label, wd: label_wds[1], for: 'bfoo_id'),
30
+ element(watir_element: Watir::Label, wd: label_wds[2], for: 'foo_id')]
28
31
 
29
- # Only the Watir::Label matching the text provided will have wd called
30
- expect(labels[0]).not_to receive(:for)
31
- expect(labels[1]).not_to receive(:for)
32
+ allow(query_scope).to receive(:labels).and_return(labels)
33
+ allow_any_instance_of(Watir::Input).to receive(:wd).and_return(input_wds[2], input_wds[2])
32
34
 
33
- allow(query_scope).to receive(:labels).and_return(labels)
34
- allow_any_instance_of(Watir::Input).to receive(:wd).and_return(input_wds[2], input_wds[2])
35
+ values_to_match = {label_element: 'foo'}
36
+ expect(matcher.match(input_wds, values_to_match, :all)).to eq [input_wds[2]]
37
+ # Only the Watir::Label matching the text provided will have wd called
38
+ expect(labels[0]).not_to have_received(:for)
39
+ expect(labels[1]).not_to have_received(:for)
40
+ end
35
41
 
36
- values_to_match = {label_element: 'foo'}
42
+ it 'returns elements without for / id pairs' do
43
+ input_wds = [wd_element(tag_name: 'input'),
44
+ wd_element(tag_name: 'input'),
45
+ wd_element(tag_name: 'input')]
37
46
 
38
- expect(matcher.match(input_wds, values_to_match, :all)).to eq [input_wds[2]]
39
- end
47
+ inputs = [element(watir_element: Watir::Input, wd: input_wds[0]),
48
+ element(watir_element: Watir::Input, wd: input_wds[1]),
49
+ element(watir_element: Watir::Input, wd: input_wds[2])]
40
50
 
41
- it 'returns elements without for / id pairs' do
42
- input_wds = [wd_element(tag_name: 'input'),
43
- wd_element(tag_name: 'input'),
44
- wd_element(tag_name: 'input')]
51
+ label_wds = [wd_element(tag_name: 'label'),
52
+ wd_element(tag_name: 'label'),
53
+ wd_element(tag_name: 'label')]
45
54
 
46
- inputs = [element(watir_element: Watir::Input, wd: input_wds[0]),
47
- element(watir_element: Watir::Input, wd: input_wds[1]),
48
- element(watir_element: Watir::Input, wd: input_wds[2])]
55
+ allow(browser).to receive(:execute_script).and_return('foob', 'Foo', 'foo')
49
56
 
50
- label_wds = [wd_element(tag_name: 'label'),
51
- wd_element(tag_name: 'label'),
52
- wd_element(tag_name: 'label')]
57
+ labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: '', input: inputs[0]),
58
+ element(watir_element: Watir::Label, wd: label_wds[1], for: '', input: inputs[1]),
59
+ element(watir_element: Watir::Label, wd: label_wds[2], for: '', input: inputs[2])]
53
60
 
54
- allow(browser).to receive(:execute_script).and_return('foob', 'Foo', 'foo')
61
+ allow(query_scope).to receive(:labels).and_return(labels)
55
62
 
56
- labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: '', input: inputs[0]),
57
- element(watir_element: Watir::Label, wd: label_wds[1], for: '', input: inputs[1]),
58
- element(watir_element: Watir::Label, wd: label_wds[2], for: '', input: inputs[2])]
63
+ values_to_match = {label_element: 'foo'}
64
+ expect(matcher.match(input_wds, values_to_match, :all)).to eq [input_wds[2]]
59
65
 
60
- # Only the Watir::Label matching the text provided will have wd called
61
- expect(labels[0]).not_to receive(:for)
62
- expect(labels[1]).not_to receive(:for)
66
+ # Only the Watir::Label matching the text provided will have wd called
67
+ expect(labels[0]).not_to have_received(:for)
68
+ expect(labels[1]).not_to have_received(:for)
69
+ end
63
70
 
64
- allow(query_scope).to receive(:labels).and_return(labels)
71
+ it 'returns elements with multiple matching label text but first missing corresponding element' do
72
+ input_wds = [wd_element(tag_name: 'input'),
73
+ wd_element(tag_name: 'input')]
65
74
 
66
- values_to_match = {label_element: 'foo'}
75
+ inputs = [element(watir_element: Watir::Input, wd: wd_element(tag_name: 'input')),
76
+ element(watir_element: Watir::Input, wd: input_wds[1])]
67
77
 
68
- expect(matcher.match(input_wds, values_to_match, :all)).to eq [input_wds[2]]
69
- end
78
+ label_wds = [wd_element(tag_name: 'label'),
79
+ wd_element(tag_name: 'label'),
80
+ wd_element(tag_name: 'label')]
70
81
 
71
- it 'returns elements with multiple matching label text but first missing corresponding element' do
72
- input_wds = [wd_element(tag_name: 'input'),
73
- wd_element(tag_name: 'input')]
82
+ allow(browser).to receive(:execute_script).and_return('foo', 'foo')
74
83
 
75
- inputs = [element(watir_element: Watir::Input, wd: wd_element(tag_name: 'input')),
76
- element(watir_element: Watir::Input, wd: input_wds[1])]
84
+ labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: '', input: inputs[0]),
85
+ element(watir_element: Watir::Label, wd: label_wds[1], for: '', input: inputs[1])]
77
86
 
78
- label_wds = [wd_element(tag_name: 'label'),
79
- wd_element(tag_name: 'label'),
80
- wd_element(tag_name: 'label')]
87
+ allow(query_scope).to receive(:labels).and_return(labels)
81
88
 
82
- allow(browser).to receive(:execute_script).and_return('foo', 'foo')
89
+ values_to_match = {label_element: 'foo'}
83
90
 
84
- labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: '', input: inputs[0]),
85
- element(watir_element: Watir::Label, wd: label_wds[1], for: '', input: inputs[1])]
91
+ expect(matcher.match(input_wds, values_to_match, :all)).to eq [input_wds[1]]
92
+ end
86
93
 
87
- allow(query_scope).to receive(:labels).and_return(labels)
94
+ it 'returns empty Array if no label element matches' do
95
+ input_wds = [wd_element(tag_name: 'input', attributes: {id: 'foo'}),
96
+ wd_element(tag_name: 'input', attributes: {id: 'bfoo'})]
88
97
 
89
- values_to_match = {label_element: 'foo'}
98
+ label_wds = [wd_element(tag_name: 'label'),
99
+ wd_element(tag_name: 'label'),
100
+ wd_element(tag_name: 'label')]
90
101
 
91
- expect(matcher.match(input_wds, values_to_match, :all)).to eq [input_wds[1]]
92
- end
102
+ allow(browser).to receive(:execute_script).and_return('Not this', 'or this')
93
103
 
94
- it 'returns empty Array if no label element matches' do
95
- input_wds = [wd_element(tag_name: 'input', attributes: {id: 'foo'}),
96
- wd_element(tag_name: 'input', attributes: {id: 'bfoo'})]
104
+ labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: 'foo'),
105
+ element(watir_element: Watir::Label, wd: label_wds[1], for: 'bfoo')]
97
106
 
98
- label_wds = [wd_element(tag_name: 'label'),
99
- wd_element(tag_name: 'label'),
100
- wd_element(tag_name: 'label')]
107
+ allow(query_scope).to receive(:labels).and_return(labels)
101
108
 
102
- allow(browser).to receive(:execute_script).and_return('Not this', 'or this')
109
+ values_to_match = {label_element: 'foo'}
103
110
 
104
- labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: 'foo'),
105
- element(watir_element: Watir::Label, wd: label_wds[1], for: 'bfoo')]
111
+ expect(matcher.match(input_wds, values_to_match, :all)).to eq []
112
+ end
106
113
 
107
- allow(query_scope).to receive(:labels).and_return(labels)
114
+ it 'returns empty Array if matching label elements do not have an corresponding input element' do
115
+ input_wds = [wd_element(tag_name: 'input'),
116
+ wd_element(tag_name: 'input')]
108
117
 
109
- values_to_match = {label_element: 'foo'}
118
+ inputs = [element(watir_element: Watir::Input, wd: wd_element(tag_name: 'input')),
119
+ element(watir_element: Watir::Input, wd: wd_element(tag_name: 'input'))]
110
120
 
111
- expect(matcher.match(input_wds, values_to_match, :all)).to eq []
112
- end
121
+ label_wds = [wd_element(tag_name: 'label'),
122
+ wd_element(tag_name: 'label')]
113
123
 
114
- it 'returns empty Array if matching label elements do not have an corresponding input element' do
115
- input_wds = [wd_element(tag_name: 'input'),
116
- wd_element(tag_name: 'input')]
124
+ allow(browser).to receive(:execute_script).and_return('foob', 'foo')
117
125
 
118
- inputs = [element(watir_element: Watir::Input, wd: wd_element(tag_name: 'input')),
119
- element(watir_element: Watir::Input, wd: wd_element(tag_name: 'input'))]
126
+ labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: '', input: inputs[0]),
127
+ element(watir_element: Watir::Label, wd: label_wds[1], for: '', input: inputs[1])]
120
128
 
121
- label_wds = [wd_element(tag_name: 'label'),
122
- wd_element(tag_name: 'label')]
129
+ allow(query_scope).to receive(:labels).and_return(labels)
123
130
 
124
- allow(browser).to receive(:execute_script).and_return('foob', 'foo')
131
+ values_to_match = {label_element: 'foo'}
125
132
 
126
- labels = [element(watir_element: Watir::Label, wd: label_wds[0], for: '', input: inputs[0]),
127
- element(watir_element: Watir::Label, wd: label_wds[1], for: '', input: inputs[1])]
133
+ expect(matcher.match(input_wds, values_to_match, :all)).to eq []
134
+ end
135
+ end
128
136
 
129
- allow(query_scope).to receive(:labels).and_return(labels)
137
+ context 'when locating one element' do
138
+ before { @filter = :first }
130
139
 
131
- values_to_match = {label_element: 'foo'}
140
+ it 'by tag name' do
141
+ elements = [wd_element(tag_name: 'div'),
142
+ wd_element(tag_name: 'span')]
143
+ @values_to_match = {tag_name: 'span'}
132
144
 
133
- expect(matcher.match(input_wds, values_to_match, :all)).to eq []
134
- end
135
- end
145
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
146
+ end
136
147
 
137
- context 'when locating one element' do
138
- before { @filter = :first }
148
+ it 'by attribute' do
149
+ elements = [wd_element(attributes: {id: 'foo'}),
150
+ wd_element(attributes: {id: 'bar'}),
151
+ wd_element(attributes: {id: 'foobar'})]
152
+ @values_to_match = {id: 'foobar'}
139
153
 
140
- it 'by tag name' do
141
- elements = [wd_element(tag_name: 'div'),
142
- wd_element(tag_name: 'span')]
143
- @values_to_match = {tag_name: 'span'}
154
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[2]
155
+ end
144
156
 
145
- expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
146
- end
157
+ it 'by class array' do
158
+ elements = [wd_element(attributes: {class: 'foob bar'}),
159
+ wd_element(attributes: {class: 'bar'}),
160
+ wd_element(attributes: {class: 'bar foo'})]
161
+ @values_to_match = {class: %w[foo bar]}
147
162
 
148
- it 'by attribute' do
149
- elements = [wd_element(attributes: {id: 'foo'}),
150
- wd_element(attributes: {id: 'bar'}),
151
- wd_element(attributes: {id: 'foobar'})]
152
- @values_to_match = {id: 'foobar'}
163
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[2]
164
+ end
153
165
 
154
- expect(matcher.match(elements, values_to_match, @filter)).to eq elements[2]
155
- end
166
+ it 'by positive index' do
167
+ elements = [wd_element(tag_name: 'div'),
168
+ wd_element(tag_name: 'span'),
169
+ wd_element(tag_name: 'div')]
156
170
 
157
- it 'by class array' do
158
- elements = [wd_element(attributes: {class: 'foob bar'}),
159
- wd_element(attributes: {class: 'bar'}),
160
- wd_element(attributes: {class: 'bar foo'})]
161
- @values_to_match = {class: %w[foo bar]}
171
+ @values_to_match = {tag_name: 'div', index: 1}
162
172
 
163
- expect(matcher.match(elements, values_to_match, @filter)).to eq elements[2]
164
- end
173
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[2]
174
+ end
165
175
 
166
- it 'by positive index' do
167
- elements = [wd_element(tag_name: 'div'),
168
- wd_element(tag_name: 'span'),
169
- wd_element(tag_name: 'div')]
176
+ it 'by negative index' do
177
+ elements = [wd_element(tag_name: 'div'),
178
+ wd_element(tag_name: 'span'),
179
+ wd_element(tag_name: 'div')]
170
180
 
171
- @values_to_match = {tag_name: 'div', index: 1}
181
+ @values_to_match = {tag_name: 'div', index: -1}
172
182
 
173
- expect(matcher.match(elements, values_to_match, @filter)).to eq elements[2]
174
- end
183
+ expect(matcher.match(elements.dup, values_to_match, @filter)).to eq elements[2]
184
+ end
175
185
 
176
- it 'by negative index' do
177
- elements = [wd_element(tag_name: 'div'),
178
- wd_element(tag_name: 'span'),
179
- wd_element(tag_name: 'div')]
186
+ it 'by visibility true' do
187
+ elements = [wd_element(tag_name: 'div', displayed?: false),
188
+ wd_element(tag_name: 'span', displayed?: true)]
189
+ @values_to_match = {visible: true}
180
190
 
181
- @values_to_match = {tag_name: 'div', index: -1}
191
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
192
+ end
182
193
 
183
- expect(matcher.match(elements.dup, values_to_match, @filter)).to eq elements[2]
184
- end
194
+ it 'by visibility false' do
195
+ elements = [wd_element(tag_name: 'div', displayed?: true),
196
+ wd_element(tag_name: 'span', displayed?: false)]
197
+ @values_to_match = {visible: false}
185
198
 
186
- it 'by visibility true' do
187
- elements = [wd_element(tag_name: 'div', displayed?: false),
188
- wd_element(tag_name: 'span', displayed?: true)]
189
- @values_to_match = {visible: true}
199
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
200
+ end
190
201
 
191
- expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
192
- end
202
+ it 'by text' do
203
+ elements = [wd_element, wd_element]
204
+ @values_to_match = {text: 'Foob'}
193
205
 
194
- it 'by visibility false' do
195
- elements = [wd_element(tag_name: 'div', displayed?: true),
196
- wd_element(tag_name: 'span', displayed?: false)]
197
- @values_to_match = {visible: false}
206
+ allow(browser).to receive(:execute_script).and_return('foo', 'Foob')
198
207
 
199
- expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
200
- end
208
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
209
+ end
201
210
 
202
- it 'by text' do
203
- elements = [wd_element, wd_element]
204
- @values_to_match = {text: 'Foob'}
211
+ it 'by visible text' do
212
+ elements = [wd_element(text: 'foo'),
213
+ wd_element(text: 'Foob')]
214
+ @values_to_match = {visible_text: /Foo|Bar/}
205
215
 
206
- allow(browser).to receive(:execute_script).and_return('foo', 'Foob')
216
+ allow(elements[0]).to receive(:text).and_return 'foo'
217
+ allow(elements[1]).to receive(:text).and_return 'Foob'
207
218
 
208
- expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
209
- end
219
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
220
+ end
210
221
 
211
- it 'by visible text' do
212
- elements = [wd_element(text: 'foo'),
213
- wd_element(text: 'Foob')]
214
- @values_to_match = {visible_text: /Foo|Bar/}
222
+ it 'by href' do
223
+ elements = [wd_element(tag_name: 'div', attributes: {href: 'froo.com'}),
224
+ wd_element(tag_name: 'span', attributes: {href: 'bar.com'})]
225
+ @values_to_match = {href: /foo|bar/}
215
226
 
216
- allow(elements[0]).to receive(:text).and_return 'foo'
217
- allow(elements[1]).to receive(:text).and_return 'Foob'
227
+ expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
228
+ end
218
229
 
219
- expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
220
- end
230
+ it "returns nil if found element doesn't match the selector tag_name" do
231
+ elements = [wd_element(tag_name: 'div')]
221
232
 
222
- it 'by href' do
223
- elements = [wd_element(tag_name: 'div', attributes: {href: 'froo.com'}),
224
- wd_element(tag_name: 'span', attributes: {href: 'bar.com'})]
225
- @values_to_match = {href: /foo|bar/}
233
+ @values_to_match = {tag_name: 'span'}
226
234
 
227
- expect(matcher.match(elements, values_to_match, @filter)).to eq elements[1]
228
- end
235
+ expect(matcher.match(elements, values_to_match, @filter)).to be_nil
236
+ end
237
+ end
229
238
 
230
- it "returns nil if found element doesn't match the selector tag_name" do
231
- elements = [wd_element(tag_name: 'div')]
239
+ context 'when locating collection' do
240
+ before { @filter = :all }
232
241
 
233
- @values_to_match = {tag_name: 'span'}
242
+ it 'by tag name' do
243
+ elements = [wd_element(tag_name: 'div'),
244
+ wd_element(tag_name: 'span'),
245
+ wd_element(tag_name: 'div')]
246
+ @values_to_match = {tag_name: 'div'}
234
247
 
235
- expect(matcher.match(elements, values_to_match, @filter)).to eq nil
236
- end
237
- end
248
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
249
+ end
238
250
 
239
- context 'when locating collection' do
240
- before { @filter = :all }
251
+ it 'by attribute' do
252
+ elements = [wd_element(attributes: {foo: 'foo'}),
253
+ wd_element(attributes: {foo: 'bar'}),
254
+ wd_element(attributes: {foo: 'foo'})]
255
+ @values_to_match = {foo: 'foo'}
241
256
 
242
- it 'by tag name' do
243
- elements = [wd_element(tag_name: 'div'),
244
- wd_element(tag_name: 'span'),
245
- wd_element(tag_name: 'div')]
246
- @values_to_match = {tag_name: 'div'}
257
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
258
+ end
247
259
 
248
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
249
- end
260
+ it 'by single class' do
261
+ elements = [wd_element(attributes: {class: 'foo bar cool'}),
262
+ wd_element(attributes: {class: 'foob bar'}),
263
+ wd_element(attributes: {class: 'bar foo foobar'})]
264
+ @values_to_match = {class: 'foo'}
250
265
 
251
- it 'by attribute' do
252
- elements = [wd_element(attributes: {foo: 'foo'}),
253
- wd_element(attributes: {foo: 'bar'}),
254
- wd_element(attributes: {foo: 'foo'})]
255
- @values_to_match = {foo: 'foo'}
266
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
267
+ end
256
268
 
257
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
258
- end
269
+ it 'by class array' do
270
+ elements = [wd_element(attributes: {class: 'foo bar cool'}),
271
+ wd_element(attributes: {class: 'foob bar'}),
272
+ wd_element(attributes: {class: 'bar foo foobar'})]
273
+ @values_to_match = {class: %w[foo bar]}
259
274
 
260
- it 'by single class' do
261
- elements = [wd_element(attributes: {class: 'foo bar cool'}),
262
- wd_element(attributes: {class: 'foob bar'}),
263
- wd_element(attributes: {class: 'bar foo foobar'})]
264
- @values_to_match = {class: 'foo'}
275
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
276
+ end
265
277
 
266
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
267
- end
278
+ it 'by visibility true' do
279
+ elements = [wd_element(tag_name: 'div'),
280
+ wd_element(tag_name: 'span'),
281
+ wd_element(tag_name: 'div')]
282
+ @values_to_match = {visible: true}
268
283
 
269
- it 'by class array' do
270
- elements = [wd_element(attributes: {class: 'foo bar cool'}),
271
- wd_element(attributes: {class: 'foob bar'}),
272
- wd_element(attributes: {class: 'bar foo foobar'})]
273
- @values_to_match = {class: %w[foo bar]}
284
+ allow(elements[0]).to receive(:displayed?).and_return false
285
+ allow(elements[1]).to receive(:displayed?).and_return true
286
+ allow(elements[2]).to receive(:displayed?).and_return true
274
287
 
275
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
276
- end
277
-
278
- it 'by visibility true' do
279
- elements = [wd_element(tag_name: 'div'),
280
- wd_element(tag_name: 'span'),
281
- wd_element(tag_name: 'div')]
282
- @values_to_match = {visible: true}
288
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
289
+ end
283
290
 
284
- allow(elements[0]).to receive(:displayed?).and_return false
285
- allow(elements[1]).to receive(:displayed?).and_return true
286
- allow(elements[2]).to receive(:displayed?).and_return true
291
+ it 'by visibility false' do
292
+ elements = [wd_element(displayed?: false),
293
+ wd_element(displayed?: true),
294
+ wd_element(displayed?: false)]
295
+ @values_to_match = {visible: false}
287
296
 
288
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
289
- end
297
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
298
+ end
290
299
 
291
- it 'by visibility false' do
292
- elements = [wd_element(displayed?: false),
293
- wd_element(displayed?: true),
294
- wd_element(displayed?: false)]
295
- @values_to_match = {visible: false}
296
-
297
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
298
- end
300
+ it 'by text' do
301
+ elements = [wd_element, wd_element, wd_element]
302
+ @values_to_match = {text: /Foo|Bar/}
299
303
 
300
- it 'by text' do
301
- elements = [wd_element, wd_element, wd_element]
302
- @values_to_match = {text: /Foo|Bar/}
304
+ allow(browser).to receive(:execute_script).and_return('foo', 'Foob', 'bBarb')
303
305
 
304
- allow(browser).to receive(:execute_script).and_return('foo', 'Foob', 'bBarb')
306
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
307
+ end
305
308
 
306
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
307
- end
309
+ it 'by visible text' do
310
+ elements = [wd_element(text: 'foo'),
311
+ wd_element(text: 'Foob'),
312
+ wd_element(text: 'bBarb')]
313
+ @values_to_match = {visible_text: /Foo|Bar/}
308
314
 
309
- it 'by visible text' do
310
- elements = [wd_element(text: 'foo'),
311
- wd_element(text: 'Foob'),
312
- wd_element(text: 'bBarb')]
313
- @values_to_match = {visible_text: /Foo|Bar/}
315
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
316
+ end
314
317
 
315
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
316
- end
318
+ it 'by href' do
319
+ elements = [wd_element(attributes: {href: 'froo.com'}),
320
+ wd_element(attributes: {href: 'bar.com'}),
321
+ wd_element(attributes: {href: 'foobar.com'})]
322
+ @values_to_match = {href: /foo|bar/}
317
323
 
318
- it 'by href' do
319
- elements = [wd_element(attributes: {href: 'froo.com'}),
320
- wd_element(attributes: {href: 'bar.com'}),
321
- wd_element(attributes: {href: 'foobar.com'})]
322
- @values_to_match = {href: /foo|bar/}
324
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
325
+ end
323
326
 
324
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
325
- end
327
+ it 'returns empty Array if found no element matches the selector tag_name' do
328
+ elements = [wd_element(tag_name: 'div')]
326
329
 
327
- it 'returns empty Array if found no element matches the selector tag_name' do
328
- elements = [wd_element(tag_name: 'div')]
330
+ @values_to_match = {tag_name: 'span'}
329
331
 
330
- @values_to_match = {tag_name: 'span'}
332
+ expect(matcher.match(elements, values_to_match, @filter)).to eq []
333
+ end
334
+ end
331
335
 
332
- expect(matcher.match(elements, values_to_match, @filter)).to eq []
333
- end
334
- end
336
+ context 'when matching Regular Expressions' do
337
+ it 'with white space' do
338
+ allow(browser).to receive(:execute_script).and_return("\n match this \n", 'no', 'match this')
335
339
 
336
- context 'when matching Regular Expressions' do
337
- it 'with white space' do
338
- allow(browser).to receive(:execute_script).and_return("\n match this \n", 'no', 'match this')
340
+ elements = [wd_element,
341
+ wd_element,
342
+ wd_element]
343
+ @values_to_match = {text: /^match this$/}
339
344
 
340
- elements = [wd_element,
341
- wd_element,
342
- wd_element]
343
- @values_to_match = {text: /^match this$/}
345
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
346
+ end
344
347
 
345
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
346
- end
348
+ it 'with tag_name' do
349
+ elements = [wd_element(tag_name: 'div'),
350
+ wd_element(tag_name: 'span'),
351
+ wd_element(tag_name: 'div')]
352
+ @values_to_match = {tag_name: /d|q/}
347
353
 
348
- it 'with tag_name' do
349
- elements = [wd_element(tag_name: 'div'),
350
- wd_element(tag_name: 'span'),
351
- wd_element(tag_name: 'div')]
352
- @values_to_match = {tag_name: /d|q/}
354
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
355
+ end
353
356
 
354
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
355
- end
357
+ it 'with single class' do
358
+ elements = [wd_element(attributes: {class: 'foo bar cool'}),
359
+ wd_element(attributes: {class: 'foob bar'}),
360
+ wd_element(attributes: {class: 'bar foo foobar'})]
361
+ @values_to_match = {class: /foob|q/}
356
362
 
357
- it 'with single class' do
358
- elements = [wd_element(attributes: {class: 'foo bar cool'}),
359
- wd_element(attributes: {class: 'foob bar'}),
360
- wd_element(attributes: {class: 'bar foo foobar'})]
361
- @values_to_match = {class: /foob|q/}
363
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
364
+ end
362
365
 
363
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
364
- end
366
+ it 'with multiple classes' do
367
+ elements = [wd_element(attributes: {class: 'foo bar cool'}),
368
+ wd_element(attributes: {class: 'foob bar'}),
369
+ wd_element(attributes: {class: 'bar foo foobar'})]
370
+ @values_to_match = {class: [/foob/, /bar/]}
365
371
 
366
- it 'with multiple classes' do
367
- elements = [wd_element(attributes: {class: 'foo bar cool'}),
368
- wd_element(attributes: {class: 'foob bar'}),
369
- wd_element(attributes: {class: 'bar foo foobar'})]
370
- @values_to_match = {class: [/foob/, /bar/]}
372
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
373
+ end
371
374
 
372
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[1], elements[2]]
373
- end
375
+ it 'with attributes' do
376
+ elements = [wd_element(attributes: {foo: 'foo'}),
377
+ wd_element(attributes: {foo: 'bar'}),
378
+ wd_element(attributes: {foo: 'foo'})]
379
+ @values_to_match = {foo: /fo/}
374
380
 
375
- it 'with attributes' do
376
- elements = [wd_element(attributes: {foo: 'foo'}),
377
- wd_element(attributes: {foo: 'bar'}),
378
- wd_element(attributes: {foo: 'foo'})]
379
- @values_to_match = {foo: /fo/}
381
+ expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
382
+ end
383
+ end
380
384
 
381
- expect(matcher.match(elements, values_to_match, @filter)).to eq [elements[0], elements[2]]
382
- end
383
- end
385
+ context 'when matching text Regexp' do
386
+ let(:elements) { [wd_element, wd_element] }
384
387
 
385
- # TODO: These should have the same tests for label locators, but the mocking is more complex
386
- # See equivalent tests in checkbox_spec
387
- context 'text_regexp deprecations' do
388
- let(:filter) { :first }
389
- let(:elements) { [wd_element, wd_element] }
388
+ before { allow(browser).to receive(:execute_script).and_return('all visible', 'some visible') }
390
389
 
391
- it 'not thrown when still matched by text content' do
392
- @values_to_match = {text: /some visible/}
393
- allow(browser).to receive(:execute_script).and_return('all visible', 'some visible')
390
+ it 'not thrown when still matched by text content' do
391
+ values_to_match = {text: /some visible/}
394
392
 
395
- expect(matcher.match(elements, values_to_match, filter)).to eq elements[1]
396
- end
393
+ expect(matcher.match(elements, values_to_match, :first)).to eq elements[1]
394
+ end
397
395
 
398
- it 'not thrown with complex regexp matched by text content' do
399
- @values_to_match = {text: /some (in|)visible/}
400
- allow(browser).to receive(:execute_script).and_return('all visible', 'some visible')
396
+ it 'not thrown with complex regexp matched by text content' do
397
+ values_to_match = {text: /some (in|)visible/}
401
398
 
402
- expect(matcher.match(elements, values_to_match, filter)).to eq elements[1]
403
- end
399
+ expect(matcher.match(elements, values_to_match, :first)).to eq elements[1]
400
+ end
404
401
 
405
- it 'thrown when no longer matched by text content' do
406
- @values_to_match = {text: /some visible$/}
407
- allow(browser).to receive(:execute_script).and_return('all visible', 'some visible')
408
- allow(browser).to receive(:timer).and_return(Watir::Wait::Timer.new)
409
- allow(browser).to receive(:timer=).and_return(Watir::Wait::Timer.new)
402
+ it 'thrown when no longer matched by text content' do
403
+ values_to_match = {text: /some visible$/}
410
404
 
411
- expect(matcher.match(elements, values_to_match, filter)).to eq elements[1]
412
- end
405
+ expect(matcher.match(elements, values_to_match, :first)).to eq elements[1]
406
+ end
413
407
 
414
- it 'not thrown when element does not exist' do
415
- @values_to_match = {text: /definitely not there/}
416
- allow(browser).to receive(:execute_script).and_return('all visible', 'some visible')
408
+ it 'not thrown when element does not exist' do
409
+ values_to_match = {text: /definitely not there/}
417
410
 
418
- expect(matcher.match(elements, values_to_match, filter)).to eq nil
419
- end
411
+ expect(matcher.match(elements, values_to_match, :first)).to be_nil
412
+ end
420
413
 
421
- # NOTE: This will work after:text_regexp deprecation removed
422
- it 'keeps element from being located' do
423
- @values_to_match = {text: /some visible some hidden/}
424
- allow(browser).to receive(:execute_script).and_return('all visible', 'some visible')
414
+ it 'keeps element from being located' do
415
+ values_to_match = {text: /some visible some hidden/}
425
416
 
426
- expect(matcher.match(elements, values_to_match, filter)).to be_nil
417
+ expect(matcher.match(elements, values_to_match, :first)).to be_nil
418
+ end
419
+ end
420
+ end
427
421
  end
428
422
  end
429
423
  end