Class: Watir::Select

Inherits:
HTMLElement show all
Defined in:
lib/watir/elements/select.rb,
lib/watir/elements/html_elements.rb

Constant Summary

Constants inherited from Element

Element::CASE_INSENSITIVE_ATTRIBUTES

Instance Attribute Summary

Attributes inherited from Element

#keyword, #selector

Instance Method Summary collapse

Methods inherited from HTMLElement

#accesskey, #content_editable, #content_editable?, #dataset, #dir, #draggable?, #hidden?, #innertext, #lang, #onabort, #onblur, #oncancel, #oncanplay, #oncanplaythrough, #onchange, #onclick, #onclose, #oncopy, #oncuechange, #oncut, #ondblclick, #ondrag, #ondragend, #ondragenter, #ondragexit, #ondragleave, #ondragover, #ondragstart, #ondrop, #ondurationchange, #onemptied, #onended, #onerror, #onfocus, #oninput, #oninvalid, #onkeydown, #onkeypress, #onkeyup, #onload, #onloadeddata, #onloadedmetadata, #onloadstart, #onmousedown, #onmouseenter, #onmouseleave, #onmousemove, #onmouseout, #onmouseover, #onmouseup, #onpaste, #onpause, #onplay, #onplaying, #onprogress, #onratechange, #onreset, #onresize, #onscroll, #onseeked, #onseeking, #onselect, #onshow, #onstalled, #onsubmit, #onsuspend, #ontimeupdate, #ontoggle, #onvolumechange, #onwaiting, #onwheel, #spellcheck?, #tabindex, #title, #translate?

Methods inherited from Element

#==, #attribute_list, #attribute_value, #attribute_values, #browser, #build, #cache=, #center, #class_name, #classes, #click, #click!, #double_click, #double_click!, #drag_and_drop_by, #drag_and_drop_on, #driver, #enabled?, #exists?, #focused?, #hash, #height, #hover, #id, #initialize, #inspect, #locate, #located?, #location, #obscured?, #present?, #reset!, #right_click, #scroll_into_view, #selector_string, #send_keys, #size, #stale?, #stale_in_context?, #style, #tag_name, #to_subtype, #visible?, #wd, #width

Methods included from AttributeHelper

#attribute, #attribute_list, #define_attribute, #define_boolean_attribute, #define_float_attribute, #define_int_attribute, #define_string_attribute, #inherit_attributes_from, #method, #typed_attributes

Methods included from Scrolling

#scroll

Methods included from Locators::ClassHelpers

#class_from_string, #element_class_name, #element_matcher_class, #locator, #locator_class, #selector_builder, #selector_builder_class

Methods included from JSExecution

#execute_script, #fire_event, #flash, #focus, #inner_html, #inner_text, #outer_html, #select_text, #selected_text, #text_content

Methods included from Adjacent

#child, #children, #following_sibling, #following_siblings, #parent, #preceding_sibling, #preceding_siblings, #siblings

Methods included from Waitable

#wait_until, #wait_until_present, #wait_while, #wait_while_present

Methods included from EventuallyPresent

#when_enabled, #when_present

Methods included from Container

