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?, #focused?, #hash, #height, #hover, #id, #in_viewport?, #initialize, #inspect, #locate, #located?, #location, #obscured?, #present?, #reset!, #right_click, #selector_string, #send_keys, #shadow_root, #size, #stale?, #stale_in_context?, #style, #tag_name, #to_subtype, #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 SearchContext

#assert_exists, #check_condition, #element_call, #exists?, #unknown_exception, #wait_for_exists

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_while

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



9
10
11
12
13
# File 'lib/watir/elements/select.rb', line 9

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

  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)


22
23
24
# File 'lib/watir/elements/select.rb', line 22

def include?(str_or_rx)
  option(text: str_or_rx).exist? || option(label: str_or_rx).exist? || option(value: 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, text: nil, value: nil, label: nil) ⇒ String Also known as: set

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:



34
35
36
37
38
39
40
41
42
# File 'lib/watir/elements/select.rb', line 34

def select(*str_or_rx, text: nil, value: nil, label: nil)
  key, value = parse_select_args(str_or_rx, text, value, label)

  if value.size > 1 || value.first.is_a?(Array)
    value.flatten.map { |v| select_all_by key, v }.first
  else
    select_matching([find_option(key, value.flatten.first)])
  end
end

#select!(*str_or_rx, text: nil, value: nil, label: nil) ⇒ Object

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

Parameters:

  • str_or_rx (String, Regexp)

Raises:



52
53
54
55
56
57
58
59
60
# File 'lib/watir/elements/select.rb', line 52

def select!(*str_or_rx, text: nil, value: nil, label: nil)
  key, value = parse_select_args(str_or_rx, text, value, label)

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

#selected?(*str_or_rx, text: nil, value: nil, label: nil) ⇒ 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:



70
71
72
73
74
# File 'lib/watir/elements/select.rb', line 70

def selected?(*str_or_rx, text: nil, value: nil, label: nil)
  key, value = parse_select_args(str_or_rx, text, value, label)

  find_option(key, value.first).selected?
end

#selected_optionsArray<Watir::Option>

Returns an array of currently selected options.

Returns:



104
105
106
# File 'lib/watir/elements/select.rb', line 104

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)

#textObject

TODO: What is default behavior without #first ?



95
96
97
# File 'lib/watir/elements/select.rb', line 95

def text
  selected_options.first&.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



83
84
85
# File 'lib/watir/elements/select.rb', line 83

def value
  selected_options.first&.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)