Class: Watir::OptionGroup

Inherits:
HTMLElement show all
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.

Examples:

group = browser.option

Instance Method Summary collapse

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_valueObject

See Also:



79
80
81
# File 'lib/watir-formhandler/option_group.rb', line 79

def field_value
  selected_options
end

#option_fieldsArray<Element>

Returns the fields of all available options.

Returns:

  • (Array<Element>)

    fields of all 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_namesArray<String>

Returns the names of all available options.

Returns:

  • (Array<String>)

    names of all options.



20
21
22
# File 'lib/watir-formhandler/option_group.rb', line 20

def option_names
  self.labels.map{ |label| label.text.strip }
end

#optionsHash<label => field>

Returns all available options fields and their respective label as a Hash.

Returns:

  • (Hash<label => field>)

    hash with all labels and fields.



34
35
36
37
38
39
40
41
42
43
# File 'lib/watir-formhandler/option_group.rb', line 34

def options
  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_optionsArray<String>

Returns the selected options of this OptionGroup.

Returns:

  • (Array<String>)

    the selected options.



68
69
70
71
72
73
74
75
# File 'lib/watir-formhandler/option_group.rb', line 68

def selected_options
  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.

Examples:

Passing a single String

group.set('Checkbox1')  #=> selects 'Checkbox1'

Passing several options

group.set('Checkbox1', 'Checkbox2') #=> selects 'Checkbox1' and 'Checkbox2'

Passing several options as Array

group.set(['Checkbox1', 'Radio1'])  #=> selects 'Checkbox1' and 'Radio1'

Parameters:

  • wanted_options (String, Array<String>)

    to be selected.



55
56
57
58
59
60
61
62
63
# File 'lib/watir-formhandler/option_group.rb', line 55

def set(*wanted_options)
  options_to_select = [*wanted_options].flatten
  options_to_deselect = option_names - options_to_select

  @options = options
  select(options_to_select, true)
  select(options_to_deselect, false)
  @options = nil
end

#tag_nameObject



84
85
86
# File 'lib/watir-formhandler/option_group.rb', line 84

def tag_name
  'option_group'
end