How to select option in drop down using Capybara
If you take a look at the source of the select
method, you can see that what it does when you pass a from
key is essentially:
find(:select, from, options).find(:option, value, options).select_option
In other words, it finds the <select>
you're interested in, then finds the <option>
within that, then calls select_option
on the <option>
node.
You've already pretty much done the first two things, I'd just rearrange them. Then you can tack the select_option
method on the end:
find('#organizationSelect').find(:xpath, 'option[2]').select_option
Capybara, selecting 1st option from dropdown?
You can find the first option element and then use the select_option
method to select it.
For example, if the select list has an id "select_id", you can do:
first('#select_id option').select_option
As @TomWalpole mentions, this will not wait for the element to appear. It would be safer to do one of the following:
first('#select_id option', minimum: 1).select_option
or
find('#select_id option:first-of-type').select_option
Make capybara click a dropdown option
To select an option from dropdown, you need to use select
method and not click_on
:
For example:
select 'CT', from: "building_state"
How to use capybara to select a select2 drop-down field
Capybara's field actions (fill_in, set, etc) only work with basic html form fields, not with JS driven widgets since they usually hide the basic fields. The key to working with any JS driven widget in Capybara is to perform the actions a user would have to perform, which in this case is click on the visible element to trigger the dropdown then click on the element you want to select.
As an example, to select "California" from the first select2 dropdown on the example page http://select2.github.io/examples.html it could be done like
first('.select2-container', minimum: 1).click
find('li.select2-results__option[role="treeitem"]', text: 'California').click
If you want to do it by typing in the search term instead of clicking on the result it would be something like
first('.select2-container', minimum: 1).click
find('.select2-dropdown input.select2-search__field').send_keys("California", :enter)
Using execute_script
and trigger
are bad ideas if you're testing a web app since it bypasses most of the checks on what a user could actually do, they're fine if you're just automating a page
Get select value of dropdown for capybara testing
find_field('restrictions__rating_movies').find('option[selected]').text
Capybara cannot find dropdown list
store_listing = browser.find_link(title: 'Store listing')
store_listing.click
application_type = browser.find(:element, 'div', role: 'button', text: 'Select an application type')
application_type.click
browser.find(:element, 'div', role: 'option', text: 'Applications').click
category_type = browser.find(:element, 'div', role: 'button', text: 'Select a category')
category_type.click
browser.find(:element, 'div', role: 'option', text: 'Shopping').click
browser.fill_in(type: "email", with: "contact@cityhive.net")
browser.click_on('Save draft', wait: 100)
sleep 2
Related Topics
Passing Parameters to Erb View
How to Remove All Non - Ascii Characters from a String in Ruby
How to Install Ruby on Rails in Windows
Ruby SASS, Unable to Resolve Dependancies
The Command Rbenv Install Is Missing
How to Handle Constants in Ruby When Using Rails
Ruby Convert Array into Function Arguments
How to Set an Attr_Accessor for a Dynamic Instance Variable
How to Change the Position of an Array Element
Ruby Loading Config (Yaml) File in Same Dir as Source
Convert String to Specific Datetime Format
Rails Model Has_Many , Belongs_To Relations
How to Password-Protect My /Sidekiq Route (I.E. Require Authentication for the Sidekiq::Web Tool)