html - 使用 capybara 选择 Bootstrap 单选按钮

标签 html rspec capybara

我有一组使用 bootstrap 的单选按钮,我想使用 Capybara 使用 id 或 value 选择它们。对于我的 rspec 测试。出于某种原因,它没有看到单选按钮,我想知道是否有人可以看看并提出一些建议。

我得到的错误是

Capybara::ElementNotFound: Unable to find visible radio button "paper" that is not disabled Capybara::ElementNotFound: Unable to find visible radio button "#paper" that is not disabled

HTML`

        </label>
        <label for="paper" id="paper" class="btn btn-primary choice col-lg-4 col-md-4">
          <input type="radio" value = "paper" name="rps" id="paper">
          <img class="card-img-top img-fluid image-responsive" src="/img/paper.png" alt="Paper">
          </input>

        </label>
        <label for="scissors" class="btn btn-primary choice col-lg-4 col-md-4">
          <input type="radio" value = "scissors" name="rps" id="scissors">
          <img class="card-img-top img-fluid image-responsive" src="/img/scissors.png" alt="Scissors">
          </input>

        </label>
      </div>`

测试

 feature 'Testing if we divert to /result chosing an option and play' do
  scenario 'Choose option and click play' do
    sign_in_and_play
    click_button('Play')
    choose("#paper")
    # choose("paper")
    expect(page).to have_content("Result displayed here")
  end
end

任何帮助将不胜感激。 谢谢

最佳答案

Capybara 告诉您它找不到单选按钮,因为它们实际上在页面上不可见。最有可能的是,如果您检查页面,您会看到 Bootstrap 隐藏了实际的单选按钮(可能使用 visibility: hidden CSS)并用图像替换它们,因此样式在浏览器中保持一致。一种解决方案是允许 Capybara 单击 label 而不是需要专门单击单选按钮:

choose('scissors', allow_label_click: true)

您还可以通过设置 Capybara.automatic_label_click = true

默认允许点击 label

注意:您的示例 HTML 是非法的,因为纸 labelinput 都有 id="paper" - 我假设类似于“剪刀”选项,其中 id 仅在输入中。

关于html - 使用 capybara 选择 Bootstrap 单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49222830/

相关文章:

html - 当表头列宽与表体位于不同表中时将表头列宽与表体对齐

javascript - 使用 javascript 更改图像 src 不起作用

ruby - RSpec 中的 DRY 双倍

mocking - rspec 模拟 : verify expectations in it "should" methods?

ruby - 是否可以在不使用 Rails 的情况下使用 Cucumber 测试大量 javascript 的 Web 应用程序?

html - SCSS Contrast(来自 Compass)输出与 LESS 的对比度不同

javascript - 可以使用图像作为 HTML slider 的背景吗?

rspec - capybara 困惑 "Matchers"

ruby-on-rails - Rails 6 所有 validate_presence_of 测试均失败

ruby-on-rails - RSpec 中意外的 nil 变量