Skip to content

Instantly share code, notes, and snippets.

@castro-raul
Forked from thijsc/gist:1391107
Created January 31, 2012 00:14
Show Gist options
  • Select an option

  • Save castro-raul/1707725 to your computer and use it in GitHub Desktop.

Select an option

Save castro-raul/1707725 to your computer and use it in GitHub Desktop.
Select item from chosen js select with Capybara and Selenium
def select_from_chosen(item_text, options)
field_id = find_field(options[:from])[:id]
within "##{field_id}_chzn" do
find('a.chzn-single').click
input = find("div.chzn-search input").native
input.send_keys(item_text)
find('ul.chzn-results').click
input.send_key(:arrow_down, :return)
within 'a.chzn-single' do
page.should have_content item_text
end
end
end
def select_from_multi_chosen(item_text, options)
field_id = find_field(options[:from])[:id]
within "##{field_id}_chzn" do
input = find("ul.chzn-choices input").native
input.click
input.send_keys(item_text)
input.send_key(:return)
within 'ul.chzn-choices' do
page.should have_content item_text
end
end
end
@clyfe
Copy link
Copy Markdown

clyfe commented Apr 7, 2012

On my system (rspec 2.0.9, capybara 1.1.2) I have to click the element first to focus it then enter keys, to make it work.

  def select_from_multi_chosen(item_text, options)
    field_id = find_field(options[:from])[:id]
    within "##{field_id}_chzn" do
      input = find("ul.chzn-choices input").native
      input.click
      input.send_keys(item_text)
      input.send_key(:return)
      within 'ul.chzn-choices' do
        page.should have_content item_text
      end
    end
  end

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