4

I use capybara with capybara-webkit and Semantic-ui, but it seams that dropdowns doesn't work out of box, because <select> element is hidden:

# feature_spec.rb
select 'option1', from: 'Options'

$ rspec feature_spec.rb

Capybara::ElementNotFound:
  Unable to find select box "Options"

Do you have working solutions for this?

boblin
  • 3,541
  • 4
  • 25
  • 29

3 Answers3

5

I've created this helper:

# for Semantic-ui dropdown
def select_from_dropdown(item_text, options)
  # find dropdown selector
  dropdown = find_field(options[:from], visible: false).first(:xpath,".//..")
  # click on dropdown
  dropdown.click
  # click on menu item
  dropdown.find(".menu .item", :text => item_text).click
end

# in spec
select_from_dropdown 'option1', from: 'Options'

I hope it helps :-)

boblin
  • 3,541
  • 4
  • 25
  • 29
0

You could also do this:

execute_script('$("#Options").dropdown("set selected", "option1");')

execute_script allows you to run scripts in your tests. It uses the semantic-ui method to select the option that you want from the dropdown.

  • Welcome to StackOverflow! When you post any code, you should describe it so that not only the asker, but the entire community, should be able to understand your code easily. – BusyProgrammer Mar 08 '17 at 23:48
  • Thanks! I thought that code was pretty self-explanatory! Anyway I edited it in case people need it. – Pete Tyldesley Mar 09 '17 at 03:11
0

After two days of searching and reading, this article was amongst one of a few that was helpful. Hopefully, this can help someone else!

I created a few methods like so, excuse the naming..I changed it.

def some_dropdown(id, text)
  dropdown = find(id).click
  dropdown.first('option', text: text).select_option
end

def select_form
  within 'content#id' do
    some_dropdown('#id', text)

    click_link_or_button 'Submit'
  end
end

I also referenced this. Also try waiting, sleep and visible: false

jasmineq
  • 521
  • 5
  • 7