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)


13
14
15
16
17
18
# File 'lib/watir/radio_set.rb', line 13

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.



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

def frame
  @frame
end

#sourceObject (readonly)

Returns the value of attribute source.



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

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


202
203
204
# File 'lib/watir/radio_set.rb', line 202

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

#[](idx) ⇒ Object

Get the n’th radio button in this set

Returns:

  • Watir::Radio



42
43
44
# File 'lib/watir/radio_set.rb', line 42

def [](idx)
  radios[idx]
end

#disabled?Boolean

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

Returns:

  • (Boolean)


90
91
92
# File 'lib/watir/radio_set.rb', line 90

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:



32
33
34
# File 'lib/watir/radio_set.rb', line 32

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

#enabled?Boolean

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

Returns:

  • (Boolean)


80
81
82
# File 'lib/watir/radio_set.rb', line 80

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.

Parameters:

  • str_or_rx (String, Regexp)

Returns:

  • (Boolean)


122
123
124
# File 'lib/watir/radio_set.rb', line 122

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

#nameString

Returns the name attribute for the set.

Returns:

  • (String)


100
101
102
# File 'lib/watir/radio_set.rb', line 100

def name
  @name ||= source.name
end

#radio(opt = {}) ⇒ Object

Returns Watir::Radio.

Returns:

  • Watir::Radio



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

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

Returns Watir::RadioCollection.

Returns:

  • Watir::RadioCollection



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

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 Also known as: set

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

Parameters:

  • str_or_rx (String, Regexp)

Returns:

  • (String)

    The value or text of the radio selected.

Raises:



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

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.

Returns:



190
191
192
# File 'lib/watir/radio_set.rb', line 190

def selected
  find(&:selected?)
end

#selected?(str_or_rx) ⇒ Boolean

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

Parameters:

  • str_or_rx (String, Regexp)

Returns:

  • (Boolean)

Raises:



154
155
156
157
158
159
# File 'lib/watir/radio_set.rb', line 154

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.

Returns:

  • (String, nil)


179
180
181
# File 'lib/watir/radio_set.rb', line 179

def text
  selected&.text
end

#typeString

If RadioSet exists, this always returns ‘radio’.

Returns:

  • (String)


110
111
112
113
# File 'lib/watir/radio_set.rb', line 110

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.

Returns:

  • (String, nil)


168
169
170
# File 'lib/watir/radio_set.rb', line 168

def value
  selected&.value
end