Ruby-on-rails 如何检查水豚中的复选框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8297624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check a checkbox in capybara?
提问by John Dow
I'm using Rspec and Capybara.
我正在使用 Rspec 和 Capybara。
How can I write a step to check a checkbox? I've tried checkby value but it can't find my checkbox. I'm not sure what to do, as I have in fact same ID with different values
我怎样才能写一个步骤来检查一个checkbox?我已经check按价值尝试过,但它找不到我的checkbox. 我不知道该怎么办,因为我实际上有不同值的相同 ID
Here is the code:
这是代码:
<input id="cityID" type="checkbox" style="text-align: center; opacity: 0;" value="61" name="cityID">
<input id="cityID" type="checkbox" style="text-align: center; opacity: 0;" value="62" name="cityID">
<input id="cityID" type="checkbox" style="text-align: center; opacity: 0;" value="63" name="cityID">
回答by Jon M
I found the following worked for me:
我发现以下对我有用:
# Check
find(:css, "#cityID[value='62']").set(true)
# Uncheck
find(:css, "#cityID[value='62']").set(false)
回答by installero
It's better not to create multiple elements with the same id, so that (and not only for that) you can easily check/uncheck a checkboxwith elegant
最好不要创建具有相同id 的多个元素,以便(不仅如此)您可以轻松地选中/取消选中带有优雅的复选框
check 'cityID'
uncheck 'cityID'
If one can not avoid multiple elements with the same id and still needs to check a checkbox with certain value, he can do so with
如果无法避免多个具有相同 id 的元素并且仍然需要选中具有特定值的复选框,他可以这样做
find(:css, "#cityID[value='62']").set(true)
find(:css, "#cityID[value='62']").set(false)
More information on capybarainput manipulations can be found here
可以在此处找到有关水豚输入操作的更多信息
回答by p1100i
When running capybara test, you got the pageobject. This you can use to check/uncheck any checkboxes. As @buruzaemon already mentioned:
运行水豚测试时,你得到了page对象。您可以使用它来选中/取消选中任何复选框。正如@buruzaemon 已经提到的:
to find and check a checkbox by name, id, or label text.
按名称、ID 或标签文本查找并选中复选框。
So lets assume you got a checkbox in your html like:
因此,让我们假设您的 html 中有一个复选框,例如:
<label>
<input type="checkbox" value="myvalue" name="myname" id="myid">
MyLabel
</label>
You could check this with:
您可以通过以下方式检查:
page.check('myid')
page.check('MyLabel')
page.check('myname')
Uncheck is the same just use page.uncheckmethod.
取消选中与使用page.uncheck方法相同。
回答by buruzaemon
I think you may have to give unique ids to your form elements, first of all.
我认为您可能必须首先为您的表单元素提供 unique ids。
But with regards to Capybara and checkboxes, the Capybara::Node::Actions#check instance methodwill allow you to find and check a checkbox by name, id, or label text.
但是对于 Capybara 和复选框,Capybara::Node::Actions#check 实例方法将允许您按名称、ID 或标签文本查找和检查复选框。
回答by Obromios
If the box is associated with text, e.g. 'Option 3', then as of capybara 3.0.3you can just do
如果该框与文本相关联,例如“选项 3”,那么从capybara 3.0.3您开始就可以
check 'Option 3'
回答by Michael Cruz
I know this is an older question, but I have been working through this myself, and having tried all of the above, this is what finally worked for me:
我知道这是一个较旧的问题,但我自己一直在解决这个问题,并尝试了上述所有方法,这最终对我有用:
find("input[type='checkbox'][value='#{cityID.id}']").set(true)
Hope this is helpful to someone. I am using Capybara 2.4.4.
希望这对某人有帮助。我正在使用水豚 2.4.4。
回答by Samuel
An old topic but another solution is:
一个古老的话题,但另一个解决方案是:
check('Option 3', allow_label_click: true)
check('Option 3', allow_label_click: true)
回答by user3853159
You can also check that all the checkboxes are not checked with this example.
您还可以检查此示例中是否未选中所有复选框。
all('input[type=checkbox]').each do |checkbox| checkbox.should_not be_checked end
all('input[type=checkbox]').each do |checkbox| checkbox.should_not_checked 结束
回答by HectorPerez
.set(true) didn't work for me so I had to call .click:
.set(true) 对我不起作用,所以我不得不调用 .click:
find(...).click
find(...).click
回答by kulssaka
you can also use :xpath instead of :css if you have some problems finding it.
如果您在查找时遇到问题,也可以使用 :xpath 而不是 :css 。
find(:xpath , '//*[@id="example"]').set(true)
find(:xpath , '//*[@id="example"]').set(true)
on Chrome (and surely other browsers), you can "inspect element" and then by right clicking on the element you are interested in, there is 'copy xpath' if you don't know what xpath was, now you do.
在 Chrome(当然还有其他浏览器)上,您可以“检查元素”,然后通过右键单击您感兴趣的元素,如果您不知道 xpath 是什么,现在可以使用“复制 xpath”。

