如何在capybara中勾选复选框?

时间:2021-02-03 10:04:22

I'm using Rspec and Capybara.

我用的是Rspec和Capybara。

How can I write a step to check a checkbox? I've tried check by 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

如何编写一个步骤来检查复选框?我试过用value检查但它找不到我的复选框。我不知道该怎么做,因为实际上我有不同值的相同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">

9 个解决方案

#1


134  

I found the following worked for me:

我发现以下方法对我有效:

# Check
find(:css, "#cityID[value='62']").set(true)

# Uncheck
find(:css, "#cityID[value='62']").set(false)

#2


112  

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 checkbox with 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 capybara input manipulations can be found here

更多关于水豚输入操作的信息可以在这里找到

#3


49  

When running capybara test, you got the page object. This you can use to check/uncheck any checkboxes. As @buruzaemon already mentioned:

在运行capybara测试时,您获得了页面对象。这可以用来检查/取消选中任何复选框。@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.uncheck method.

Uncheck和use page是一样的。取消方法。

#4


26  

I think you may have to give unique ids to your form elements, first of all.

首先,我认为您可能需要为表单元素提供唯一的id。

But with regards to Capybara and checkboxes, the Capybara::Node::Actions#check instance method will allow you to find and check a checkbox by name, id, or label text.

但是对于Capybara和复选框,Capybara:::Node::Actions#check instance方法将允许您根据名称、id或标签文本查找和检查复选框。

#5


5  

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.

希望这对某人有帮助。我使用的是Capybara 2.4.4。

#6


1  

you can also use :xpath instead of :css if you have some problems finding it.

如果您在查找时遇到一些问题,也可以使用:xpath而不是:css。

find(:xpath , '//*[@id="example"]').set(true)

找到(:xpath,' / / *[@ id =“例子”])这里(真正的)

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是什么,就会有“copy xpath”,现在你就知道了。

#7


1  

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

(“输入(type =复选框)”)。每个做| |复选框复选框。should_not be_checked结束

#8


1  

.set(true) didn't work for me so I had to call .click:

.set(true)对我不起作用,所以我不得不打电话。

find(...).click

找到(…).click

#9


0  

check find(".whenever input")[:id]

I think this will make capybara wait for any event listener attached to that input, which sometimes is a pain-in-the-ass if it doesn't waits .... If that input doesn't have an ID, choose another property (there must be one)...

我认为这将使水豚等待输入的任何事件监听器相连,有时是一个痛苦源头如果没有等待....如果输入没有ID,请选择另一个属性(必须有一个)……

#1


134  

I found the following worked for me:

我发现以下方法对我有效:

# Check
find(:css, "#cityID[value='62']").set(true)

# Uncheck
find(:css, "#cityID[value='62']").set(false)

#2


112  

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 checkbox with 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 capybara input manipulations can be found here

更多关于水豚输入操作的信息可以在这里找到

#3


49  

When running capybara test, you got the page object. This you can use to check/uncheck any checkboxes. As @buruzaemon already mentioned:

在运行capybara测试时,您获得了页面对象。这可以用来检查/取消选中任何复选框。@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.uncheck method.

Uncheck和use page是一样的。取消方法。

#4


26  

I think you may have to give unique ids to your form elements, first of all.

首先,我认为您可能需要为表单元素提供唯一的id。

But with regards to Capybara and checkboxes, the Capybara::Node::Actions#check instance method will allow you to find and check a checkbox by name, id, or label text.

但是对于Capybara和复选框,Capybara:::Node::Actions#check instance方法将允许您根据名称、id或标签文本查找和检查复选框。

#5


5  

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.

希望这对某人有帮助。我使用的是Capybara 2.4.4。

#6


1  

you can also use :xpath instead of :css if you have some problems finding it.

如果您在查找时遇到一些问题,也可以使用:xpath而不是:css。

find(:xpath , '//*[@id="example"]').set(true)

找到(:xpath,' / / *[@ id =“例子”])这里(真正的)

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是什么,就会有“copy xpath”,现在你就知道了。

#7


1  

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

(“输入(type =复选框)”)。每个做| |复选框复选框。should_not be_checked结束

#8


1  

.set(true) didn't work for me so I had to call .click:

.set(true)对我不起作用,所以我不得不打电话。

find(...).click

找到(…).click

#9


0  

check find(".whenever input")[:id]

I think this will make capybara wait for any event listener attached to that input, which sometimes is a pain-in-the-ass if it doesn't waits .... If that input doesn't have an ID, choose another property (there must be one)...

我认为这将使水豚等待输入的任何事件监听器相连,有时是一个痛苦源头如果没有等待....如果输入没有ID,请选择另一个属性(必须有一个)……