我有一组使用 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 是非法的,因为纸 label
和 input
都有 id="paper"
- 我假设类似于“剪刀”选项,其中 id
仅在输入中。
关于html - 使用 capybara 选择 Bootstrap 单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49222830/