Skip to content

Instantly share code, notes, and snippets.

@NaN1488
Forked from erkattak/jquery_chosen_helper.rb
Last active December 15, 2015 09:39
Show Gist options
  • Save NaN1488/5240475 to your computer and use it in GitHub Desktop.
Save NaN1488/5240475 to your computer and use it in GitHub Desktop.
Update to work with chosen jquery plugin. If don't know the id of the select and find option by text rather than id. Whitout using execute_script
# Example:
# chosen_select 'select[name="projects"]', "ProjectA"
#
def chosen_select (css_select, option_text)
element_id = page.find(css_select)['id']
page.driver.browser.mouse.down find("##{element_id}_chzn").native
within("##{element_id}_chzn") do
find(:xpath,"//li[text()=\"#{option_text}\"]").click
end
end
@Paxa
Copy link

Paxa commented Apr 22, 2015

For current version of capybara and choosen this working:

  def chosen_select(css_selector, value)
    page.evaluate_script(%{$('#{css_selector}').val('#{value}').trigger("chosen:updated");})
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment