Capybara can't find select box for Semantic-ui

泪湿孤枕 提交于 2019-12-23 12:53:05

问题


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?


回答1:


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 :-)




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/34921509/capybara-cant-find-select-box-for-semantic-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!