我在 Hospitals 表单中有一个部门的嵌套模型。代码片段如下所示:
<%= f.simple_fields_for :hospital do |h| %>
.
.
.
<%= h.simple_fields_for :departments do |builder| %>
<%= render "department_fields", :f => builder %>
<% end %>
.
.
<% end %>
_department_fields 部分如下所示:
<div class="fields">
<%= f.input :name %>
<%= link_to_remove_fields "remove", f %>
</span>
</div>
因此,表单底部有一个地方供用户输入最多三个部门名称。
我正在使用 Rails 3、Cucumber、Capybara 和 Selenium 进行集成测试。
在 Cucumber 中测试此表单时,是否有一种简单的方法可以填写重复字段?
理想情况下,我希望能够像这样编写我的功能:
And I fill in the first "Name" with "Cardiology"
And I fill in the second "Name" with "Radiology"
有什么方法可以在 Cucumber/Capybara 中轻松近似此值吗?有人已经想出了解决这个问题的一些步骤吗?
最佳答案
Capybara 可以通过使用 inside 来处理这个问题。例如:
And I fill in "Name" with "Radiology" within "fields"
例如,如果您在两个表单区域周围都有 id 为“hospital_fields”和“department_fields”的 div,则可以执行以下操作来区分要填写的字段:
And I fill in "Name" with "Cardiology" within "hospital_fields"
And I fill in "Name" with "Radiology" within "department_fields"
您还可以通过使用文本字段 id 而不是字段标签名称来更具体。例如,如果第一个文本字段的 id 为“hospital_name”,第二个文本字段的 id 为“hospital_department_name”,您可以执行以下操作:
And I fill in "hospital_name" with "Cardiology"
And I fill in "hospital_deparment_name" with "Radiology"
更新:您还可以添加自定义 cucumber 步骤以使用编号输入:
When /^(?:|I )fill in the (\d+)(?:st|nd|rd|th) "([^"]*)" with "([^"]*)"$/ do |num, name, value|
find(:xpath, ".//form/input[@name='#{name}'][#{num}]").set(value)
end
And I fill in the 1st "name" with "Cardiology"
And I fill in the 2nd "name" with "Radiology"
UPDATE:要与标签匹配,请使用以下表达式:
find(:xpath, ".//input[@id=//label[contains(.,'#{name}')]/@for][#{num}]").set(value)
关于ruby-on-rails - 用 Cucumber 测试嵌套模型表单的简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5794820/