Module: SeleniumPlus::Elements::Select

Included in:
Selenium::WebDriver::Element
Defined in:
lib/selenium_plus/selenium/select.rb

Instance Method Summary collapse

Instance Method Details

#first_selected_optionObject

Get the first selected option in this select element



51
52
53
54
# File 'lib/selenium_plus/selenium/select.rb', line 51

def first_selected_option
  option = select_raw.find { |e| e.selected? }
  option.text or raise 'no options are selected'
end

#multiple?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/selenium_plus/selenium/select.rb', line 13

def multiple?
  self[:multiple]
end

#select(value) ⇒ Object

Select an option by it’s text or value

Parameters:

  • value (String)

    The text or value of option



21
22
23
24
25
# File 'lib/selenium_plus/selenium/select.rb', line 21

def select(value)
  opt = select_raw.select{ |opt| opt[:value] == value || opt.text == value }
  opt.first.send_keys(:control) if multiple?
  opt.first.click unless opt.empty?
end

#select_optionsArray<String>

Get all options text for this select element

Returns:

  • (Array<String>)


30
31
32
# File 'lib/selenium_plus/selenium/select.rb', line 30

def select_options
  select_raw.map{ |opt| opt.text}
end

#select_rawArray<Selenium::WebDriver::Element>

Get all options for this select element

Returns:



7
8
9
10
11
# File 'lib/selenium_plus/selenium/select.rb', line 7

def select_raw
  if self.tag_name == 'select'
    self.find_all(:css, 'option')
  end
end

#select_valuesArray<String>

Get all option’s values for this select element

Returns:

  • (Array<String>)


37
38
39
# File 'lib/selenium_plus/selenium/select.rb', line 37

def select_values
  select_raw.map{ |opt| opt[:value]}
end

#selected_optionsArray<String>

Get all selected options text for this select element

Returns:

  • (Array<String>)


45
46
47
# File 'lib/selenium_plus/selenium/select.rb', line 45

def selected_options
  select_raw.select { |e| e.selected? }.map{ |e| e.text}
end