#a, #abbr, #abbrs, #address, #addresses, #area, #areas, #article, #articles, #as, #aside, #asides, #audio, #audios, #b, #base, #bases, #bdi, #bdis, #bdo, #bdos, #blockquote, #blockquotes, #body, #bodys, #br, #brs, #bs, #button, #buttons, #canvas, #canvases, #caption, #captions, #checkbox, #checkboxes, #circle, #circles, #cite, #cites, #code, #codes, #col, #colgroup, #colgroups, #cols, #data, #datalist, #datalists, #datas, #date_field, #date_fields, #date_time_field, #date_time_fields, #dd, #dds, #defs, #defss, #del, #dels, #desc, #descs, #details, #detailses, #dfn, #dfns, #dialog, #dialogs, #div, #divs, #dl, #dls, #dt, #dts, #element, #elements, #ellipse, #ellipses, #em, #embed, #embeds, #ems, #extract_selector, #field_set, #field_sets, #fieldset, #fieldsets, #figcaption, #figcaptions, #figure, #figures, #file_field, #file_fields, #font, #fonts, #footer, #footers, #foreign_object, #foreign_objects, #forms, #frame, #frames, #frameset, #framesets, #g, #gs, #h1, #h1s, #h2, #h2s, #h3, #h3s, #h4, #h4s, #h5, #h5s, #h6, #h6s, #head, #header, #headers, #heads, #hidden, #hiddens, #hr, #hrs, #html, #htmls, #i, #iframe, #iframes, #image, #images, #img, #imgs, #input, #inputs, #ins, #inses, #is, #kbd, #kbds, #label, #legend, #legends, #li, #line, #linear_gradient, #linear_gradients, #lines, #link, #links, #lis, #main, #mains, #map, #maps, #mark, #marker, #markers, #marks, #meta, #metadata, #metadatas, #metas, #meter, #meters, #nav, #navs, #noscript, #noscripts, #object, #objects, #ol, #ols, #optgroup, #optgroups, #option, #options, #output, #outputs, #p, #param, #params, #path, #paths, #pattern, #patterns, #picture, #pictures, #polygon, #polygons, #polyline, #polylines, #pre, #pres, #progress, #progresses, #ps, #q, #qs, #radial_gradient, #radial_gradients, #radio, #radio_set, #radios, #rb, #rbs, #rect, #rects, #rp, #rps, #rt, #rtc, #rtcs, #rts, #rubies, #ruby, #s, #samp, #samps, #script, #scripts, #section, #sections, #select_list, #select_lists, #selects, #small, #smalls, #source, #sources, #span, #spans, #ss, #stop, #stops, #strong, #strongs, #style, #styles, #sub, #subs, #summaries, #summary, #sup, #sups, #svg, #svgs, #switch, #switches, #symbol, #symbols, #table, #tables, #tbody, #tbodys, #td, #tds, #template, #templates, #text_field, #text_fields, #text_path, #text_paths, #textarea, #textareas, #tfoot, #tfoots, #th, #thead, #theads, #ths, #time, #times, #title, #titles, #tr, #track, #tracks, #trs, #tspan, #tspans, #u, #ul, #uls, #us, #use, #uses, #var, #vars, #video, #videos, #view, #views, #wbr, #wbrs

Methods included from JSSnippets

#execute_js

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

#autocompleteString

Returns value of autocomplete property.

Returns:

  • (String)

    value of autocomplete property


294
# File 'lib/watir/elements/html_elements.rb', line 294

attribute(String, :autocomplete, :autocomplete)

#autofocus?Boolean

Returns value of autofocus property.

Returns:

  • (Boolean)

    value of autofocus property


295
# File 'lib/watir/elements/html_elements.rb', line 295

attribute("Boolean", :autofocus?, :autofocus)

#clearObject

Clears all selected options.

Raises:


7
8
9
10
11
# File 'lib/watir/elements/select.rb', line 7

def clear
  raise Exception::Error, 'you can only clear multi-selects' unless multiple?

  selected_options.each(&:click)
end

#disabled?Boolean

Returns value of disabled property.

Returns:

  • (Boolean)

    value of disabled property


296
# File 'lib/watir/elements/html_elements.rb', line 296

attribute("Boolean", :disabled?, :disabled)

#formString

Returns value of form property.

Returns:

  • (String)

    value of form property


297
# File 'lib/watir/elements/html_elements.rb', line 297

attribute(String, :form, :form)

#include?(str_or_rx) ⇒ Boolean

Returns true if the select list has one or more options where text or label matches the given value.

Parameters:

  • str_or_rx (String, Regexp)

Returns:

  • (Boolean)

20
21
22
# File 'lib/watir/elements/select.rb', line 20

def include?(str_or_rx)
  option(text: str_or_rx).exist? || option(label: str_or_rx).exist?
end

#labelsString

Returns value of labels property.

Returns:

  • (String)

    value of labels property


309
# File 'lib/watir/elements/html_elements.rb', line 309

attribute(String, :labels, :labels)

#lengthInteger

Returns value of length property.

Returns:

  • (Integer)

    value of length property


302
# File 'lib/watir/elements/html_elements.rb', line 302

attribute(Integer, :length, :length)

#multiple?Boolean

Returns value of multiple property.

Returns:

  • (Boolean)

    value of multiple property


298
# File 'lib/watir/elements/html_elements.rb', line 298

attribute("Boolean", :multiple?, :multiple)

#nameString

Returns value of name property.

Returns:

  • (String)

    value of name property


299
# File 'lib/watir/elements/html_elements.rb', line 299

attribute(String, :name, :name)

#required?Boolean

Returns value of required property.

Returns:

  • (Boolean)

    value of required property


300
# File 'lib/watir/elements/html_elements.rb', line 300

attribute("Boolean", :required?, :required)

#select(*str_or_rx) ⇒ String

Select the option whose text or label matches the given string.

Parameters:

  • str_or_rx (String, Regexp)

Returns:

  • (String)

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

Raises:


32
33
34
35
36
37
38
# File 'lib/watir/elements/select.rb', line 32

def select(*str_or_rx)
  if str_or_rx.size > 1 || str_or_rx.first.is_a?(Array)
    str_or_rx.flatten.map { |v| select_all_by v }.first
  else
    str_or_rx.flatten.map { |v| select_by v }.first
  end
