水豚:找不到 collection_select 和 text_field_tag 的 css
Capybara: Unable to find css for collection_select and text_field_tag
我有一个 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 和输入,我这样写:
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
找到并 select 并填充上述选项?
select 的 from
选项用于通过名称、id、test_id 属性或标签文本定位
select "2", from: "test_id" # find by id
select "2", from: "test[id]" # find by name
fill_in "input_test_questions", with: "1"
应该按照显示的 HTML 所写的那样工作,除非您有 JS/CSS 隐藏页面上文本输入的行为。如果您隐藏它,那么 Capybara 将无法填写它,因为用户将无法填写。
注意:您显示的错误消息 Capybara::ElementNotFound: Unable to find css "#test_id"
应该不可能来自您显示的代码,除非您使用的是非常旧的 Capybara 版本。希望这只是您所做的其他尝试的错误 copy/paste。
我有一个 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 和输入,我这样写:
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
找到并 select 并填充上述选项?
select 的 from
选项用于通过名称、id、test_id 属性或标签文本定位
select "2", from: "test_id" # find by id
select "2", from: "test[id]" # find by name
fill_in "input_test_questions", with: "1"
应该按照显示的 HTML 所写的那样工作,除非您有 JS/CSS 隐藏页面上文本输入的行为。如果您隐藏它,那么 Capybara 将无法填写它,因为用户将无法填写。
注意:您显示的错误消息 Capybara::ElementNotFound: Unable to find css "#test_id"
应该不可能来自您显示的代码,除非您使用的是非常旧的 Capybara 版本。希望这只是您所做的其他尝试的错误 copy/paste。