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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b92e73452df613d4cebbab78ba085259a504b00e
4
+ data.tar.gz: 078a6861a8b11e88e9c1675c3b7c877d1991b062
5
+ SHA512:
6
+ metadata.gz: 12d2941e5e8b6f3de215eaa2e5bc8088e6629c7f8dc3c6b452d83aeb1475b4ae94c614b50942f085ba973c10e40491aea575b53262708b759d9956692802b3c5
7
+ data.tar.gz: 0231e8ffe1c62f472da7626c69ce5821b0a552c1dcd19d7b9353d7a9e2435d1b5b8e194bdeaab8cb5ad8ab2d2367ea45181f273c0c63586ce61b52fc8ac3d2ba
@@ -0,0 +1,19 @@
1
+ module Watir
2
+ require 'watir-webdriver/elements/checkbox'
3
+ class CheckBox
4
+
5
+ # Will click the CheckBox unless the #checked? returns the same value as the given one.
6
+ # @param [Boolean] value the value to achieve on the CheckBox.
7
+ # @return [Boolean] the new value of this CheckBox.
8
+ def set(value)
9
+ click unless !!value == checked?
10
+ end
11
+
12
+
13
+ # Returns whether this CheckBox is set or not.
14
+ # @return [Boolean] is checked?
15
+ def field_value
16
+ checked?
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,49 @@
1
+ module Watir
2
+ module Container
3
+ # Searches for the specified label and returns the form field belonging to it, identified by the
4
+ # 'for' attribute of the label. Alternatively, you may pass a Watir::Label.
5
+ # @param [String, Watir::Label] label the label for which to find the form field.
6
+ # @param [Watir::Element] start_node the node where to start searching for the label.
7
+ # @param [Boolean] include_groups whether to detect the group of a given label.
8
+ # @return [Watir::Element] form field of the given label.
9
+ def field(label, start_node: nil, include_groups: false)
10
+ start_node ||= self
11
+ field_label = label.respond_to?(:for) ? label : start_node.label(text: label)
12
+ determine_field(start_node, field_label, include_groups)
13
+ end
14
+
15
+
16
+ # Fills in the given value(s) to the passed attribute. It therefore accepts the same parameters
17
+ # as the #field method.
18
+ # @param [String, Watir::Label] label the label for which to find the form field.
19
+ # @param [Watir::Element] start_node the node where to start searching for the label.
20
+ # @param [String, Boolean, Array] value to be set.
21
+ # @param [Boolean] include_groups whether to detect the group of a given label.
22
+ def fill_in(label, value, start_node: nil, include_groups: nil)
23
+ field(label, start_node: start_node, include_groups: include_groups).set(value)
24
+ end
25
+
26
+
27
+ # Returns an OptionGroup
28
+ # @return [OptionGroup] the selected OptionGroup
29
+ def option_group(*args)
30
+ selector = args.first.respond_to?(:elements) ? args.first : extract_selector(args)
31
+ OptionGroup.new(self, selector)
32
+ end
33
+ Watir.extend_tag_to_class(:option_group, OptionGroup)
34
+
35
+
36
+
37
+ private
38
+ def determine_field(start_node, label, include_groups)
39
+ if include_groups
40
+ group_member_count = label.parent.checkboxes.count + label.parent.radios.count
41
+ return option_group(label.parent) if group_member_count > 1
42
+ end
43
+
44
+ field_id = label.for
45
+ found_field = start_node.element(id: field_id)
46
+ found_field ? found_field.to_subtype : nil
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ module Watir
2
+ class Element
3
+ attr_reader :selector
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ module Watir
2
+ class FileField
3
+ # Returns the file name of this FileField as a string.
4
+ # @return [String] file name.
5
+ def field_value
6
+ value || ''
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,91 @@
1
+ module Watir
2
+
3
+ # The OptionGroup represents the parent node of any grouped checkboxes or radio buttons. Its
4
+ # purpose is to provide a way to select different options in a more natural way, like the user
5
+ # would do it: instead of going through all checkboxes one by one and setting them to 'true',
6
+ # OptionGroup allows to specify the option names that are desired to be opted in.
7
+ # @example
8
+ # group = browser.option
9
+ class OptionGroup < HTMLElement
10
+ # Allows selector to be an HTMLElement, in which case the internal element will be set to this
11
+ # HTMLElement node.
12
+ def initialize(parent, selector)
13
+ stripped_selector = selector.respond_to?(:selector) ? selector.selector : selector
14
+ super parent, stripped_selector
15
+
16
+ @element = selector if selector.respond_to?(:selector)
17
+ end
18
+
19
+
20
+ # Returns the names of all available options.
21
+ # @return [Array<String>] names of all options.
22
+ def option_names
23
+ self.labels.map{ |label| label.text.strip }
24
+ end
25
+
26
+
27
+ # Returns the fields of all available options.
28
+ # @return [Array<Element>] fields of all options.
29
+ def option_fields
30
+ self.checkboxes.to_a + self.radios.to_a
31
+ end
32
+
33
+
34
+ # Returns all available options fields and their respective label as a Hash.
35
+ # @return [Hash<label => field>] hash with all labels and fields.
36
+ def options
37
+ option_hash = {}
38
+ my_labels = option_names
39
+ my_inputs = option_fields
40
+
41
+ my_labels.count.times do |index|
42
+ option_hash[my_labels[index]] = my_inputs[index]
43
+ end
44
+ option_hash
45
+ end
46
+
47
+
48
+ # Selects the given option(s) and deselects all other ones. This can not be done with
49
+ # radio buttons, however, as they cannot be deselected.
50
+ # @param [String, Array<String>] wanted_options to be selected.
51
+ # @example Passing a single String
52
+ # group.set('Checkbox1') #=> selects 'Checkbox1'
53
+ # @example Passing several options
54
+ # group.set('Checkbox1', 'Checkbox2') #=> selects 'Checkbox1' and 'Checkbox2'
55
+ # @example Passing several options as Array
56
+ # group.set(['Checkbox1', 'Radio1']) #=> selects 'Checkbox1' and 'Radio1'
57
+ def set(*wanted_options)
58
+ options_to_select = [*wanted_options].flatten
59
+ options_to_deselect = option_names - options_to_select
60
+
61
+ @options = options
62
+ select(options_to_select, true)
63
+ select(options_to_deselect, false)
64
+ @options = nil
65
+ end
66
+
67
+
68
+ # Returns the selected options of this OptionGroup.
69
+ # @return [Array<String>] the selected options.
70
+ def selected_options
71
+ selected = []
72
+ my_labels = option_names
73
+ inputs.each_with_index do |field, index|
74
+ selected << my_labels[index] if field.checked?
75
+ end
76
+ selected
77
+ end
78
+
79
+
80
+ # @see #selected_options
81
+ def field_value
82
+ selected_options
83
+ end
84
+
85
+
86
+ private
87
+ def select(options_to_select, value_to_set)
88
+ options_to_select.each{ |option| @options[option].set(value_to_set) }
89
+ end
90
+ end # OptionGroup
91
+ end # Watir
@@ -0,0 +1,18 @@
1
+ module Watir
2
+ require 'watir-webdriver/elements/radio'
3
+ class Radio
4
+ # Clicks this Radio unless #checked? returns the same value as the given one.
5
+ # @param [Boolean] value to be set.
6
+ # @return nil
7
+ def set(value)
8
+ click unless checked? || !value
9
+ end
10
+
11
+
12
+ # Returns whether this CheckBox is set or not.
13
+ # @return [Boolean] is checked?
14
+ def field_value
15
+ checked?
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,34 @@
1
+ module Watir
2
+ require 'watir-webdriver/elements/select'
3
+ class Select
4
+ # Selects the given option(s) of this Select. If this Select has the 'multiple' attribute, it
5
+ # accept an array, otherwise it will call the internal '#select' method.
6
+ # @param [String, Array<String>] values the values to be selected.
7
+ # @return [String, Array<String>] the selected values.
8
+ def set(*values)
9
+ if multiple?
10
+ select_multiple(*values)
11
+ else
12
+ select(values.first)
13
+ end
14
+ end
15
+
16
+
17
+ # Selects the given options of this Select.
18
+ # @param [Array<String>] values to be selected.
19
+ # @return [Array<String>] the selected values.
20
+ def select_multiple(*values)
21
+ clear
22
+ values.flatten.each{ |value| select(value) }
23
+ end
24
+
25
+
26
+ # Selected option(s) of this Select.
27
+ # @return [String, Array<String>] if only one option is selected, return it as string,
28
+ # otherwise, return it as an array of strings.
29
+ def field_value
30
+ opts = selected_options
31
+ opts.count == 1 ? opts.first.text : opts.map(&:text)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,10 @@
1
+ module Watir
2
+ class TextArea
3
+ # Returns the text currently set in this TextField
4
+ # @return [String] the current text in the field. If the field is not set at all, it will return
5
+ # and empty string.
6
+ def field_value
7
+ value || ''
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Watir
2
+ class TextField
3
+ # Returns the text currently set in this TextField
4
+ # @return [String] the current text in the field. If the field is not set at all, it will return
5
+ # and empty string.
6
+ def field_value
7
+ value || ''
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ module Watir
2
+ # As the tag_to_class hash is frozen once Watir is done loading its base classes, it cannot be
3
+ # extended with custom classes. Therefore, extend_tag_to_class unfreezes it, by replacing it with
4
+ # a duplicate and freezing it again after the addition.
5
+ #
6
+ # @param [Symbol] tag the tag to add to the list.
7
+ # @param [Element] tag_class the element class to return for the given tag.
8
+ # @return [Hash] the new tag-class hash.
9
+ def self.extend_tag_to_class(tag, tag_class)
10
+ @tag_to_class = @tag_to_class.dup.merge(tag => tag_class)
11
+ @tag_to_class.freeze
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ require 'watir'
2
+ require 'watir-webdriver'
3
+ require 'selenium-webdriver'
4
+
5
+ require 'watir_formhandler/watir'
6
+ require 'watir_formhandler/element'
7
+ require 'watir_formhandler/option_group'
8
+ require 'watir_formhandler/container'
9
+ require 'watir_formhandler/check_box'
10
+ require 'watir_formhandler/select'
11
+ require 'watir_formhandler/radio'
12
+ require 'watir_formhandler/text_field'
13
+ require 'watir_formhandler/text_area'
14
+ require 'watir_formhandler/file_field'
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ module Watir
4
+ describe CheckBox do
5
+ before(:each) { @checkbox = Watir::CheckBox.new('', id: '') }
6
+ let(:checkbox) { @checkbox }
7
+
8
+ describe '#set' do
9
+
10
+ context 'when given true' do
11
+ it 'checks it if unchecked' do
12
+ allow(checkbox).to receive(:checked?).and_return(false)
13
+ expect(checkbox).to receive(:click)
14
+ checkbox.set(true)
15
+ end
16
+
17
+ it 'does not check it if checked' do
18
+ allow(checkbox).to receive(:checked?).and_return(true)
19
+ expect(checkbox).to_not receive(:click)
20
+ checkbox.set(true)
21
+ end
22
+ end
23
+
24
+
25
+ context 'when given false' do
26
+ it 'unchecks it if checked' do
27
+ allow(checkbox).to receive(:checked?).and_return(true)
28
+ expect(checkbox).to receive(:click)
29
+ checkbox.set(false)
30
+ end
31
+
32
+ it 'does not uncheck it if unchecked' do
33
+ allow(checkbox).to receive(:checked?).and_return(false)
34
+ expect(checkbox).to_not receive(:click)
35
+ checkbox.set(false)
36
+ end
37
+ end #context when given false
38
+ end # #set
39
+
40
+
41
+ describe '#field_value' do
42
+ context 'if checked' do
43
+ it 'returns true' do
44
+ allow(checkbox).to receive(:checked?).and_return(true)
45
+ expect(checkbox.field_value).to eq(true)
46
+ end
47
+ end
48
+ context 'if unchecked' do
49
+ it 'returns false' do
50
+ allow(checkbox).to receive(:checked?).and_return(false)
51
+ expect(checkbox.field_value).to eq(false)
52
+ end
53
+ end
54
+ end # #read
55
+ end
56
+ end
@@ -0,0 +1,242 @@
1
+ require 'spec_helper'
2
+
3
+ module Watir
4
+ describe Container 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 '#option_group' do
12
+ context 'using a selector' do
13
+ it "returns a Watir::OptionGroup for id: 'checkboxset'" do
14
+ expect(browser.option_group(id: 'checkboxset')).to be_a(Watir::OptionGroup)
15
+ end
16
+
17
+ it "returns a Watir::OptionGroup for class: 'radioset' selector" do
18
+ expect(browser.option_group(class: 'radioset')).to be_a(Watir::OptionGroup)
19
+ end
20
+ end
21
+
22
+ context 'using a node' do
23
+ it 'returns a Watir::OptionGroup for a given Watir::Element' do
24
+ fieldset = browser.fieldset(id: 'checkboxset')
25
+ expect(browser.option_group(fieldset).tag_name).to eq('fieldset')
26
+ end
27
+ end
28
+ end # #option_group
29
+
30
+
31
+ describe '#field' do
32
+ describe 'without a start node' do
33
+ it 'returns form field of the specified label string' do
34
+ expect(browser.field('Checkbox').id).to eq('checkbox')
35
+ end
36
+
37
+ it 'returns form field of the given label node' do
38
+ field_label = browser.label(text: 'Checkbox')
39
+ expect(browser.field(field_label).id).to eq('checkbox')
40
+ end
41
+ end
42
+
43
+
44
+ describe 'with a start node' do
45
+ context 'specifying the label with a string' do
46
+ it 'searches the label within given Watir::Element' do
47
+ field_label = double('label', for: 'checkbox')
48
+ start_node = double('enter_element', label: field_label)
49
+
50
+ expect(start_node).to receive(:label).with(text: 'Checkbox')
51
+ expect(start_node).to receive(:element).with(id: 'checkbox')
52
+ browser.field('Checkbox', start_node: start_node)
53
+ end
54
+
55
+ it 'returns form field of the label within Watir::Element' do
56
+ start_node = browser.element(id: 'main_content')
57
+ expect(browser.field('Checkbox', start_node: start_node).id).to eq('checkbox')
58
+ end
59
+ end
60
+
61
+
62
+ context 'specifying the label with a Watir::Label' do
63
+ it 'searches the label within given Watir::Element' do
64
+ field_label = double('label', for: 'checkbox')
65
+ start_node = double('enter_element')
66
+
67
+ expect(start_node).to receive(:element).with(id: 'checkbox')
68
+ browser.field(field_label, start_node: start_node)
69
+ end
70
+
71
+ it 'returns form field of the label within Watir::Element' do
72
+ start_node = browser.element(id: 'main_content')
73
+ field_label = browser.label(text: 'Checkbox')
74
+ expect(browser.field(field_label, start_node: start_node).id).to eq('checkbox')
75
+ end
76
+ end
77
+ end
78
+
79
+
80
+ describe 'depending on subtype' do
81
+ context 'if a single field is in the area' do
82
+ it 'returns a Watir::Checkbox for a checkbox' do
83
+ expect(browser.field('Checkbox')).to be_a(Watir::CheckBox)
84
+ end
85
+
86
+ it 'returns a Watir::Select for a select' do
87
+ expect(browser.field('Select')).to be_a(Watir::Select)
88
+ end
89
+
90
+ it 'returns a Watir::TextField for a text field' do
91
+ expect(browser.field('Text Field')).to be_a(Watir::TextField)
92
+ end
93
+
94
+ it 'returns a Watir::TextArea for a text area' do
95
+ expect(browser.field('Text Area')).to be_a(Watir::TextArea)
96
+ end
97
+
98
+ it 'returns a Watir::Radio for a radio button' do
99
+ expect(browser.field('Radio')).to be_a(Watir::Radio)
100
+ end
101
+
102
+ it 'returns a Watir::FileField for a file field' do
103
+ expect(browser.field('File')).to be_a(Watir::FileField)
104
+ end
105
+ end
106
+
107
+ context 'if fields are grouped' do
108
+ it 'returns Watir::OptionGroup for grouped checkboxes' do
109
+ expect(browser.field('Checkbox1', include_groups: true)).to be_a(Watir::OptionGroup)
110
+ end
111
+
112
+ it 'returns Watir::OptionGroup for grouped radio buttons' do
113
+ expect(browser.field('Radio1', include_groups: true)).to be_a(Watir::OptionGroup)
114
+ end
115
+
116
+ it 'returns Watir::OptionGroup for grouped chckboxes and radio buttons' do
117
+ expect(browser.field('Checkbox5', include_groups: true)).to be_a(Watir::OptionGroup)
118
+ end
119
+ end
120
+ end # describe sybtype
121
+ end # #field
122
+
123
+
124
+ describe '#fill_in' do
125
+ describe 'without a start node' do
126
+ it 'fills a Watir::Checkbox' do
127
+ browser.fill_in('Checkbox', true)
128
+ expect(browser.checkboxes.first).to be_checked
129
+ end
130
+
131
+ it 'fills a Watir::Radio' do
132
+ browser.fill_in('Radio', true)
133
+ expect(browser.radios.first).to be_checked
134
+ end
135
+
136
+ it 'fills a Watir::FileField' do
137
+ browser.fill_in('File', File.join(HTML_DIR, FORM_PAGE))
138
+ expect(browser.file_fields.first.value).to eq(FORM_PAGE)
139
+ end
140
+
141
+ it 'fills a Watir::TextField' do
142
+ browser.fill_in('Text Field', 'test text')
143
+ expect(browser.text_fields.first.value).to eq('test text')
144
+ end
145
+
146
+ it 'fills a Watir::TextArea' do
147
+ browser.fill_in('Text Area', 'test text')
148
+ expect(browser.textareas.first.value).to eq('test text')
149
+ end
150
+
151
+ it 'fills a Watir::Select with single select' do
152
+ browser.fill_in('Select', 'Test2')
153
+ expect(browser.selects.first.selected_options.count).to eq(1)
154
+ expect(browser.selects.first.selected_options.first.text).to eq('Test2')
155
+ end
156
+
157
+ it 'fills a Watir::Select with multiple select' do
158
+ browser.fill_in('Multi Select', %w(Option2 Option3))
159
+ multiselect = browser.select(id: 'multiselect')
160
+ expect(multiselect.selected_options.count).to eq(2)
161
+ expect(multiselect.selected_options.map(&:text)).to eq(%w(Option2 Option3))
162
+ end
163
+
164
+ it 'fills a Watir::OptionGroup' do
165
+ browser.fill_in('Checkbox5', %w(Checkbox5 Radio7), include_groups: true)
166
+ option_group = browser.option_group(id: 'radio_and_checkbox')
167
+ expect(option_group.selected_options.count).to eq(2)
168
+ expect(option_group.selected_options).to eq(%w(Checkbox5 Radio7))
169
+ end
170
+ end # desribe: without a start node
171
+
172
+
173
+ describe 'with a start node' do
174
+ let(:start_node){ browser.element(id: 'main_content') }
175
+
176
+ it 'fills a Watir::Checkbox from start node' do
177
+ allow(start_node).to receive(:label).and_return(browser.label(text: 'Checkbox'))
178
+ expect(start_node).to receive(:label)
179
+ browser.fill_in('Checkbox', true, start_node: start_node)
180
+ expect(browser.checkboxes.first).to be_checked
181
+ end
182
+
183
+ it 'fills a Watir::Radio from start node' do
184
+ allow(start_node).to receive(:label).and_return(browser.label(text: 'Radio'))
185
+ expect(start_node).to receive(:label)
186
+ browser.fill_in('Radio', true, start_node: start_node)
187
+ expect(browser.radios.first).to be_checked
188
+ end
189
+
190
+ it 'fills a Watir::Select with single select from start node' do
191
+ allow(start_node).to receive(:label).and_return(browser.label(text: 'Select'))
192
+ expect(start_node).to receive(:label)
193
+ browser.fill_in('Select', 'Test2', start_node: start_node)
194
+ expect(browser.selects.first.selected_options.count).to eq(1)
195
+ expect(browser.selects.first.selected_options.first.text).to eq('Test2')
196
+ end
197
+
198
+ it 'fills a Watir::Select with multiple select from start node' do
199
+ allow(start_node).to receive(:label).and_return(browser.label(text: 'Multi Select'))
200
+ expect(start_node).to receive(:label)
201
+ browser.fill_in('Multi Select', %w(Option2 Option3), start_node: start_node)
202
+ multiselect = browser.select(id: 'multiselect')
203
+ expect(multiselect.selected_options.count).to eq(2)
204
+ expect(multiselect.selected_options.map(&:text)).to eq(%w(Option2 Option3))
205
+ end
206
+
207
+ it 'fills a Watir::TextField from start node' do
208
+ allow(start_node).to receive(:label).and_return(browser.label(text: 'Text Field'))
209
+ expect(start_node).to receive(:label)
210
+ browser.fill_in('Text Field', 'test text', start_node: start_node)
211
+ expect(browser.text_fields.first.value).to eq('test text')
212
+ end
213
+
214
+ it 'fills a Watir::TextArea from start node' do
215
+ allow(start_node).to receive(:label).and_return(browser.label(text: 'Text Area'))
216
+ expect(start_node).to receive(:label)
217
+ browser.fill_in('Text Area', 'test text', start_node: start_node)
218
+ expect(browser.textareas.first.value).to eq('test text')
219
+ end
220
+
221
+ it 'fills a Watir::FileField from start node' do
222
+ allow(start_node).to receive(:label).and_return(browser.label(text: 'File'))
223
+ expect(start_node).to receive(:label)
224
+ browser.fill_in('File', File.join(HTML_DIR, FORM_PAGE), start_node: start_node)
225
+ expect(browser.file_fields.first.value).to eq(FORM_PAGE)
226
+ end
227
+
228
+ it 'fills a Watir::OptionGroup from start node' do
229
+ allow(start_node).to receive(:label).and_return(browser.label(text: 'Checkbox5'))
230
+ expect(start_node).to receive(:label)
231
+ browser.fill_in(
232
+ 'Checkbox5', %w(Checkbox5 Radio7), include_groups: true, start_node: start_node
233
+ )
234
+ option_group = browser.option_group(id: 'radio_and_checkbox')
235
+ expect(option_group.selected_options.count).to eq(2)
236
+ expect(option_group.selected_options).to eq(%w(Checkbox5 Radio7))
237
+ end
238
+ end # descibe: with a start node
239
+ end # #fill_in
240
+ end
241
+
242
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ module Watir
4
+ describe FileField do
5
+ describe '#field_value' do
6
+ subject { FileField.new('', id: '') }
7
+
8
+ context 'no path is set' do
9
+ it 'returns empty string' do
10
+ allow(subject).to receive(:value).and_return(nil)
11
+ expect(subject.field_value).to eq('')
12
+ end
13
+ end
14
+
15
+ context 'a path is set' do
16
+ it 'returns string with file name' do
17
+ allow(subject).to receive(:value).and_return('form.html')
18
+ expect(subject.field_value).to eq('form.html')
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end