如何使用水豚DSL测试Select2元素? [英] How to test a Select2 element with capybara DSL?

查看:31
本文介绍了如何使用水豚DSL测试Select2元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过Ajax加载结果的页面上有一个 Select2 元素。想要使用capybara / rspec(使用poltergeist驱动程序)进行测试,但是由于Select2元素实际上开始时是一个隐藏字段,然后填充< ul> 处理完结果后,正常的 select fill_in 选择助手会工作。

I have a Select2 element on a page that loads results via ajax. Would like to test this with capybara/rspec (using the poltergeist driver), but since the Select2 element actually starts out as a hidden field, and then populates a <ul> once results are processed, none of the normal select, fill_in or choose helpers will work.

我现在所拥有的就像

  it "should have a search field for events" do
    click_link "Select an Event"       # this works as expected
    within('.select2-container') do    # works
      find('.select2-search input').set(event.description[0,5])  # won't work!
      find(:xpath, "li[text()='#{event.description}']").click 
    end
    click_button "Search"
    page.should have_content("Displaying all 2 photos")
  end

上面的第4行,显然水豚可以找到元素,但其值未更改,并且Select2从不进行其ajax调用。 (由于搜索字段元素没有标签,ID或名称属性,因此不能在其中使用普通的 fill_in

line 4 above, apparently capybara can find the element but the value isn't changed and Select2 never makes its ajax call. (can't use normal fill_in there because the search field element has no label, id, or name attributes)

推荐答案

停止挠头,这样做就可以了

Stop scratching your head, do this and it will work

select "option_name_here", :from => "Id Of Your select field"

我尝试了每个模块和我该死的所有事情,最后才做了无需任何帮助,只需一小段代码即可完成这些工作。

I tried every module and every damn thing I could and finally just did this without any helper and such things by a simple line of code.

这篇关于如何使用水豚DSL测试Select2元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