Capybara selectbox

27 Dec 2012

Capybara's documentation doesn't explain well.

Here is the method `select`:

- (Object) select(value, options = {})

and here is the description:

Find a select box on the page and select a particular option from it. If the select box is a multiple select, select can be called multiple times to select more than one option. The select box can be found via its name, id or label text. page.select 'March', :from => 'Month' Parameters: value (String) — Which option to select The (Hash{:from => String}) — id, name or label of the select box

It turns out that `select` selects the option by its name, not its value.

Anyway, I've written my own `select_by_value. You can just include it into your ``Rspec```. Here is the code:

def select_by_value(opt_value, options={}) if options.has_key?(:from) find(:select, options[:from]).find(:xpath, "./option[@value='#{opt_value}']").select_option else find(:xpath, "./option[@value='#{opt_value}']").select_option end end

Give it a kudos