首页 文章

使用capybara选择一个bootstrap单选按钮

提问于
浏览
1

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

我得到的错误是

Capybara :: ElementNotFound:无法找到未被禁用的可见单选按钮“纸张”Capybara :: ElementNotFound:无法找到未禁用的可见单选按钮“#paper”

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

任何帮助将非常感激 . 谢谢

1 回答

  • 1

    Capybara告诉你它无法找到单选按钮,因为它们实际上在页面上看不到 . 最有可能的是,如果您检查页面,您将看到引导程序隐藏了实际的单选按钮(可能具有可见性:隐藏的CSS)并用图像替换它们,因此样式在浏览器中是一致的 . 一个解决方案是允许Capybara点击标签,而不是需要专门点击单选按钮

    choose('scissors', allow_label_click: true)
    

    您还可以通过设置 Capybara.automatic_label_click = true 来默认允许标签点击

    注意:您的示例HTML是非法的,因为纸质标签和输入都有 id="paper" - 我假设它就像"scissors"选项,其中id仅在输入上 .

相关问题