使用 Capybara 检查复选框 [英] Checking checkboxes with Capybara
问题描述
使用 Capybara 我一辈子都无法在表单上选中一个复选框.
在我尝试过的请求规范中:
check("名字")page.check("名字")page.check("pickem_option_ids_10")find(:css, "#pickem_option_ids_11[value='11']").set(true)find(:css, "#pickem_option_ids_11").set(true)
来自我的表单的片段:
<label class="check_boxes optional control-label">选项:</label><div class="控件"><label class="checkbox"><input class="check_boxes optional" id="pickem_option_ids_10" name="pickem[option_ids][]" type="checkbox" value="10"/>名字标签><label class="checkbox"><input class="check_boxes optional" id="pickem_option_ids_11" name="pickem[option_ids][]" type="checkbox" value="11"/>中间名标签>
我从 这个 SO 线程中得到了一些 find() 的想法一>.
我在其他规范中取得了一些成功,其中我有一个带有 Active 标签的复选框,我只是说 check("Active")
.
今天遇到同样的问题,我环顾四周,这似乎有效:
find(:xpath, "//input[@value='10']").set(true)
当然,你可以用你想要的任何东西替换10"——只需检查你的 HTML 并使用你的值.
希望有所帮助.
Using Capybara I cannot for the life of me select a checkbox on my form.
In my request spec I've tried:
check("First Name")
page.check("First Name")
page.check("pickem_option_ids_10")
find(:css, "#pickem_option_ids_11[value='11']").set(true)
find(:css, "#pickem_option_ids_11").set(true)
Snippet from my form:
<div class="control-group check_boxes optional">
<label class="check_boxes optional control-label">Options:</label>
<div class="controls">
<label class="checkbox">
<input class="check_boxes optional" id="pickem_option_ids_10" name="pickem[option_ids][]" type="checkbox" value="10" />First Name
</label>
<label class="checkbox">
<input class="check_boxes optional" id="pickem_option_ids_11" name="pickem[option_ids][]" type="checkbox" value="11" />Middle Name
</label>
</div>
</div>
I got some of the find() ideas from this SO thread.
I've had some success in other specs where I have a single checkbox with a label of Active and I just say check("Active")
.
Had the same problem today, I looked around and this seemed to work:
find(:xpath, "//input[@value='10']").set(true)
of course you can replace '10' with anything you want - just check your HTML and use your value.
Hope that helps.
这篇关于使用 Capybara 检查复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!