Class: Celerity::SelectList

Inherits:
InputElement show all
Defined in:
lib/celerity/watir_compatibility.rb,
lib/celerity/elements/select_list.rb

Constant Summary collapse

TAGS =
[ Identifier.new('select') ]
DEFAULT_HOW =
:name

Constants inherited from InputElement

InputElement::ATTRIBUTES

Constants inherited from Element

Element::ATTRIBUTES, Element::BASE_ATTRIBUTES, Element::CELLHALIGN_ATTRIBUTES, Element::CELLVALIGN_ATTRIBUTES, Element::HTML_401_TRANSITIONAL, Element::TO_S_SIZE

Instance Attribute Summary

Attributes inherited from Element

#container

Attributes included from Container

#browser

Instance Method Summary collapse

Methods inherited from InputElement

#readonly?

Methods included from DisabledElement

#assert_enabled, #disabled?, #enabled?

Methods included from ClickableElement

#click, #click_and_attach, #double_click, #download, #right_click

Methods inherited from Element

#assert_exists, #attribute_string, #attribute_value, #exists?, #fire_event, #focus, #initialize, #javascript_object, #locate, #method_missing, #methods, #object, #parent, #respond_to?, #text, #to_s, #to_xml, #visible?, #xpath

Methods included from Container

#area, #areas, #button, #buttons, #cell, #cells, #check_box, #checkboxes, #container=, #contains_text, #dd, #dds, #div, #divs, #dl, #dls, #dt, #dts, #em, #ems, #file_field, #file_fields, #form, #forms, #frame, #frames, #h1, #h1s, #h2, #h2s, #h3, #h3s, #h4, #h4s, #h5, #h5s, #h6, #h6s, #hidden, #hiddens, #image, #images, #inspect, #label, #labels, #li, #link, #links, #lis, #map, #maps, #meta, #metas, #ol, #ols, #option, #p, #pre, #pres, #ps, #radio, #radios, #row, #rows, #select_list, #select_lists, #span, #spans, #strong, #strongs, #table, #tables, #tbodies, #tbody, #text_field, #text_fields, #tfoot, #tfoots, #th, #thead, #theads, #ths, #ul, #uls, #update_page

Methods included from ShortInspect

#short_inspect

Constructor Details

This class inherits a constructor from Celerity::Element

Dynamic Method Handling

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

Instance Method Details

#clearObject

Clear all selected options



30
31
32
33
34
# File 'lib/celerity/elements/select_list.rb', line 30

def clear
  # assert_exists called by SelectList#type here
  # TODO: should update page for each option changed?
  @object.getSelectedOptions.each { |e| e.setSelected(false) } unless type() == 'select-one'
end

#clear_selectionObject



70
# File 'lib/celerity/watir_compatibility.rb', line 70

alias_method :clear_selection,  :clear

#clearSelectionObject



69
# File 'lib/celerity/watir_compatibility.rb', line 69

alias_method :clearSelection,   :clear

#getAllContentsObject



68
# File 'lib/celerity/watir_compatibility.rb', line 68

alias_method :getAllContents,   :options

#getSelectedItemsObject



67
# File 'lib/celerity/watir_compatibility.rb', line 67

alias_method :getSelectedItems, :selected_options

#include?(value) ⇒ true, false

Returns true if the select list has one or more options matching the given value.

Parameters:

  • value (String, Regexp)

    A value.

Returns:

  • (true, false)


69
70
71
72
# File 'lib/celerity/elements/select_list.rb', line 69

def include?(value)
  assert_exists
  !!@object.getOptions.find { |e| matches_option?(e, value) }
end

#includes?Object



72
# File 'lib/celerity/watir_compatibility.rb', line 72

alias_method :includes?,        :include?

#optionsArray<String>

Returns An array of strings representing the text value of the select list’s options.

Returns:

  • (Array<String>)

    An array of strings representing the text value of the select list’s options.



10
11
12
13
14
15
# File 'lib/celerity/elements/select_list.rb', line 10

def options
  assert_exists
  @object.getOptions.map do |e|
    e.asText.empty? ? e.getLabelAttribute : e.asText
  end
end

#select(value) ⇒ String? Also known as: set

Select the option(s) matching the given value. If several options match the value given, all will be selected.

Parameters:

  • value (String, Regexp)

    A value.

Returns:

  • (String, nil)

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

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/celerity/elements/select_list.rb', line 46

def select(value)
  assert_exists
  raise NoValueFoundException, "unknown option with value #{value.inspect} for select_list #{@conditions.inspect}" unless include?(value)

  selected = nil
  matching = @object.getOptions.select do |option|
    next unless matches_option?(option, value)

    selected ||= option.asText
    @container.update_page option.click
  end

  selected
end

#select_valueObject



71
# File 'lib/celerity/watir_compatibility.rb', line 71

alias_method :select_value,     :select

#selected?(value) ⇒ true, false

Returns true if any of the selected options match the given value.

Parameters:

  • value (String, Regexp)

    A value.

Returns:

  • (true, false)

Raises:



82
83
84
85
86
# File 'lib/celerity/elements/select_list.rb', line 82

def selected?(value)
  assert_exists
  raise UnknownObjectException, "unknown option with value #{value.inspect} for select_list #{@conditions.inspect}" unless include?(value)
  !!@object.getOptions.find { |e| matches_option?(e, value) && e.isSelected }
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.



21
22
23
24
# File 'lib/celerity/elements/select_list.rb', line 21

def selected_options
  assert_exists
  @object.getSelectedOptions.map { |e| e.asText.empty? ? e.getLabelAttribute : e.asText }
end

#typeString

Returns ‘select-multiple’ if the select list has the ‘multiple’ attribute, defined, otherwise ‘select-one’.

Returns:

  • (String)


95
96
97
98
# File 'lib/celerity/elements/select_list.rb', line 95

def type
  assert_exists
  'select-' + (@object.hasAttribute('multiple') ? 'multiple' : 'one')
end

#valueString?

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

Returns:

  • (String, nil)


107
108
109
110
111
112
# File 'lib/celerity/elements/select_list.rb', line 107

def value
  assert_exists
  if (option = @object.getSelectedOptions.to_a.first)
    option.getValueAttribute
  end
end