Reputation: 1383
How can I use the instance method checked?
in Capybara's Class: Capybara::Node::Element
?
I did not get any proper documentation on its usage!
we can use check and uncheck as below:
page.check('some_text')
page.uncheck('some_text')
but page.checked?('some_text')
throws error. I want use checked? method itself to verify if a checkbox is set. How can I achieve this?
Upvotes: 0
Views: 145
Reputation: 46836
The checked?
method is available to the Capybara::Node::Element. page
is a Capybara::Session object, which is why you get an undefined method error.
To use checked?
, you need to get the checkbox element by using find
, find_field
, etc.
I assume the HTML of the page is something like:
<html>
<body>
<input id="box" type="checkbox">
<label for="box">some_text</label>
</body>
</html>
You can get the checkbox, as a Capybara::Node::Element, by using:
page.find_field('some_text')
The checked?
method can be used on the object returned by finder:
page.check('some_text')
p page.find_field('some_text').checked?
#=> true
page.uncheck('some_text')
p page.find_field('some_text').checked?
#=> false
Upvotes: 1