Class: Watir::SelectList

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

Overview

Returned by Container#select_list.

Instance Attribute Summary

Attributes inherited from Element

#container

Attributes included from Container

#page_container

Instance Method Summary collapse

Methods inherited from InputElement

#alt, #disabled?, #label, #name, #required?, #src, #type, #value

Methods inherited from Element

#<=>, #attribute_value, #class_name, #click, #disabled?, #double_click, #enabled?, #exists?, #fire_event, #flash, #focus, #focused?, #id, #initialize, #inner_html, #inspect, #method_missing, #ole_object, #outer_html, #parent, #right_click, #send_keys, #style, #tag_name, #text, #title, #to_s, #to_subtype, #unique_number, #visible?

Methods included from DragAndDropHelper

#drag_and_drop_by, #drag_and_drop_on

Methods included from Container

#a, #abbr, #address, #alert, #area, #article, #aside, #audio, #b, #base, #bdi, #bdo, #blockquote, #body, #br, #button, #canvas, #caption, #checkbox, #cite, #code, #col, #colgroup, #command, #data, #datalist, #dd, #del, #details, #dfn, #div, #dl, #dt, #element, #em, #embed, #fieldset, #figcaption, #figure, #file_field, #font, #footer, #form, #frame, #frameset, #h1, #h2, #h3, #h4, #h5, #h6, #head, #header, #hgroup, #hidden, #hr, #i, #img, #input, #ins, #kbd, #keygen, #label, #legend, #li, #map, #mark, #menu, #meta, #meter, #modal_dialog, #nav, #noscript, #object, #ol, #optgroup, #option, #output, #p, #param, #pre, #progress, #q, #radio, #rp, #rt, #ruby, #s, #samp, #script, #section, #small, #source, #span, #strong, #style, #sub, #summary, #sup, #table, #tbody, #td, #text_field, #textarea, #tfoot, #th, #thead, #time, #title, #tr, #track, #u, #ul, #var, #video, #wbr

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

Clear the selected options in the select list.



33
34
35
36
37
# File 'lib/watir-classic/input_elements.rb', line 33

def clear
  perform_action do
    options.each(&:clear)
  end
end

#include?(text_or_regexp) ⇒ Boolean

Returns true when option with text includes in select list, false otherwise.

Parameters:

  • text_or_regexp (String, Regexp)

    option with text to search for.

Returns:

  • (Boolean)

    true when option with text includes in select list, false otherwise.



83
84
85
# File 'lib/watir-classic/input_elements.rb', line 83

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

#multiple?String, ...

Retrieve element’s multiple? from the OLE method.

Returns:

  • (String, Boolean, Fixnum)

    element’s “multiple?” attribute value. Return type depends of the attribute type.

  • (String)

    an empty String if the “multiple?” attribute does not exist.

See Also:



27
# File 'lib/watir-classic/input_elements.rb', line 27

attr_ole :multiple?

#select(item) ⇒ Object

Select an item or items in a select box.

Parameters:

  • item (String, Regexp)

    option to select by text, label or value.

Raises:



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/watir-classic/input_elements.rb', line 45

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
  first_present_option_value matching_options, :text
end

#select_value(item) ⇒ Object

Select an item or items in a select box by option’s value.

Parameters:

  • item (String, Regexp)

    option to select by value.

Raises:



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

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)
  first_present_option_value matching_options, :value
end

#selected?(text_or_regexp) ⇒ Boolean

Returns true when option with text is selected in select list, false otherwise.

Parameters:

  • text_or_regexp (String, Regexp)

    option with text to search for.

Returns:

  • (Boolean)

    true when option with text is selected in select list, false otherwise.

Raises:



91
92
93
94
# File 'lib/watir-classic/input_elements.rb', line 91

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_optionsArray<OptionCollection>

Returns array of selected options.

Examples:

Retrieve selected options as a text:

browser.select_list.selected_options.map(&:text)

Returns:

  • (Array<OptionCollection>)

    array of selected options.



75
76
77
# File 'lib/watir-classic/input_elements.rb', line 75

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