我在Angular.js中实现了以下下拉菜单/ select:
<select id="user_accounts" ng-model="account_chosen"
ng-options="item.name for item in accounts">
<option value="">All</option>
</select>
帐户包含一系列对象:帐户A,帐户B,帐户C,在客户端呈现,供用户查看。
在Capybara RSpec方面,我想整合一个类似的测试:
select('account A', from: "user_accounts")
然而,运行测试时,我收到“无法找到选项'帐户A'”错误。如何配置测试以从下拉列表中正确选择选项?
答案 0 :(得分:2)
假设您有像phantomjs或selenium这样的驱动程序,您可以执行以下操作:
点击文字选择:
find('#user_accounts').find(:xpath, "option[normalize-space(text())='account A']").select_option
或检查选择框是否包含textitems:
element=find('#user_accounts')
element.should have_selector(:xpath, "option[normalize-space(text())='account A']")
答案 1 :(得分:0)
您还没有提到您正在使用哪种Capybara驱动程序,但如果您的测试依赖于客户端渲染,则需要确保使用可以执行JavaScript的驱动程序。默认的RackTest驱动程序不能。
有关将驱动程序设置为Selenium或其他支持JavaScript的选项的说明,请参阅https://github.com/jnicklas/capybara#drivers。