Rails : Capybara : When Checkboxes and Radio buttons are not found

There may come situation when you try to check(tick) the checkboxes via RSpec. using codes like

find('#agree_legal_guardian').click
find('#agree_terms').click

 

Problem was:

We have hidden the original checkbox and the fancy checkbox was being shown. Since the item was hidden Capybara was not able to find the element.

Solution

Instead of searching for hidden item, I triggered the click event on the Label for that Checkbox

within('.term-condition') do
  find('[for=agree_legal_guardian]').click
  find('[for=agree_terms]').click
end

Useful links

http://www.rubydoc.info/gems/capybara/1.0.0/Capybara/Node/Finders#find-instance_method

Advertisements