watir-formhandler 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,97 @@
1
+ <html>
2
+ <head>
3
+ <title>Example Form</title>
4
+ </head>
5
+ <body>
6
+ <div id="main_content">
7
+ <label for='checkbox'>Checkbox</label>
8
+ <input id="checkbox" type="checkbox" /><br />
9
+ <br />
10
+
11
+ <label for="select">Select</label>
12
+ <select id="select">
13
+ <option value="test">Test</option>
14
+ <option value="test2">Test2</option>
15
+ </select><br />
16
+ <br />
17
+
18
+ <label for="multiselect">Multi Select</label>
19
+ <select multiple id="multiselect">
20
+ <option value="option1">Option1</option>
21
+ <option value="option2">Option2</option>
22
+ <option value="option3">Option3</option>
23
+ </select><br />
24
+ <br />
25
+
26
+ <label for="text">Text Field</label>
27
+ <input id="text" type="text" /><br />
28
+ <br />
29
+
30
+ <label for="textarea">Text Area</label>
31
+ <textarea id="textarea"></textarea><br />
32
+ <br />
33
+ <label for="radio">Radio<input type="radio" id="radio" value="test" /></label><br />
34
+ <br />
35
+
36
+ <label for="date">Date</label>
37
+ <input id="date" type="date" />
38
+ <br />
39
+ <br />
40
+
41
+ <label for="file">File</label>
42
+ <input type="file" id="file">
43
+ <br />
44
+ <br />
45
+
46
+ <fieldset id="checkboxset">
47
+ <label for="checkbox1">
48
+ <input type="checkbox" id="checkbox1">Checkbox1
49
+ </label><br />
50
+ <label for="checkbox2">
51
+ <input type="checkbox" id="checkbox2">Checkbox2
52
+ </label><br />
53
+ <label for="checkbox3">
54
+ <input type="checkbox" id="checkbox3">Checkbox3
55
+ </label><br />
56
+ <label for="checkbox4">
57
+ <input type="checkbox" id="checkbox4">Checkbox4
58
+ </label><br />
59
+ </fieldset><br />
60
+
61
+ <fieldset id="radioset" class="radioset">
62
+ <label for="radio1">
63
+ <input type="radio" name="radioset" id="radio1">Radio1
64
+ </label><br />
65
+ <label for="radio2">
66
+ <input type="radio" name="radioset" id="radio2">Radio2
67
+ </label><br />
68
+ <label for="radio3">
69
+ <input type="radio" name="radioset" id="radio3">Radio3
70
+ </label><br />
71
+ <label for="radio4">
72
+ <input type="radio" name="radioset" id="radio4">Radio4
73
+ </label>
74
+ </fieldset>
75
+
76
+ <fieldset id="radio_and_checkbox">
77
+ <label for="checkbox5">
78
+ <input type="checkbox" id="checkbox5">Checkbox5
79
+ </label><br />
80
+ <label for="checkbox6">
81
+ <input type="checkbox" id="checkbox6">Checkbox6
82
+ </label><br />
83
+ <label for="radio5">
84
+ <input type="radio" name="radioset2" id="radio5">Radio5
85
+ </label><br />
86
+ <label for="radio6">
87
+ <input type="radio" name="radioset2" id="radio6">Radio6
88
+ </label><br />
89
+ <label for="radio7">
90
+ <input type="radio" name="radioset2" id="radio7">Radio7
91
+ </label>
92
+ </fieldset>
93
+
94
+ </div>
95
+ </body>
96
+
97
+ </html>
@@ -0,0 +1,310 @@
1
+ require 'spec_helper'
2
+
3
+ module Watir
4
+ describe OptionGroup do
5
+ before(:all) { @browser = Watir::Browser.new }
6
+ after(:all) { @browser.close}
7
+ let(:browser){ @browser }
8
+
9
+ before(:each) { browser.goto(local_url(FORM_PAGE)) }
10
+
11
+ describe 'behaving like a normal Watir::Element' do
12
+ describe '#exists? should return' do
13
+ it 'true if it is present in the DOM' do
14
+ expect(browser.option_group(id: 'radioset').exist?).to eq(true)
15
+ end
16
+
17
+ it 'false otherwise' do
18
+ expect(browser.option_group(id: 'nowhere').exist?).to eq(false)
19
+ end
20
+ end
21
+ end
22
+
23
+
24
+ describe '#option_names' do
25
+ it 'gives the names of all available checkboxes' do
26
+ expected_options = %w(Checkbox1 Checkbox2 Checkbox3 Checkbox4)
27
+ expect(browser.option_group(id: 'checkboxset').option_names).to include(*expected_options)
28
+ end
29
+
30
+ it 'gives the names of all available radio buttons' do
31
+ expected_options = %w(Radio1 Radio2 Radio3 Radio4)
32
+ expect(browser.option_group(id: 'radioset').option_names).to include(*expected_options)
33
+ end
34
+
35
+ it 'gives the names of all available radio buttons and checkboxes' do
36
+ expected_options = %w(Checkbox5 Checkbox6 Radio5 Radio6 Radio7)
37
+ option_group = browser.option_group(id: 'radio_and_checkbox')
38
+ expect(option_group.option_names).to include(*expected_options)
39
+ end
40
+ end
41
+
42
+
43
+ describe '#option_fields' do
44
+ it 'provides all fields belonging to an OptionGroup of checkboxes' do
45
+ expected_ids = %w(checkbox1 checkbox2 checkbox3 checkbox4)
46
+ option_group = browser.option_group(id: 'checkboxset')
47
+ expect(option_group.option_fields.map(&:id)).to include(*expected_ids)
48
+ end
49
+
50
+ it 'provides all fields belonging to an OptionGroup of radio buttons' do
51
+ expected_ids = %w(radio1 radio2 radio3 radio4)
52
+ option_group = browser.option_group(id: 'radioset')
53
+ expect(option_group.option_fields.map(&:id)).to include(*expected_ids)
54
+ end
55
+
56
+ it 'provides all fields belonging to an OptionGroup of radio buttons and checkboxes' do
57
+ expected_ids = %w(checkbox5 checkbox6 radio5 radio6 radio7)
58
+ option_group = browser.option_group(id: 'radio_and_checkbox')
59
+ expect(option_group.option_fields.map(&:id)).to include(*expected_ids)
60
+ end
61
+ end
62
+
63
+
64
+ describe '#options' do
65
+ it 'gives a hash with all available checkboxes and their labels' do
66
+ option_hash = {'Checkbox1' => 'checkbox1',
67
+ 'Checkbox2' => 'checkbox2',
68
+ 'Checkbox3' => 'checkbox3',
69
+ 'Checkbox4' => 'checkbox4'
70
+ }
71
+ found_options = browser.option_group(id: 'checkboxset').options
72
+
73
+ expect(found_options).to be_a(Hash)
74
+ expect(found_options.keys).to eq(option_hash.keys)
75
+ expect(found_options.values.map(&:id)).to eq(option_hash.values)
76
+ end
77
+
78
+ it 'gives a hash with all available radio buttons and their labels' do
79
+ option_hash = {'Radio1' => 'radio1',
80
+ 'Radio2' => 'radio2',
81
+ 'Radio3' => 'radio3',
82
+ 'Radio4' => 'radio4'
83
+ }
84
+ found_options = browser.option_group(id: 'radioset').options
85
+
86
+ expect(found_options).to be_a(Hash)
87
+ expect(found_options.keys).to eq(option_hash.keys)
88
+ expect(found_options.values.map(&:id)).to eq(option_hash.values)
89
+ end
90
+
91
+ it 'gives a hash with all available radio buttons, checkboxes and their labels' do
92
+ option_hash = { 'Checkbox5' => 'checkbox5',
93
+ 'Checkbox6' => 'checkbox6',
94
+ 'Radio5' => 'radio5',
95
+ 'Radio6' => 'radio6',
96
+ 'Radio7' => 'radio7',
97
+ }
98
+ found_options = browser.option_group(id: 'radio_and_checkbox').options
99
+
100
+ expect(found_options).to be_a(Hash)
101
+ expect(found_options.keys).to eq(option_hash.keys)
102
+ expect(found_options.values.map(&:id)).to eq(option_hash.values)
103
+ end
104
+ end
105
+
106
+
107
+ describe '#set' do
108
+ let(:option_group) { @option_group }
109
+
110
+ context 'without preselected elements' do
111
+ context 'for OptionGroup only containing checkboxes' do
112
+ before(:each){ @option_group = browser.option_group(id: 'checkboxset') }
113
+
114
+ it 'clicks a single option' do
115
+ target_option = browser.checkbox(id: 'checkbox1')
116
+ option_group.set('Checkbox1')
117
+ expect(target_option).to be_checked
118
+ end
119
+
120
+ it 'clicks multiple options' do
121
+ target_options = browser.element(id: 'checkboxset').checkboxes[0..1]
122
+ option_group.set('Checkbox1', 'Checkbox2')
123
+ target_options.each{ |option| expect(option).to be_checked }
124
+ end
125
+
126
+ it 'click multiple options given as array' do
127
+ target_options = browser.element(id: 'checkboxset').checkboxes[0..1]
128
+ option_group.set(%w(Checkbox1 Checkbox2))
129
+ target_options.each{ |option| expect(option).to be_checked }
130
+ end
131
+ end
132
+
133
+
134
+ context 'for OptionGroup only containing radio buttons' do
135
+ before(:each){ @option_group = browser.option_group(id: 'radioset') }
136
+
137
+ it 'clicks a single option' do
138
+ target_option = browser.radio(id: 'radio1')
139
+ option_group.set('Radio1')
140
+ expect(target_option).to be_checked
141
+ end
142
+
143
+ it 'clicks multiple options' do
144
+ target_option = browser.radio(id: 'radio3')
145
+ option_group.set('Radio1', 'Radio2', 'Radio3')
146
+ expect(target_option).to be_checked
147
+ end
148
+
149
+ it 'click multiple options given as array' do
150
+ target_option = browser.radio(id: 'radio3')
151
+ option_group.set(%w(Radio1 Radio2 Radio3))
152
+ expect(target_option).to be_checked
153
+ end
154
+ end
155
+
156
+
157
+ context 'for OptionGroup containing checkboxes and radio buttons' do
158
+ before(:each){ @option_group = browser.option_group(id: 'radio_and_checkbox') }
159
+
160
+ it 'clicks a single option' do
161
+ target_option = browser.checkbox(id: 'checkbox5')
162
+ option_group.set('Checkbox5')
163
+ expect(target_option).to be_checked
164
+ end
165
+
166
+ it 'clicks multiple options' do
167
+ target_options = browser.element(id: 'radio_and_checkbox').checkboxes[0..1].to_a
168
+ target_options << browser.element(id: 'radio_and_checkbox').radios[0]
169
+ option_group.set('Checkbox5', 'Checkbox6', 'Radio5')
170
+ target_options.each { |option| expect(option).to be_checked }
171
+ end
172
+
173
+ it 'click multiple options given as array' do
174
+ target_options = browser.element(id: 'radio_and_checkbox').checkboxes[0..1].to_a
175
+ target_options << browser.element(id: 'radio_and_checkbox').radios[0]
176
+ option_group.set(%w(Checkbox5 Checkbox6 Radio5))
177
+ target_options.each { |option| expect(option).to be_checked }
178
+ end
179
+ end
180
+ end
181
+
182
+
183
+ context 'with preselected elements' do
184
+ context 'for OptionGroup only containing checkboxes' do
185
+ before(:each){ @option_group = browser.option_group(id: 'checkboxset') }
186
+ let(:fieldset) { browser.element(id: 'checkboxset') }
187
+ let(:preselected) { fieldset.checkboxes[2..3] }
188
+ before(:each) { preselected.map(&:click) }
189
+
190
+ it 'selects a single option and deselects any other' do
191
+ target_option = browser.element(id: 'checkboxset').checkboxes.first
192
+ option_group.set('Checkbox1')
193
+ preselected.each{ |option| expect(option).to_not be_checked }
194
+ expect(target_option).to be_checked
195
+ end
196
+
197
+ it 'selects multiple options and deselect unwanted ones' do
198
+ target_options = browser.element(id: 'checkboxset').checkboxes[0..1]
199
+ option_group.set('Checkbox1', 'Checkbox2')
200
+ preselected.each{ |option| expect(option).to_not be_checked }
201
+ target_options.each{ |option| expect(option).to be_checked }
202
+ end
203
+
204
+ it 'performs no clicks if preselected elements equal desired ones' do
205
+ preselected.each{ |option| expect(option).to_not receive(:click) }
206
+ option_group.set('Checkbox3', 'Checkbox4')
207
+ end
208
+ end
209
+
210
+
211
+ context 'for OptionGroup only containing radio buttons' do
212
+ before(:each){ @option_group = browser.option_group(id: 'radioset') }
213
+ let(:fieldset) { browser.element(id: 'radioset') }
214
+ let(:preselected) { fieldset.radios[1] }
215
+ before(:each) { preselected.click }
216
+
217
+ it 'selects a single option and deselects any other' do
218
+ target_option = browser.element(id: 'radioset').radios.first
219
+ sleep 2
220
+ option_group.set('Radio1')
221
+ sleep 2
222
+ expect(preselected).to_not be_checked
223
+ expect(target_option).to be_checked
224
+ end
225
+
226
+ it 'selects multiple options and deselect unwanted ones' do
227
+ target_option = browser.element(id: 'radioset').radios[3]
228
+ option_group.set('Radio3', 'Radio4')
229
+ expect(preselected).to_not be_checked
230
+ expect(target_option).to be_checked
231
+ end
232
+
233
+ it 'performs no clicks if preselected elements equal desired ones' do
234
+ expect(preselected).to_not receive(:click)
235
+ option_group.set('Radio2')
236
+ end
237
+ end
238
+
239
+
240
+ context 'for OptionGroup containing checkboxes and radio buttons' do
241
+ before(:each){ @option_group = browser.option_group(id: 'radio_and_checkbox') }
242
+ let(:fieldset) { browser.element(id: 'radio_and_checkbox') }
243
+ let(:preselected) { fieldset.inputs[1..2].map(&:to_subtype) }
244
+ before(:each) { preselected.map(&:click) }
245
+
246
+ it 'selects a single option and deselects any other' do
247
+ target_option = fieldset.checkboxes.first
248
+ option_group.set('Checkbox5')
249
+ expect(preselected.first).to_not be_checked
250
+ expect(preselected.last).to be_checked
251
+ expect(target_option).to be_checked
252
+ end
253
+
254
+ it 'selects multiple options and deselect unwanted ones' do
255
+ target_options = [fieldset.checkboxes.first, fieldset.radios.last]
256
+ option_group.set('Checkbox5', 'Radio7')
257
+ preselected.each{ |option| expect(option).to_not be_checked }
258
+ target_options.each{ |option| expect(option).to be_checked }
259
+ end
260
+
261
+ it 'performs no clicks if preselected elements equal desired ones' do
262
+ preselected.each{ |option| expect(option).to_not receive(:click) }
263
+ option_group.set('Checkbox6', 'Radio5')
264
+ end
265
+ end # group with radio and checkbox
266
+ end # with preselected elements
267
+ end # #set
268
+
269
+
270
+ describe '#selected_options' do
271
+ it 'returns the selected options of a group' do
272
+ option_group = browser.option_group(id: 'radio_and_checkbox')
273
+ option_group.set(%w(Checkbox5 Radio7))
274
+ expect(option_group.selected_options).to eq(%w(Checkbox5 Radio7))
275
+ end
276
+
277
+ it 'returns empty array if nothing is selected' do
278
+ option_group = browser.option_group(id: 'radio_and_checkbox')
279
+ expect(option_group.selected_options).to be_empty
280
+ end
281
+ end #selected_options
282
+
283
+
284
+ describe '#field_value' do
285
+ subject { OptionGroup.new('', id: '') }
286
+
287
+ context 'no option is selected' do
288
+ it 'returns an empty array' do
289
+ allow(subject).to receive(:selected_options).and_return([])
290
+ expect(subject.field_value).to eq([])
291
+ end
292
+ end
293
+
294
+ context 'one option is selected' do
295
+ it 'returns an array with one element' do
296
+ allow(subject).to receive(:selected_options).and_return(['Option1'])
297
+ expect(subject.field_value).to eq(['Option1'])
298
+ end
299
+ end
300
+
301
+ context 'several options are selected' do
302
+ it 'returns an array with one element' do
303
+ opts = %w(Option1 Option3)
304
+ allow(subject).to receive(:selected_options).and_return(opts)
305
+ expect(subject.field_value).to eq(opts)
306
+ end
307
+ end
308
+ end #field_value
309
+ end
310
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ module Watir
4
+ describe Radio do
5
+ before(:each) { @radio = Watir::Radio.new('', id: '') }
6
+ let(:radio) { @radio }
7
+
8
+ describe '#set' do
9
+ context 'when given true' do
10
+ it 'clicks it if unchecked' do
11
+ allow(radio).to receive(:checked?).and_return(false)
12
+ expect(radio).to receive(:click)
13
+ radio.set(true)
14
+ end
15
+
16
+ it 'does not click it if checked' do
17
+ allow(radio).to receive(:checked?).and_return(true)
18
+ expect(radio).to_not receive(:click)
19
+ radio.set(true)
20
+ end
21
+ end
22
+
23
+
24
+ context 'when given false' do
25
+ it 'does not click it if checked' do
26
+ allow(radio).to receive(:checked?).and_return(true)
27
+ expect(radio).to_not receive(:click)
28
+ radio.set(false)
29
+ end
30
+
31
+ it 'does not click it if unchecked' do
32
+ allow(radio).to receive(:checked?).and_return(false)
33
+ expect(radio).to_not receive(:click)
34
+ radio.set(false)
35
+ end
36
+ end # context when given false
37
+ end # #set
38
+
39
+
40
+ describe '#field_value' do
41
+ context 'if checked' do
42
+ it 'returns true' do
43
+ allow(radio).to receive(:checked?).and_return(true)
44
+ expect(radio.field_value).to eq(true)
45
+ end
46
+ end
47
+ context 'if unchecked' do
48
+ it 'returns false' do
49
+ allow(radio).to receive(:checked?).and_return(false)
50
+ expect(radio.field_value).to eq(false)
51
+ end
52
+ end
53
+ end # #read
54
+ end
55
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ module Watir
4
+ describe Select do
5
+ before(:all) { @browser = Browser.new }
6
+ after(:all) { @browser.close }
7
+ before(:each) { @browser.goto(local_url(FORM_PAGE)) }
8
+ let(:select) { @select }
9
+
10
+ describe '#set' do
11
+
12
+ context 'single select' do
13
+ it 'selects the given option' do
14
+ select = @browser.select(id: 'select')
15
+ select.set('Test2')
16
+ expect(select.selected_options.first.text).to eq('Test2')
17
+ end
18
+ end
19
+
20
+
21
+ context 'multiple select' do
22
+ before(:each) { @select = @browser.select(id: 'multiselect') }
23
+ it 'should select a single option' do
24
+ select.set('Option2')
25
+ expect(select.selected_options.count).to eq(1)
26
+ expect(select.selected_options.first.text).to eq('Option2')
27
+ end
28
+
29
+ it 'should select multiple options' do
30
+ select.set('Option2', 'Option3')
31
+ expect(select.selected_options.count).to eq(2)
32
+ expect(select.selected_options.map(&:text)).to eq(['Option2', 'Option3'])
33
+ end
34
+
35
+ it 'should select multiple options from an array' do
36
+ select.set(['Option2', 'Option3'])
37
+ expect(select.selected_options.count).to eq(2)
38
+ expect(select.selected_options.map(&:text)).to eq(['Option2', 'Option3'])
39
+ end
40
+ end # context multiple select
41
+ end # #set
42
+
43
+
44
+ describe '#field_value' do
45
+ context 'one option is selected' do
46
+ subject { @browser.select(id: 'select') }
47
+ it 'returns a string' do
48
+ expect(subject.field_value).to eq('Test')
49
+ end
50
+ end
51
+
52
+ context 'multiple options are selected' do
53
+ subject { @browser.select(id: 'multiselect') }
54
+ it 'returns a list of selected values' do
55
+ opts = %w(Option1 Option3)
56
+ subject.set(opts)
57
+ expect(subject.field_value.count).to eq(2)
58
+ expect(subject.field_value).to eq(opts)
59
+ end
60
+ end
61
+ end #field_value
62
+ end
63
+ end
@@ -0,0 +1,16 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'watir'
5
+ require 'watir-webdriver'
6
+ require 'selenium-webdriver'
7
+ require 'rspec'
8
+ require 'watir_formhandler'
9
+ require 'pry'
10
+
11
+ HTML_DIR = File.join(File.dirname(File.expand_path(__FILE__)), 'html')
12
+ FORM_PAGE = 'form.html'
13
+
14
+ def local_url(page)
15
+ File.join('file://', HTML_DIR, page)
16
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ module Watir
4
+ describe TextArea do
5
+ describe '#field_value' do
6
+ subject { Watir::TextArea.new('', id: '') }
7
+
8
+ context 'has text' do
9
+ it 'returns the set text' do
10
+ allow(subject).to receive(:value).and_return('test text')
11
+ expect(subject.field_value).to eq('test text')
12
+ end
13
+ end
14
+
15
+ context 'empty' do
16
+ it 'returns empty string'do
17
+ allow(subject).to receive(:value).and_return('')
18
+ expect(subject.field_value).to eq('')
19
+ end
20
+ end
21
+
22
+ context 'unset' do
23
+ it 'returns empty string'do
24
+ allow(subject).to receive(:value).and_return(nil)
25
+ expect(subject.field_value).to eq('')
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ module Watir
4
+ describe TextField do
5
+ describe '#field_value' do
6
+ subject { Watir::TextField.new('', id: '') }
7
+
8
+ context 'has text' do
9
+ it 'returns the set text' do
10
+ allow(subject).to receive(:value).and_return('test text')
11
+ expect(subject.field_value).to eq('test text')
12
+ end
13
+ end
14
+
15
+ context 'empty' do
16
+ it 'returns empty string'do
17
+ allow(subject).to receive(:value).and_return('')
18
+ expect(subject.field_value).to eq('')
19
+ end
20
+ end
21
+
22
+ context 'unset' do
23
+ it 'returns empty string'do
24
+ allow(subject).to receive(:value).and_return(nil)
25
+ expect(subject.field_value).to eq('')
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Watir do
4
+ describe 'extend_tag_to_class' do
5
+ TestClass = Class.new
6
+ it 'should add a new tag-class combination to the original hash' do
7
+ Watir.extend_tag_to_class(:test, TestClass)
8
+ expect(Watir.tag_to_class.keys).to include(:test)
9
+ expect(Watir.tag_to_class[:test]).to be_a(Class)
10
+ end
11
+
12
+ it 'should freeze the tag_to_class hash again after the addition' do
13
+ Watir.extend_tag_to_class(:test, TestClass)
14
+ expect(Watir.tag_to_class).to be_frozen
15
+ end
16
+ end
17
+ end