我正在尝试使用capybara从下拉菜单中选择一个项目。
下拉菜单是使用茧宝石生成的嵌套物品。css id和名称都是动态生成的。
页面源的复制/粘贴看起来像这样:
<div class="form-group select required protocol_step_items_orientation_id col-sm-3" title="Select an orientation for the plane of imaging." data-toggle="tooltip" data-placement="auto" data-delay="{"show":700,"hide":100}" data-animation="true">
<div class="col-sm-9">
<select id="protocol_step_items_attributes_1432720278702_orientation_id" class="select required form-control" name="protocol[step_items_attributes][1432720278702][orientation_id]">
<option value="">Orientation</option>
<option value="74">Axial</option>
<option value="75">Sagittal</option>
<option value="76">Coronal</option>
<option value="77">Sagital Oblique</option>
<option value="78">Coronal Oblique</option>
<option value="79">Axial Oblique</option>
</select>
</div>
</div>
我可以给一个capybara查找器选择什么语法:
<select id="protocol_step_items_attributes_1432720278702_orientation_id" class="select required form-control" name="protocol[step_items_attributes][1432720278702][orientation_id]">
编辑:以下代码生成选择框:
<div class="links" >
<div class="row">
<%= link_to_add_association 'Add Imaging Step', f, :step_items, :wrap_object => Proc.new{|item| item=StepItem.new}, :class=>"btn btn-primary btn-xs add_imaging_step", title: "Click here to add an imaging step.", data: {toggle: "tooltip", placement: "right", animation: true, delay: {show: 700, hide: 100}}%>
<%= link_to_add_association 'Add an Imaging Step List', f, :step_lists, :wrap_object => Proc.new{|item| item=StepList.new}, :class=>"btn btn-primary btn-xs",title: "Click here to add an imaging step list.", data: {toggle: "tooltip", placement: "right", animation: true, delay: {show: 700, hide: 100}}%>
<%= link_to_add_association 'Add an Imaging Tip', f, :tips, :class=>"btn btn-primary btn-xs", title: "Click here to add an imaging tip.", data: {toggle: "tooltip", placement: "right", animation: true, delay: {show: 700, hide: 100}}%>
<%= link_to_add_association 'Add an Image', f, :images, :class=>"btn btn-primary btn-xs", title: "Click here to add an image.", data: {toggle: "tooltip", placement: "right", animation: true, delay: {show: 700, hide: 100}}%>
<%= link_to_add_association 'Add a Document', f, :documents, :class=>"btn btn-primary btn-xs", title: "Click here to add a document.", data: {toggle: "tooltip", placement: "right", animation: true, delay: {show: 700, hide: 100}}%>
</div><!--row-->
EDIT2:我想答案可能在这里:
使用rspec和capybara测试茧动态添加的字段
或此处:
从下拉菜单中选择没有id的元素
…但我似乎不能找出正确的语法给水豚finder。
您可以为下拉列表指定一个id。然后你可以通过id选择元素。
下面的链接告诉你如何通过id选择元素。http://www.rubydoc.info/github/jnicklas/capybara/Capybara/Node/Finders find_by_id-instance_method
这行得通:
orientations=page.all('html body main form#new_protocol.simple_form.form-horizontal div.form-inline div#step_items.ui-sortable div div#step_lists div div#tips div div#images div div#documents div div.links div.nested-fields div.form-inputs.row div.form-group.select.required.protocol_step_items_orientation_id.col-sm-3 div.col-sm-9 select')
orientations.first.select('Axial')
…但感觉很脆。有没有更好的办法?