0

I'm trying to get the selected value of a dropdown box with next code:

scenario 'USA as default country' do
  expect(page).to have_select('report_country_code', selected: 'United States')
end

Html for this field is the next:

<select class="form-control country required" name="report[country_code]" id="report_country_code">
  <option value="US">United States</option>
  ....
  ....
  <option value="ZW">Zimbabwe</option>
</select>

And the error looks like:

expected to find select box "report_country_code" with "United States" selected but there were no matches. Also found "United States .... Zimbabwe", which matched the selector but not all filters.
verrom
  • 419
  • 6
  • 18
  • `have_select(..., selected: '...')` is correct, but the error is saying that your page has the `select` tag but `"United States"` is not selected. And indeed in your sample HTML the "United States" option does not have the `selected` property, which would explain the error. – Chris Salzberg Sep 21 '16 at 13:19
  • @shioyama, thanks, that clear now. But why I can test this form and do not to select Country (this field is neccessary)? And when I visit /new form in browser, I see "United States" in this field.. – verrom Sep 21 '16 at 13:23
  • I suppose it appears selected because it is the first in the list of options. – Chris Salzberg Sep 21 '16 at 13:26
  • Since you have a 'required' class on the element and not the required attribute, I assume you're using a JS library to do the client side validation rather than just letting the browser handle it. In that case the JS library may be assigning the first option by default when run in a browser that supports JS. If you're running your tests using the rack-test driver that won't happen (no JS support), so nothing is selected. – Thomas Walpole Sep 21 '16 at 18:19
  • Thanks a lot! I've changed the logic of my test. – verrom Sep 23 '16 at 10:05

1 Answers1

0

Had same problem. How to install lxml on Ubuntu helped me by

sudo apt-get install libxml2-dev