class Watir::RadioSet

Attributes

frame[R]
source[R]

Public Class Methods

new(query_scope, selector) click to toggle source
# 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

Public Instance Methods

==(other) click to toggle source

Returns true if two elements are equal.

@example

browser.radio_set(id: 'new_user_newsletter_yes') == browser.radio_set(id: 'new_user_newsletter_no')
#=> true
# File lib/watir/radio_set.rb, line 199
def ==(other)
  other.is_a?(self.class) && radios == other.radios
end
Also aliased as: eql?
[](idx) click to toggle source

Get the n'th radio button in this set

@return Watir::Radio

# File lib/watir/radio_set.rb, line 40
def [](idx)
  radios[idx]
end
disabled?() click to toggle source

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

@return [Boolean]

# File lib/watir/radio_set.rb, line 88
def disabled?
  !enabled?
end
each(&block) click to toggle source

Yields each Radio associated with this set.

@example

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

@yieldparam [Watir::RadioSet] element iterate through the radio buttons.

# File lib/watir/radio_set.rb, line 30
def each(&block)
  radios.each(&block)
end
enabled?() click to toggle source

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

@return [Boolean]

# File lib/watir/radio_set.rb, line 78
def enabled?
  any?(&:enabled?)
end
eql?(other)
Alias for: ==
include?(str_or_rx) click to toggle source

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

@param [String, Regexp] str_or_rx @return [Boolean]

# File lib/watir/radio_set.rb, line 120
def include?(str_or_rx)
  radio(label: str_or_rx).exist?
end
name() click to toggle source

Returns the name attribute for the set.

@return [String]

# File lib/watir/radio_set.rb, line 98
def name
  @name ||= source.name
end
radio(opt = {}) click to toggle source

@return Watir::Radio

# 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 = {}) click to toggle source

@return Watir::RadioCollection

# 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) click to toggle source

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

@param [String, Regexp] str_or_rx @raise [Watir::Exception::UnknownObjectException] if the Radio does not exist. @return [String] The value or text of the radio selected.

# 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
selected() click to toggle source

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

@return [Watir::Radio, nil]

# File lib/watir/radio_set.rb, line 187
def selected
  find(&:selected?)
end
selected?(str_or_rx) click to toggle source

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

@param [String, Regexp] str_or_rx @raise [Watir::Exception::UnknownObjectException] if the options do not exist @return [Boolean]

# 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
text() click to toggle source

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

@return [String, nil]

# File lib/watir/radio_set.rb, line 176
def text
  selected&.text
end
type() click to toggle source

If RadioSet exists, this always returns 'radio'.

@return [String]

# File lib/watir/radio_set.rb, line 108
def type
  assert_exists
  'radio'
end
value() click to toggle source

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

@return [String, nil]

# File lib/watir/radio_set.rb, line 165
def value
  selected&.value
end

Private Instance Methods

single_radio_collection() click to toggle source
# File lib/watir/radio_set.rb, line 213
def single_radio_collection
  collection = RadioCollection.new(frame, source.selector)
  collection.first.cache = source.wd
  collection
end