
Untitled
By: a guest on
Jun 21st, 2012 | syntax:
None | size: 0.93 KB | hits: 71 | expires: Never
dropdown with selected item, how to get its name/ label using capybara
<select id="my-dropdown" name="my-dropdown">
<option value="1">Peter</option>
<option value="2" selected>Pan</option>
</select>
find_field("#my-dropdown").value
find_field("#my-dropdown").label
find_field('#my-dropdown option[selected]').text
find_field('My Dropdown').find('option[selected]').text
#based on Capybara::Node::Actions#select
def find_select_option(select_finder, option_finder)
no_select_msg = "cannot select option, no select box with id, name, or label '#{select_finder}' found"
no_option_msg = "cannot select option, no option with text '#{option_finder}' in select box '#{select_finder}'"
select = find(:xpath, XPath::HTML.select(select_finder), :message => no_select_msg)
select.find(:xpath, XPath::HTML.option(option_finder), :message => no_option_msg)
end
find_select_option('Countries', 'United States').should be_selected