Get select value of dropdown for capybara testing

Ruby on-RailsRspecCapybara

Ruby on-Rails Problem Overview


I have to write tests for a web site. I am trying to get the selected value of a dropdown box. So far i can get the contents of the dropdown by doing

find_field('restrictions__rating_movies').text

returns - Don't Allow Movies G PG M R13 R15 R16 R18 R RP16 Allow All Movies

I can get the value of the selected object.

find_field('restrictions__rating_movies').value

returns - 1000

This does not help me much though because i am trying to get the text of the selected item from a drop down box.

<select class="" id="restrictions__rating_movies" name="restrictions[][rating_movies]">            
<option value="0">Don't Allow Movies</option>
<option value="100">G</option>
<option value="200">PG</option>
<option value="300">M</option>
<option value="325">R13</option>
<option value="350">R15</option>
<option value="375">R16</option>
<option value="400">R18</option>
<option value="500">R</option>
<option value="600">RP16</option>
<option value="1000" selected="selected">Allow All Movies</option></select>

in this case shown i need to get the value 'Allow All Movies' I have tried many different combinations of the above two examples.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

There's a have_select matcher if you use Capybara with Rspec:

expect(page).to have_select(
  'my-select',         # locator
  selected: 'Option 2' # option
) 
Parameters:

Locator (String) (defaults to: nil) — The label, name or id of a select box Options (Hash) using :selected (String, Array) — Options which should be selected

Solution 2 - Ruby on-Rails

find_field('restrictions__rating_movies').find('option[selected]').text

Solution 3 - Ruby on-Rails

Very simple way to get value of selected option is:

find("#restrictions__rating_movies").value

This will return selected select option value.

Solution 4 - Ruby on-Rails

If you only need to assert if a field is selected with a given option, the straightforward answer is

#Find a select box by (label) name or id and assert the given text is selected
When /^select box "([^"]*)" is selected with "([^"]*)"$/ do |dropdown, selected_text|    
  assert page.has_select?(dropdown, selected: selected_text)
end

Source: http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers#has_select%3F-instance_method

But the title of your question is "Get select value for dropdown". And I've run into a similar problem where I would like not only to assert the selection, but also retrieve the text and value of the selected field. I've found no straight way on API. The easiest way I've found was: #all("option").find &:selected?

When /^ select box "([^"]*)" is selected with "([^"]*)"$/ do |dropdown, selected_text|
  sb = find_field(dropdown)
  sb_selected = sb.all("option").find &:selected?
  msg = "Selected: #{sb_selected.text.inspect} - value:#{sb_selected.value.inspect}"
  assert page.has_select?(dropdown, selected: selected_text), msg
end

This gives me a more comprehensive error message when the assertion fails.

If there's multiple selections you can use #select in place of #find, as in #all("option").select &:selected?. It will return an Array.

This answer doesn't rely on the 'option[selected]' trick as the previous ones, so it works even if the selection is done by Javascript (which was the reason why the previous answers didn't work for me at all).

Tested on:

capybara (2.2.1)
capybara-webkit (1.1.0)
cucumber (1.3.14)
cucumber-rails (1.4.0)

Solution 5 - Ruby on-Rails

If you want to find the current selected text, without assuming what it might be so that you can just compare it to an expectation, the following works even if the selection was made by JS (so that there is no 'option[selected]').

First I find the value of the select, then I find the text of the option with that value:

  def selected(selector)
    value = find(selector).value
    text = find(selector).find("option[value='#{value}']").text
  end

Solution 6 - Ruby on-Rails

Create a simple function to return the text given a select element (dropdown):

def get_dropdown_selected_item_text(dropdown)
  value = dropdown.value
  return dropdown.all(:css, "option").select {|opt| opt.value == value} .first.text
end

Solution 7 - Ruby on-Rails

Would something like this work?

within("//select[@id='restrictions__rating_movies']") do
  find_field("//option[@selected='selected']").text
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrandonView Question on Stackoverflow
Solution 1 - Ruby on-RailsgylazView Answer on Stackoverflow
Solution 2 - Ruby on-RailsBrandonView Answer on Stackoverflow
Solution 3 - Ruby on-RailsVijay ChouhanView Answer on Stackoverflow
Solution 4 - Ruby on-RailsAbinoam Jr.View Answer on Stackoverflow
Solution 5 - Ruby on-RailsAdamView Answer on Stackoverflow
Solution 6 - Ruby on-RailsSteven SolomonView Answer on Stackoverflow
Solution 7 - Ruby on-RailsFrostView Answer on Stackoverflow