I D*_*ani 2 html rspec capybara
我有一组使用 bootstrap 的单选按钮,并希望使用 Capybara 使用 id 或 value 来选择它们。对于我的 rspec 测试。由于某种原因,它没有看到单选按钮,我想知道是否有人可以看看并提出一些建议。
我得到的错误是
Capybara::ElementNotFound:无法找到未禁用的可见单选按钮“paper” 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>`
Run Code Online (Sandbox Code Playgroud)
测试
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
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激。谢谢
Capybara 告诉您它找不到单选按钮,因为它们实际上在页面上不可见。最有可能的是,如果您检查页面,您会发现 bootstrap 隐藏了实际的单选按钮(可能使用visibility: hiddenCSS)并用图像替换它们,以便样式在浏览器中保持一致。解决此问题的一种方法是允许水豚单击,label而不需要专门单击单选按钮:
choose('scissors', allow_label_click: true)
Run Code Online (Sandbox Code Playgroud)
label您还可以通过设置默认允许点击Capybara.automatic_label_click = true
注意:您的示例 HTML 是非法的,因为纸张label和input都有id="paper"- 我假设这意味着类似于“剪刀”选项,其中id仅在输入上。