Class: Watir::SelectList

Inherits:
InputElement show all
Defined in:
lib/watir/input_elements.rb,
lib/watir/camel_case.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 collapse

INPUT_TYPES =

:stopdoc:

["select-one", "select-multiple"]

Constants inherited from Element

Element::TO_S_SIZE

Instance Attribute Summary collapse

Attributes inherited from Element

#container

Attributes included from Container

#activeObjectHighLightColor, #page_container, #type_keys, #typingspeed

Instance Method Summary collapse

Methods inherited from InputElement

#initialize, #locate

Methods inherited from Element

#<=>, #activeObjectHighLightColor, #after_text, #assert_enabled, #assert_exists, #attribute_value, #before_text, #click, #click!, #click_no_wait, #document, #enabled?, #exists?, #fire_event, #flash, #focus, #initialize, #inspect, #ole_object, #ole_object=, #parent, #text, #to_s, #type_keys, #typingspeed, #visible?

Methods included from Container

#area, #areas, #button, #buttons, #cell, #cells, #checkbox, #checkboxes, #dds, #divs, #dls, #dts, #element, #elements, #ems, #file_field, #file_fields, #form, #forms, #frame, #hidden, #hiddens, #image, #images, #labels, #link, #links, #lis, #locate_all_elements, #locate_input_element, #locate_tagged_element, #log, #map, #maps, #modal_dialog, #popup, #pres, #ps, #radio, #radios, #row, #rows, #select_list, #select_lists, #set_container, #show_all_objects, #spans, #strongs, #table, #tables, #text_field, #text_fields, #wait

Constructor Details

This class inherits a constructor from Watir::InputElement

Instance Attribute Details

#oObject

exposed to Option class



25
26
27
# File 'lib/watir/input_elements.rb', line 25

def o
  @o
end

Instance Method Details

#clearObject

This method clears the selected items in the select box



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/watir/input_elements.rb', line 29

def clear
  assert_exists
  highlight(:set)
  wait = false
  @o.each do |selectBoxItem|
    if selectBoxItem.selected
      selectBoxItem.selected = false
      wait = true
    end
  end
  @container.wait if wait
  highlight(:clear)
end

#clearSelectionObject



64
# File 'lib/watir/camel_case.rb', line 64

alias clearSelection clear

#getAllContentsObject



62
# File 'lib/watir/camel_case.rb', line 62

alias getAllContents options

#getSelectedItemsObject



63
# File 'lib/watir/camel_case.rb', line 63

alias getSelectedItems selected_options

#include?(text_or_regexp) ⇒ Boolean

Does the SelectList include the specified option (text)?

Returns:

  • (Boolean)


119
120
121
# File 'lib/watir/input_elements.rb', line 119

def include? text_or_regexp
  getAllContents.grep(text_or_regexp).size > 0
end

#includes?Object



65
# File 'lib/watir/camel_case.rb', line 65

alias includes? include?

#option(attribute, value) ⇒ Object

this method provides the access to the <option> item in select_list

Usage example:

Given the following html:

<select  id="gender">
  <option value="U">Unknown</option>
  <option value="M" selected>Male</option>
  <option value="F">Female</option>
</select>

get the value attribute of option with visible text ‘Female’

browser.select_list(:id, 'gender').option(:text, 'Female').value #=> 'F'

or find out if the value ‘M’ is selected

 browser.select_list(:id, 'gender').option(:value, 'M').selected #=> true

* attribute  - Symbol :value, :text or other attribute - how we find an item in the select box
* value  - string or reg exp - what we are looking for


151
152
153
154
# File 'lib/watir/input_elements.rb', line 151

def option(attribute, value)
  assert_exists
  Option.new(self, attribute, value)
end

#optionsObject

Returns array of all text items displayed in a select box An empty array is returned if the select box has no contents. Raises UnknownObjectException if the select box is not found



95
96
97
98
99
100
101
# File 'lib/watir/input_elements.rb', line 95

def options 
  assert_exists
  @container.log "There are #{@o.length} items"
  returnArray = []
  @o.each { |thisItem| returnArray << thisItem.text }
  return returnArray
end

#select(item) ⇒ Object Also known as: set

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


47
48
49
# File 'lib/watir/input_elements.rb', line 47

def select(item)
  select_item_in_select_list(:text, item)
end

#select_item_in_select_list(attribute, value) ⇒ Object

BUG: Should be private Selects something from the select box

* name  - symbol  :value or :text - how we find an item in the select box
* item  - string or reg exp - what we are looking for


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/watir/input_elements.rb', line 63

def select_item_in_select_list(attribute, value) #:nodoc:
  assert_exists
  highlight(:set)
  found = false

  value = value.to_s unless [Regexp, String].any? { |e| value.kind_of? e }

  @container.log "Setting box #{@o.name} to #{attribute.inspect} => #{value.inspect}"
  @o.each do |option| # items in the list
    if value.matches(option.invoke(attribute.to_s))
      if option.selected
        found = true
        break
      else
        option.selected = true
        @o.fireEvent("onChange")
        @container.wait
        found = true
        break
      end
    end
  end

  unless found
    raise NoValueFoundException, "No option with #{attribute.inspect} of #{value.inspect} in this select element"
  end
  highlight(:clear)
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, reg exp


55
56
57
# File 'lib/watir/input_elements.rb', line 55

def select_value(item)
  select_item_in_select_list(:value, item)
end

#selected?(text_or_regexp) ⇒ Boolean

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

Returns:

  • (Boolean)


124
125
126
127
128
129
130
# File 'lib/watir/input_elements.rb', line 124

def selected? text_or_regexp
  unless includes? text_or_regexp
    raise UnknownObjectException, "Option #{text_or_regexp.inspect} not found."
  end

  getSelectedItems.grep(text_or_regexp).size > 0
end

#selected_optionsObject

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



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/watir/input_elements.rb', line 105

def selected_options
  assert_exists
  returnArray = []
  @container.log "There are #{@o.length} items"
  @o.each do |thisItem|
    if thisItem.selected
      @container.log "Item (#{thisItem.text}) is selected"
      returnArray << thisItem.text
    end
  end
  return returnArray
end