Class: Watir::RadioSet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Exception
Defined in:
lib/watir/radio_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_scope, selector) ⇒ RadioSet

Returns a new instance of RadioSet.

Raises:

  • (ArgumentError)

11
12
13
14
15
16
# File 'lib/watir/radio_set.rb', line 11

def initialize(query_scope, selector)
  raise ArgumentError, "invalid argument: #{selector.inspect}" unless selector.is_a? Hash

  @source = Radio.new(query_scope, selector)
  @frame = @source.parent(tag_name: 'form')
end

Instance Attribute Details

#frameObject (readonly)

Returns the value of attribute frame


9
10
11
# File 'lib/watir/radio_set.rb', line 9

def frame
  @frame
end

#sourceObject (readonly)

Returns the value of attribute source


9
10
11
# File 'lib/watir/radio_set.rb', line 9

def source
  @source
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Returns true if two elements are equal.

Examples:

browser.radio_set(id: 'new_user_newsletter_yes') == browser.radio_set(id: 'new_user_newsletter_no')#=> true

199
200
201
# File 'lib/watir/radio_set.rb', line 199

def ==(other)
  other.is_a?(self.class) && radios == other.radios
end

#[](idx) ⇒ Object

Get the n'th radio button in this set


40
41
42
# File 'lib/watir/radio_set.rb', line 40

def [](idx)
  radios[idx]
end

#disabled?Boolean

Returns true if all radio buttons in the set are disabled.


88
89
90
# File 'lib/watir/radio_set.rb', line 88

def disabled?
  !enabled?
end

#each {|element| ... } ⇒ Object

Yields each Radio associated with this set.

Examples:

radio_set = browser.radio_set
radio_set.each do |radio|
  puts radio.text
end

Yield Parameters:


30
31
32
# File 'lib/watir/radio_set.rb', line 30

def each(&block)
  radios.each(&block)
end

#enabled?Boolean

Returns true if any radio buttons in the set are enabled.


78
79
80
# File 'lib/watir/radio_set.rb', line 78

def enabled?
  any?(&:enabled?)
end

#include?(str_or_rx) ⇒ Boolean

Returns true if the radio set has one or more radio buttons where label matches the given value.


120
121
122
# File 'lib/watir/radio_set.rb', line 120

def include?(str_or_rx)
  radio(label: str_or_rx).exist?
end

#nameString

Returns the name attribute for the set.


98
99
100
# File 'lib/watir/radio_set.rb', line 98

def name
  @name ||= source.name
end

#radio(opt = {}) ⇒ Object


48
49
50
51
52
53
54
55
56
# File 'lib/watir/radio_set.rb', line 48

def radio(opt = {})
  if !name.empty? && (!opt[:name] || opt[:name] == name)
    frame.radio(opt.merge(name: name))
  elsif name.empty?
    source
  else
    raise UnknownObjectException, "#{opt[:name]} does not match name of RadioSet: #{name}"
  end
end

#radios(opt = {}) ⇒ Object


62
63
64
65
66
67
68
69
70
# File 'lib/watir/radio_set.rb', line 62

def radios(opt = {})
  if !name.empty? && (!opt[:name] || opt[:name] == name)
    element_call(:wait_for_present) { frame.radios(opt.merge(name: name)) }
  elsif name.empty?
    single_radio_collection
  else
    raise UnknownObjectException, "#{opt[:name]} does not match name of RadioSet: #{name}"
  end
end

#select(str_or_rx) ⇒ String

Select the radio button whose value or label matches the given string.

Raises:


132
133
134
135
136
137
138
139
140
141
# File 'lib/watir/radio_set.rb', line 132

def select(str_or_rx)
  %i[value label].each do |key|
    radio = radio(key => str_or_rx)
    next unless radio.exist?

    radio.click unless radio.selected?
    return key == :value ? radio.value : radio.text
  end
  raise UnknownObjectException, "Unable to locate radio matching #{str_or_rx.inspect}"
end

#selectedWatir::Radio?

Returns the selected Radio element. Returns nil if no radio button is selected.


187
188
189
# File 'lib/watir/radio_set.rb', line 187

def selected
  find(&:selected?)
end

#selected?(str_or_rx) ⇒ Boolean

Returns true if any of the radio button label matches the given value.

Raises:


151
152
153
154
155
156
# File 'lib/watir/radio_set.rb', line 151

def selected?(str_or_rx)
  found = frame.radio(label: str_or_rx)
  return found.selected? if found.exist?

  raise UnknownObjectException, "Unable to locate radio matching #{str_or_rx.inspect}"
end

#textString?

Returns the text of the selected radio button in the set. Returns nil if no option is selected.


176
177
178
# File 'lib/watir/radio_set.rb', line 176

def text
  selected&.text
end

#typeString

If RadioSet exists, this always returns 'radio'.


108
109
110
111
# File 'lib/watir/radio_set.rb', line 108

def type
  assert_exists
  'radio'
end

#valueString?

Returns the value of the selected radio button in the set. Returns nil if no radio is selected.


165
166
167
# File 'lib/watir/radio_set.rb', line 165

def value
  selected&.value
end