【问题标题】:Capybara. How to check a checkbox generated in a loop水豚。如何检查循环中生成的复选框
【发布时间】:2016-05-19 18:12:21
【问题描述】:

我想使用 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.erb 生成的 index.html

<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: 'test@gmail.com'
   check('Apply for table 15')
   click_on('Submit for Tables')
   expect(page).to have_content('Tables were successfully assigned')
end

当我运行这个测试时,它说: 失败/错误:check('申请表 15') 水豚::ElementNotFound: 找不到“申请表 15”复选框。存在 id - 15 的表。你们能帮我解决这个问题吗?

【问题讨论】:

  • 我怀疑您的“申请表”字符串中缺少空格字符,因此不是将其标记为“申请表 15”,而是将其标记为“申请表 15”。
  • shoover,感谢您的回答,但我在发布此问题时不小心错过了一个空格。问题仍然在这里。当我在循环外添加 check_box_tag 时,水豚可以找到它,但在循环内 - 不能
  • 发布实际生成的html而不是erb
  • 您是否隐藏了复选框,只是设置标签以显示选中/未选中状态?
  • 汤姆·沃波尔,不,我没有

标签: ruby-on-rails checkbox rspec capybara


【解决方案1】:

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

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

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

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

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

希望这会有所帮助:)

【讨论】:

  • 这些都不适合我。我在 Windows 上工作,无法安装 capybara-webkit,所以,我想,这就是我面临这个问题的原因
猜你喜欢
  • 2012-01-08
  • 1970-01-01
  • 2021-03-28
  • 2017-10-07
  • 1970-01-01
  • 2023-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多