end

#select!(*str_or_rx) ⇒ Object

Uses JavaScript to select the option whose text matches the given string.

Parameters:

  • str_or_rx (String, Regexp)

Raises:


64
65
66
67
68
69
70
# File 'lib/watir/elements/select.rb', line 64

def select!(*str_or_rx)
  if str_or_rx.size > 1 || str_or_rx.first.is_a?(Array)
    str_or_rx.flatten.map { |v| select_by! v, :multiple }.first
  else
    str_or_rx.flatten.map { |v| select_by! v, :single }.first
  end
end

#select_all(*str_or_rx) ⇒ String

Select all options whose text or label matches the given string.

Parameters:

  • str_or_rx (String, Regexp)

Returns:

  • (String)

    The text of the first option selected.

Raises:


48
49
50
51
52
53
54
55
# File 'lib/watir/elements/select.rb', line 48

def select_all(*str_or_rx)
  Watir.logger.deprecate('#select_all',
                         '#select with an Array instance',
                         ids: [:select_all])

  results = str_or_rx.flatten.map { |v| select_all_by v }
  results.first
end

#select_all!(*str_or_rx) ⇒ Object

Uses JavaScript to select all options whose text matches the given string.

Parameters:

  • str_or_rx (String, Regexp)

Raises:


79
80
81
82
83
84
85
86
# File 'lib/watir/elements/select.rb', line 79

def select_all!(*str_or_rx)
  Watir.logger.deprecate('#select_all!',
                         '#select! with an Array instance',
                         ids: [:select_all])

  results = str_or_rx.flatten.map { |v| select_by!(v, :multiple) }
  results.first
end

#select_value(str_or_rx) ⇒ String

Selects the option(s) whose value attribute matches the given string.

Parameters:

  • str_or_rx (String, Regexp)

Returns:

  • (String)

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

Raises:

See Also:

  • +select+

98
99
100
101
# File 'lib/watir/elements/select.rb', line 98

def select_value(str_or_rx)
  Watir.logger.deprecate '#select_value', '#select', ids: [:select_value]
  select_by str_or_rx
end

#selected?(str_or_rx) ⇒ Boolean

Returns true if any of the selected options' text or label matches the given value.

Parameters:

  • str_or_rx (String, Regexp)

Returns:

  • (Boolean)

Raises:


111
112
113
114
115
116
117
118
119
120
121
# File 'lib/watir/elements/select.rb', line 111

def selected?(str_or_rx)
  by_text = options(text: str_or_rx)
  return true if by_text.find(&:selected?)

  by_label = options(label: str_or_rx)
  return true if by_label.find(&:selected?)

  return false unless (by_text.size + by_label.size).zero?

  raise(UnknownObjectException, "Unable to locate option matching #{str_or_rx.inspect}")
end

#selected_optionsArray<Watir::Option>

Returns an array of currently selected options.

Returns:


152
153
154
# File 'lib/watir/elements/select.rb', line 152

def selected_options
  element_call { execute_js :selectedOptions, self }
end

#selectedindexInteger

Returns value of selectedIndex property.

Returns:

  • (Integer)

    value of selectedIndex property


304
# File 'lib/watir/elements/html_elements.rb', line 304

attribute(Integer, :selectedindex, :selectedIndex)

#selectedoptionsString

Returns value of selectedOptions property.

Returns:

  • (String)

    value of selectedOptions property


303
# File 'lib/watir/elements/html_elements.rb', line 303

attribute(String, :selectedoptions, :selectedOptions)

#textString?

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

Returns:

  • (String, nil)

142
143
144
145
# File 'lib/watir/elements/select.rb', line 142

def text
  option = selected_options.first
  option&.text
end

#typeString

Returns value of type property.

Returns:

  • (String)

    value of type property


301
# File 'lib/watir/elements/html_elements.rb', line 301

attribute(String, :type, :type)

#validationmessageString

Returns value of validationMessage property.

Returns:

  • (String)

    value of validationMessage property


308
# File 'lib/watir/elements/html_elements.rb', line 308

attribute(String, :validationmessage, :validationMessage)

#validityString

Returns value of validity property.

Returns:

  • (String)

    value of validity property


307
# File 'lib/watir/elements/html_elements.rb', line 307

attribute(String, :validity, :validity)

#valueString

Returns value of value property.

Returns:

  • (String)

    value of value property


130
131
132
133
# File 'lib/watir/elements/select.rb', line 130

def value
  option = selected_options.first
  option&.value
end

#willvalidate?Boolean

Returns value of willValidate property.

Returns:

  • (Boolean)

    value of willValidate property


306
# File 'lib/watir/elements/html_elements.rb', line 306

attribute("Boolean", :willvalidate?, :willValidate)