2016-02-09 25 views
0

我想使用Capybara测试我的应用程序。用户检查一些表格,点击确认并且应用程序显示该消息。但我无法访问循环中生成的任何复选框。水豚。如何检查在循环中生成的复选框

index.html.erb

<%= form_for(@user, :html => {:class => 'form-horizontal'}) do |f| %> 
...... 
<div id="tables" class="transitions-enabled"> 
    <% @tables.each do |table| %> 
     ...... 
     <%= label_tag 'table-check-box-' + table.id.to_s, 'Apply for table ' + table.id.to_s %> 
     <%= check_box_tag 'user[poker_table_ids][]', table.id, nil, id: ('table-check-box-' + table.id.to_s) %> 
    <% end %> 
</div> 

index.html generated from index.html.erb

<form class="form-horizontal" id="new_user" action="/users" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="YSbO1s8fXdB9oKDaN49GQJwg09W7ZnJSAui+hNDkoD/g3T3RPoM/HsAEftnzVk2Ss/Y0VpDzA58g80j2t9c9rQ==" /> 
<div class="form-group has-feedback"> 
    <label for="email" class="col-sm-2 control-label">Email</label> 
    <div class="col-sm-6"> 
    <input type="email" class="form-control" id="email" name="user[email]" /> 
    <span class="glyphicon form-control-feedback"></span> 
    </div> 
</div> 
<div class="form-group"> 
    <div class="col-sm-offset-4 col-sm-4"> 
    <input type="submit" name="commit" value="Submit for Tables" id="submit" class="btn btn-default" /> 
    </div> 
</div> 

<br> 

<div id="tables" class="transitions-enabled"> 
     <div class="box panel panel-default"> 
     <div class="row"> 
      <div class="col-md-6 col-md-offset-3"> 
      <h3>|15| Table 1</h3> 
      </div> 
     </div> 
     <hr> 
     <div class="row"> 
      <div class="col-md-4 col-md-offset-1"> 
      <p>2016/2/10</p> 
      </div> 
      <div class="col-md-4 col-md-offset-1"> 
      <p>Starts at 20:40</p> 
      </div> 
     </div> 
     <hr> 
     <div class="row"> 
      <div class="col-md-6 col-md-offset-1"> 
      <p>Players: 1</p> 
      </div> 

      <div class="col-md-2 col-md-offset-1"> 
       <label for="table-check-box-15">Apply for table 15</label> 
       <input type="checkbox" name="user[poker_table_ids][]" id="table-check-box-15" value="15" /> 
      </div> 
     </div> 
     </div> 
    </div> 
</form> 

submit_for_tables_spec.rb

... 
scenario 'should submit for tables' do 
    visit root_path 
    fill_in 'Email', with: '[email protected]' 
    check('Apply for table 15') 
    click_on('Submit for Tables') 
    expect(page).to have_content('Tables were successfully assigned') 
end 

当我运行这个测试,它说: 故障/错误:检查( '申请表15') 水豚: :ElementNotFound: 无法找到复选框“申请表15”。带有id - 15的表存在。你们能帮我解决这个问题吗?

+0

我怀疑你在'Apply for table'字符串中缺少一个空格字符,所以不是将它标记为“申请表15”,而是将它标记为“申请table15”。 – shoover

+0

shoover,谢谢你的回答,但是我在发布这个问题时意外错过了一个空间。问题仍然存在。当我在循环外添加一个check_box_tag时,水豚可以找到它,但是在循环内部 - 不能 –

+0

发布实际的html而不是erb –

回答

0

您可以尝试使用以下的方法之一选择复选框:

within(#tables) 
    check('Apply for table 15') 
end 

OR

within(.transitions-enabled) 
    check('Apply for table 15') 
end 

OR

find(:css, "#table-check-box-15").set(true) 

OR

page.execute_script("$('#table-check-box-15').prop('checked', true);") 

希望这会有所帮助:)

+0

这一切都为我工作。我在Windows上工作,不能安装capybara-webkit,所以,我想这就是为什么我面临这个问题 –

相关问题