我想用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从index.html.erb生成
<form class="form-horizontal" id="new_user" action="/users" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" /><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: 'test@gmail.com'
check('Apply for table 15')
click_on('Submit for Tables')
expect(page).to have_content('Tables were successfully assigned')
end
当我运行此测试时,它说: 失败/错误:检查(&#39;申请表15&#39;) 水豚:: ElementNotFound: 无法找到复选框&#34;申请表15&#34;。存在id为15的表。你能帮我解决这个问题吗?
答案 0 :(得分: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);")
希望这会有所帮助:)