我有一个这样的collection_select
和text_field_tag
:
<%= form_tag method_path(@test.id), method: :get do %>
<%= collection_select(:test, :id, Test.all, :id, :id, prompt: true, include_blank: 'Select Test') %>
<%= text_field_tag(:input_test_questions, 'Test ids') %>
<%= submit_tag "Add" %>
<% end %>
这将生成以下html:
<select name="test[id]" id="test_id"><option value="">Select Test</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="text" name="input_test_questions" id="input_test_questions" value="Test ids">
<input type="submit" name="commit" value="Add">
我正在尝试使用Capybara
为上面的内容编写集成测试。要选择并输入我写的内容,如下所示:
select "2", from: "#test_id"
fill_in "input_test_questions", with: "1"
但是我得到了以下错误:
Capybara::ElementNotFound: Unable to find css "#test_id"
Capybara::ElementNotFound: Unable to find field "input_test_questions" that is not disabled
我如何纠正这个错误,以便Capybara
找到并选择并填充上述选项?
发布于 2019-06-23 15:47:55
select的from
选项用于通过名称、id、test_id属性或标签文本来定位元素。它不需要CSS选择器。
select "2", from: "test_id" # find by id
select "2", from: "test[id]" # find by name
fill_in "input_test_questions", with: "1"
应该像所显示的那样工作,除非您的JS/CSS行为隐藏了页面上的文本输入。如果你隐藏了它,那么Capybara就不能填写它,因为用户不能。
注意:你给Capybara::ElementNotFound: Unable to find css "#test_id"
显示的错误信息不可能来自你显示的代码,除非你使用的是一个非常旧的Capybara版本。希望这只是你从其他尝试中复制/粘贴的错误。
https://stackoverflow.com/questions/56725328
复制相似问题