Class: Watir::SelectList

Inherits:
InputElement show all
Defined in:
lib/watir-classic/input_elements.rb

Overview

This class is the way in which select boxes are manipulated. Normally a user would not need to create this object as it is returned by the Watir::Container#select_list method

Constant Summary

Constants inherited from Element

Element::TO_S_SIZE

Instance Attribute Summary

Attributes inherited from Element

#container

Attributes included from Container

#activeObjectHighLightColor, #page_container, #type_keys, #typingspeed

Instance Method Summary collapse

Methods inherited from InputElement

#locate

Methods inherited from Element

#<=>, #__ole_inner_elements, #activeObjectHighLightColor, #assert_enabled, #assert_exists, #attribute_value, #click, #click!, #create_event, #disabled?, #dispatch_event, #document, #double_click, #enabled?, #exists?, #fire_event, #flash, #focus, #focused?, #initialize, #inspect, #locate, #method_missing, #ole_object, #ole_object=, #parent, #right_click, #send_keys, #style, #tag_name, #text, #to_s, #to_subtype, #type_keys, #typingspeed, #visible?

Methods included from DragAndDropHelper

#drag_and_drop_by, #drag_and_drop_on

Methods included from Container

#__ole_inner_elements, #alert, #locator_for, #modal_dialog, #set_container, support_element, #wait

Methods included from Exception

message_for_unable_to_locate

Methods included from ElementExtensions

#present?, #wait_until_present, #wait_while_present, #when_present

Constructor Details

This class inherits a constructor from Watir::Element

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Watir::Element

Instance Method Details

#clearObject

This method clears the selected items in the select box



26
27
28
29
30
# File 'lib/watir-classic/input_elements.rb', line 26

def clear
  perform_action do
    options.each {|option| option.clear}
  end
end

#include?(text_or_regexp) ⇒ Boolean

Does the SelectList include the specified option (text)?

Returns:

  • (Boolean)


64
65
66
# File 'lib/watir-classic/input_elements.rb', line 64

def include? text_or_regexp
  !options.map(&:text).grep(text_or_regexp).empty?
end

#select(item) ⇒ Object

This method selects an item, or items in a select box, by text. Raises NoValueFoundException if the specified value is not found.

* item   - the thing to select, string or reg exp


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/watir-classic/input_elements.rb', line 35

def select(item)
  matching_options = []
  perform_action do
    matching_options = matching_items_in_select_list(:text, item) + 
      matching_items_in_select_list(:label, item) + 
      matching_items_in_select_list(:value, item)
    raise NoValueFoundException, "No option with :text, :label or :value of #{item.inspect} in this select element" if matching_options.empty?
    matching_options.each(&:select)
  end
  matching_options.first.text
end

#select_value(item) ⇒ Object

Selects an item, or items in a select box, by value. Raises NoValueFoundException if the specified value is not found.

* item   - the value of the thing to select, string or reg exp


50
51
52
53
54
55
# File 'lib/watir-classic/input_elements.rb', line 50

def select_value(item)
  matching_options = matching_items_in_select_list(:value, item)
  raise NoValueFoundException, "No option with :value of #{item.inspect} in this select element" if matching_options.empty?
  matching_options.each(&:select)
  matching_options.first.value
end

#selected?(text_or_regexp) ⇒ Boolean

Is the specified option (text) selected? Raises exception of option does not exist.

Returns:

  • (Boolean)

Raises:



69
70
71
72
# File 'lib/watir-classic/input_elements.rb', line 69

def selected? text_or_regexp
  raise UnknownObjectException, "Option #{text_or_regexp.inspect} not found." unless include? text_or_regexp
  !selected_options.map(&:text).grep(text_or_regexp).empty?
end

#selected_optionsObject

Returns array of the selected text items in a select box Raises UnknownObjectException if the select box is not found.



59
60
61
# File 'lib/watir-classic/input_elements.rb', line 59

def selected_options
  options.select(&:selected?)
end