Class: Watir::OptionGroup
- Inherits:
-
HTMLElement
- Object
- HTMLElement
- Watir::OptionGroup
- Defined in:
- lib/watir-formhandler/option_group.rb
Overview
The OptionGroup represents the parent node of any grouped checkboxes or radio buttons. Its purpose is to provide a way to select different options in a more natural way, like the user would do it: instead of going through all checkboxes one by one and setting them to ‘true’, OptionGroup allows to specify the option names that are desired to be opted in.
Instance Method Summary collapse
- #field_value ⇒ Object
-
#initialize(parent, selector) ⇒ OptionGroup
constructor
Allows selector to be an HTMLElement, in which case the internal element will be set to this HTMLElement node.
-
#option_fields ⇒ Array<Element>
Returns the fields of all available options.
-
#option_names ⇒ Array<String>
Returns the names of all available options.
-
#options ⇒ Hash<label => field>
Returns all available options fields and their respective label as a Hash.
-
#selected_options ⇒ Array<String>
Returns the selected options of this OptionGroup.
-
#set(*wanted_options) ⇒ Object
Selects the given option(s) and deselects all other ones.
- #tag_name ⇒ Object
Constructor Details
#initialize(parent, selector) ⇒ OptionGroup
Allows selector to be an HTMLElement, in which case the internal element will be set to this HTMLElement node.
12 13 14 15 |
# File 'lib/watir-formhandler/option_group.rb', line 12 def initialize(parent, selector) stripped_selector = selector.respond_to?(:selector) ? selector.selector : selector super parent, stripped_selector end |
Instance Method Details
#field_value ⇒ Object
79 80 81 |
# File 'lib/watir-formhandler/option_group.rb', line 79 def field_value end |
#option_fields ⇒ Array<Element>
Returns the fields of all available options.
27 28 29 |
# File 'lib/watir-formhandler/option_group.rb', line 27 def option_fields self.checkboxes.to_a + self.radios.to_a end |
#option_names ⇒ Array<String>
Returns the names of all available options.
20 21 22 |
# File 'lib/watir-formhandler/option_group.rb', line 20 def option_names self.labels.map{ |label| label.text.strip } end |
#options ⇒ Hash<label => field>
Returns all available options fields and their respective label as a Hash.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/watir-formhandler/option_group.rb', line 34 def option_hash = {} my_labels = option_names my_inputs = option_fields my_labels.count.times do |index| option_hash[my_labels[index]] = my_inputs[index] end option_hash end |
#selected_options ⇒ Array<String>
Returns the selected options of this OptionGroup.
68 69 70 71 72 73 74 75 |
# File 'lib/watir-formhandler/option_group.rb', line 68 def selected = [] my_labels = option_names inputs.each_with_index do |field, index| selected << my_labels[index] if field.checked? end selected end |
#set(*wanted_options) ⇒ Object
Selects the given option(s) and deselects all other ones. This can not be done with radio buttons, however, as they cannot be deselected.
55 56 57 58 59 60 61 62 63 |
# File 'lib/watir-formhandler/option_group.rb', line 55 def set(*) = [*].flatten = option_names - @options = select(, true) select(, false) @options = nil end |
#tag_name ⇒ Object
84 85 86 |
# File 'lib/watir-formhandler/option_group.rb', line 84 def tag_name 'option_group' end |