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

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, #focused?, #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, #del, #dels, #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, #ins, #inses, #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

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



28
29
30
# File 'lib/celerity/elements/select_list.rb', line 28

def clear
  @object.getSelectedOptions.each { |e| e.setSelected(false) } if multiple?
end

#clearSelectionObject



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

alias_method :clearSelection,   :clear

#getAllContentsObject



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

alias_method :getAllContents,   :options

#getSelectedItemsObject



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

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)


86
87
88
89
# File 'lib/celerity/elements/select_list.rb', line 86

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

#includes?Object



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

alias_method :includes?,        :include?

#multiple?Boolean

Returns true if the select list supports multiple selections

Returns:

  • (Boolean)


121
122
123
# File 'lib/celerity/elements/select_list.rb', line 121

def multiple?
  type == "select-multiple"
end

#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
# File 'lib/celerity/elements/select_list.rb', line 10

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

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

Select the option(s) whose text or label matches the given string. If several options match the value given, all will be selected.

Parameters:

  • value (String, Regexp)

    A value.

Returns:

  • (String)

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

Raises:



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

def select(value)
  assert_exists

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

    selected ||= option.asText
    option.click
  end

  unless selected
    raise NoValueFoundException, "unknown option with value #{value.inspect} for select_list #{@conditions.inspect}"
  end

  selected
end

#select_value(value) ⇒ 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:



68
69
70
71
72
73
74
75
76
77
# File 'lib/celerity/elements/select_list.rb', line 68

def select_value(value)
  assert_exists
  selected = @object.getOptions.map { |e| e.click if Util.matches?(e.getValueAttribute, value) }.compact.first

  unless selected
    raise NoValueFoundException, "unknown option with value #{value.inspect} for select_list #{@conditions.inspect}"
  end

  selected.asText
end

#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:



99
100
101
102
103
# File 'lib/celerity/elements/select_list.rb', line 99

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.



19
20
21
22
# File 'lib/celerity/elements/select_list.rb', line 19

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)


112
113
114
115
# File 'lib/celerity/elements/select_list.rb', line 112

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)


132
133
134
135
136
137
# File 'lib/celerity/elements/select_list.rb', line 132

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