Class: Watir::SelectList

Inherits:
Select show all
Includes:
Exception
Defined in:
lib/watir-webdriver/elements/select_list.rb

Constant Summary

Constants inherited from BaseElement

BaseElement::IGNORED_ATTRIBUTES

Instance Method Summary collapse

Methods inherited from BaseElement

attribute_list, #attribute_value, attributes, #click, #double_click, #driver, #element, #exists?, #fire_event, #flash, #focus, #html, #initialize, #inspect, #parent, #right_click, #run_checkers, #send_keys, #style, #tag_name, #text, typed_attributes, #visible?

Methods included from Container

add

Methods included from XpathSupport

#element_by_xpath, #elements_by_xpath

Constructor Details

This class inherits a constructor from Watir::BaseElement

Instance Method Details

#clearObject

Clear all selected options

Raises:



23
24
25
26
27
28
29
30
31
# File 'lib/watir-webdriver/elements/select_list.rb', line 23

def clear
  assert_exists

  raise Error, "you can only clear multi-selects" unless multiple?

  options.each do |o|
    o.toggle if o.selected?
  end
end

#enabled?Boolean

Returns true if this element is enabled

Returns:

  • (Boolean)


15
16
17
# File 'lib/watir-webdriver/elements/select_list.rb', line 15

def enabled?
  !disabled?
end

#include?(str_or_rx) ⇒ Boolean Also known as: includes?

Returns true if the select list has one or more options where text or label matches the given value.

Parameters:

  • value (String, Regexp)

    A value.

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/watir-webdriver/elements/select_list.rb', line 45

def include?(str_or_rx)
  assert_exists
  # TODO: optimize similar to selected?
  options.any? { |e| str_or_rx === e.text }
end

#optionsObject



33
34
35
36
# File 'lib/watir-webdriver/elements/select_list.rb', line 33

def options
  assert_exists
  super
end

#select(str_or_rx) ⇒ String

Select the option(s) whose text or label matches the given string. If this is a multi-select and several options match the value given, all will be selected.

Parameters:

  • value (String, Regexp)

    A value.

Returns:

  • (String)

    The text of the option selected. If multiple options match, returns the first match.

Raises:



61
62
63
# File 'lib/watir-webdriver/elements/select_list.rb', line 61

def select(str_or_rx)
    select_by :text, str_or_rx, multiple?
end

#select_value(str_or_rx) ⇒ String

Selects the option(s) whose value attribute matches the given string.

Parameters:

  • value (String, Regexp)

    A value.

Returns:

  • (String)

    The option selected. If multiple options match, returns the first match

Raises:

See Also:

  • +select+


75
76
77
# File 'lib/watir-webdriver/elements/select_list.rb', line 75

def select_value(str_or_rx)
  select_by :value, str_or_rx, multiple?
end

#selected?(str_or_rx) ⇒ Boolean

Returns true if any of the selected options’ text or label match the given value.

Parameters:

  • value (String, Regexp)

    A value.

Returns:

  • (Boolean)

Raises:



87
88
89
90
91
92
93
94
95
96
# File 'lib/watir-webdriver/elements/select_list.rb', line 87

def selected?(str_or_rx)
  assert_exists
  matches = @element.find_elements(:tag_name, 'option').select { |e| str_or_rx === e.text || str_or_rx === e.attribute(:label) }

  if matches.empty?
    raise UnknownObjectException, "Unable to locate option matching #{str_or_rx.inspect}"
  end

  matches.any? { |e| e.selected? }
end

#selected_optionsArray<String>

Returns An array of strings representing the text value of the currently selected options.

Returns:

  • (Array<String>)

    An array of strings representing the text value of the currently selected options.



117
118
119
120
# File 'lib/watir-webdriver/elements/select_list.rb', line 117

def selected_options
  assert_exists
  options.map { |e| e.text if e.selected? }.compact
end

#valueString?

Returns the value of the first selected option in the select list. Returns nil if no option is selected.

Returns:



105
106
107
108
109
110
# File 'lib/watir-webdriver/elements/select_list.rb', line 105

def value
  o = options.find { |e| e.selected? }
  return if o.nil?

  o.value
end