【发布时间】:2017-05-06 13:34:48
【问题描述】:
有在线帮助将 select2 与 capybara 结合使用(请参阅下面的链接),但据我所见,对于 select2 下拉字段没有帮助。我尝试了各种各样的事情,包括尝试在 :js => false 时填写该字段(使用类似于 find(:xpath, "//input[@id='product_manufacturer_id']").set "Test product manufacturer" 的内容)或对其他 select2 字段有效的解决方案的变体(请参阅下面给出的链接)。到目前为止,没有什么对我有用。
关于我的配置的注意事项:
- 使用 select2 版本 3
- 使用 capybara WebKit
- 这个特定的字段也使用简单的形式,并且是一个关联(
f.association...也是如此)。特别是,ProductManufacturer实例与产品之间存在has_many关系。 - select2 下拉字段应该由匹配查询文本(即您在搜索字段中键入的文本)的
ProductManufacturer实例动态填充。
如果了解我如何实现工厂会有所帮助:
这是我的工厂文件:
factory :product do
name "Test product"
url { Faker::Name.name.parameterize }
access_level 1
product_manufacturer
end
factory :product_manufacturer do
name "Test product manufacturer"
factory :product_manufacturer_with_product do
transient do
products_count 1
end
after(:create) do |product, evaluator|
create_list(:product,
evaluator.products_count,
product: product)
end
end
end
然后,在测试开始之前,我运行:
@product_manufacturer = create(:product_manufacturer)
我最近的尝试: 我的辅助方法(适用于我的其他 select2 字段):
def select2_choose(id, options)
page.execute_script "$('#{id}').trigger('keydown').val('{options[:query]}').trigger('keyup');"
find(".select2-result-label", :text => options[:choose]).click
end
然后我是如何实现的:
select2_choose( "#s2id_autogen7",
:query => @product_manufacturer.name[0...-2],
:choose => @product_manufacturer.name)
输出如下错误信息:
Failure/Error: create_product
Capybara::ElementNotFound:
Unable to find css ".select2-result-label" with text "Test product manufacturer"
(基本意思是找到并点击了下拉框,并且输入了查询文本“测试产品制造”。但是select2没有从数据库中找到选项来找到它。)
注意我已经成功使用factory_girl生成了我的ProductManufacturer实例对象@product_manufacturer,调用puts @product_manufacturer等东西成功,返回实例对象:ProductManufacturer:0x007f0145f9cb38>。
其他相关但未完全解决此问题的问题:
- 选择 select2 下拉菜单(但不是在 capybara 中):
How to select option in drop down using Capybara
Unable to select item in Select2 drop down
- 在 capybara 中选择 select2 选项(但不使用下拉菜单):
How to test a Select2 element with capybara DSL?(注意:我已成功使用此处的答案来选择非下拉 select2 字段)
- 使用 selenium 选择 select2 下拉菜单
【问题讨论】:
标签: rspec capybara factory-bot select2 capybara-webkit