Get selected value of dropdown list for testing capybara

I need to write tests for a website. I am trying to get the selected value of a dropdown. So far I can get the contents of the dropdown menu by doing

find_field('restrictions__rating_movies').text 

returns - Do not allow movies r PG M R13 R15 R16 R18 r RP16 Allow all films

I can get the value of the selected object.

 find_field('restrictions__rating_movies').value 

returns - 1000

This doesn’t help much, because I am trying to get the text of the selected item from the drop-down list.

 <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 it is shown that I need to get the value "Allow all movies", I tried many different combinations of the two above examples.

+45
ruby-on-rails rspec capybara
Jul 16 2018-12-12T00:
source share
5 answers
 find_field('restrictions__rating_movies').find('option[selected]').text 
+35
Jul 16 2018-12-14T00:
source share

There have_select if you use Capybara with Rspec:

 expect(page).to have_select('my-select', selected: 'Option 2') 
+126
Sep 04 '13 at 20:15
source share

A very simple way to get the value of the selected option:

 find("#restrictions__rating_movies").value 

This will return the selected option value.

+10
Aug 26 '14 at 4:00 p.m.
source share

If you only need to claim that the field is selected with the given option, a direct answer

 #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 name of your question is "Get the selection value for the drop-down list . " And I had a similar problem when I would like not only to approve the selection, but also to extract the text and value of the selected field. I did not find a direct path to the API. The easiest way I've found is: #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 complete error message when the statement fails.

If there are several options, you can use #select instead of #find, as in #all("option").select &:selected? . It will return an array.

This answer does not rely on the 'option [selected]' trick as the previous one, so it works even if the selection is done by Javascript (which was the reason that the previous answers did not work for me at all).

Tested:

 capybara (2.2.1) capybara-webkit (1.1.0) cucumber (1.3.14) cucumber-rails (1.4.0) 
+3
Jun 28 '14 at 15:58
source share

Something like this work?

 within("//select[@id='restrictions__rating_movies']") do find_field("//option[@selected='selected']").text end 
-one
Jul 16 '12 at 9:25
source share



All Articles