2016-02-09 26 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,所以,我想這就是爲什麼我面臨這個問題 –

相關問題