我需要填写Capybara/RSpec的输入字段.
<div class="form-group control_center_operators_name">
<label class="control-label"
for="control_center_operators_attributes_1477562669357_name">
Name
</label>
<input class="form-control"
type="text" value=""
name="control_center[operators_attributes][1477562669357][name]"
id="control_center_operators_attributes_1477562669357_name" />
</div>
Run Code Online (Sandbox Code Playgroud)
我的问题是,有更多这样的字段,用户可以编辑现有的运算符.例如
<div class="form-group control_center_operators_name">
<label class="control-label"
for="control_center_operators_attributes_0_name">
Name
</label>
<input class="form-control"
type="text" value="ABC"
name="control_center[operators_attributes][0][name]"
id="control_center_operators_attributes_0_name" />
</div>
Run Code Online (Sandbox Code Playgroud)
我对这个领域的了解:
更多限制:
value=''字段,因为有4个输入字段与此查询匹配.如何用Capybara/CSS填写空名称,电话,电子邮件和主页字段?
用于寻找场地的Capybara方法是#find_field.您也可以使用:with选项按值过滤
find_field('Name', with: '').set('new value to set')
Run Code Online (Sandbox Code Playgroud)
请注意,将匹配元素的当前值属性,如果自页面加载后字段的值已更改,则可能与value属性不同.另一种选择是将一个包装元素作为范围,使该字段唯一,然后在该元素中找到.例如,如果这些字段集中的每一个都在带有运算符类的div中,并且您想要在最后一个运算符集中填写名称,那么您可以执行此操作
all('.operator', minimum: 1).last.fill_in('Name', with: 'new value to set')
Run Code Online (Sandbox Code Playgroud)
更新:截至Capybara 3 all默认具有等待行为,因此minimum不需要该选项
all('.operator').last.fill_in('Name', with: 'new value to set')
Run Code Online (Sandbox Code Playgroud)