Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether the check box is checked or not capybara Rspec

Tags:

CHECK

<div class="checkbox">     <input id="yes_1212" class="check_uncheck" type="checkbox" value="true" name="yes" checked="checked">     <label></label>     </div> 

UNCHECK

 <div class="checkbox ">     <input id="allow__100" class="check_uncheck" type="checkbox" value="false" name="Allow">     <label></label>     </div> 

how to check whether the check box is checked or not

like image 621
jerrytom Avatar asked Jan 14 '17 07:01

jerrytom


1 Answers

There are multiple ways depending on exactly what you're trying to do - if you've already found the element and just want to know whether or not it's checked you can do something like

element = find('#yes_1212') ... element.checked? 

If you're trying to assert that the box is on the page and is checked/unchecked you could do

expect(page).to have_field('yes_1212', checked: true) # checked: false or unchecked: true for not checked 

or

expect(page).to have_checked_field('yes_1212')  # or have_unchecked_field 

If you want a boolean response and don't already have a reference to the element

page.has_field?('allow__100', unchecked: true) page.has_unchecked_field?('allow_100') 

In all cases if the input element is actually non-visible for styling reasons you can pass visible: false

like image 85
Thomas Walpole Avatar answered Sep 22 '22 01:09

Thomas Walpole