Aks..
Aks..

Reputation: 1383

Usage of checked? in Capybara

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

Answers (1)

Justin Ko
Justin Ko

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

Related Questions