2016-03-07 19:52:19 -05:00
# frozen_string_literal: true
2018-02-28 19:11:41 -05:00
2012-07-21 16:44:10 -04:00
Capybara :: SpecHelper . spec " # check " do
before do
@session . visit ( '/form' )
end
2010-08-27 14:40:25 -04:00
2012-07-21 16:44:10 -04:00
describe " 'checked' attribute " do
it " should be true if checked " do
@session . check ( " Terms of Use " )
2013-11-14 12:43:36 -05:00
expect ( @session . find ( :xpath , " //input[@id='form_terms_of_use'] " ) [ 'checked' ] ) . to be_truthy
2010-08-27 14:40:25 -04:00
end
2012-06-08 09:53:25 -04:00
2012-07-21 16:44:10 -04:00
it " should be false if unchecked " do
2013-11-14 12:43:36 -05:00
expect ( @session . find ( :xpath , " //input[@id='form_terms_of_use'] " ) [ 'checked' ] ) . to be_falsey
2010-08-27 14:40:25 -04:00
end
2012-07-21 16:44:10 -04:00
end
2009-12-15 14:58:51 -05:00
2016-10-04 14:10:29 -04:00
it " should trigger associated events " , requires : [ :js ] do
2012-07-21 16:44:10 -04:00
@session . visit ( '/with_js' )
@session . check ( 'checkbox_with_event' )
2018-02-28 19:11:41 -05:00
expect ( @session ) . to have_css ( '#checkbox_event_triggered' )
2012-07-21 16:44:10 -04:00
end
2010-04-22 10:17:31 -04:00
2012-07-21 16:44:10 -04:00
describe " checking " do
it " should not change an already checked checkbox " do
2016-07-27 13:56:52 -04:00
expect ( @session . find ( :xpath , " //input[@id='form_pets_dog'] " ) ) . to be_checked
2012-07-21 16:44:10 -04:00
@session . check ( 'form_pets_dog' )
2016-07-27 13:56:52 -04:00
expect ( @session . find ( :xpath , " //input[@id='form_pets_dog'] " ) ) . to be_checked
2010-08-27 14:40:25 -04:00
end
2010-04-22 10:17:31 -04:00
2012-07-21 16:44:10 -04:00
it " should check an unchecked checkbox " do
2016-07-27 13:56:52 -04:00
expect ( @session . find ( :xpath , " //input[@id='form_pets_cat'] " ) ) . not_to be_checked
2012-07-21 16:44:10 -04:00
@session . check ( 'form_pets_cat' )
2016-07-27 13:56:52 -04:00
expect ( @session . find ( :xpath , " //input[@id='form_pets_cat'] " ) ) . to be_checked
2010-08-27 14:40:25 -04:00
end
2012-07-21 16:44:10 -04:00
end
2010-08-27 14:40:25 -04:00
2012-07-21 16:44:10 -04:00
describe " unchecking " do
it " should not change an already unchecked checkbox " do
2016-07-27 13:56:52 -04:00
expect ( @session . find ( :xpath , " //input[@id='form_pets_cat'] " ) ) . not_to be_checked
2012-07-21 16:44:10 -04:00
@session . uncheck ( 'form_pets_cat' )
2016-07-27 13:56:52 -04:00
expect ( @session . find ( :xpath , " //input[@id='form_pets_cat'] " ) ) . not_to be_checked
2010-08-27 14:40:25 -04:00
end
2012-07-21 16:44:10 -04:00
it " should uncheck a checked checkbox " do
2016-07-27 13:56:52 -04:00
expect ( @session . find ( :xpath , " //input[@id='form_pets_dog'] " ) ) . to be_checked
2012-07-21 16:44:10 -04:00
@session . uncheck ( 'form_pets_dog' )
2016-07-27 13:56:52 -04:00
expect ( @session . find ( :xpath , " //input[@id='form_pets_dog'] " ) ) . not_to be_checked
2010-08-27 14:40:25 -04:00
end
2012-07-21 16:44:10 -04:00
end
it " should check a checkbox by id " do
@session . check ( " form_pets_cat " )
@session . click_button ( 'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'pets' ] ) . to include ( 'dog' , 'cat' , 'hamster' )
2012-07-21 16:44:10 -04:00
end
it " should check a checkbox by label " do
@session . check ( " Cat " )
@session . click_button ( 'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'pets' ] ) . to include ( 'dog' , 'cat' , 'hamster' )
2012-07-21 16:44:10 -04:00
end
2009-12-15 14:58:51 -05:00
2012-09-06 03:33:43 -04:00
it " casts to string " do
2018-02-28 19:11:41 -05:00
@session . check ( :form_pets_cat )
2012-09-06 03:33:43 -04:00
@session . click_button ( 'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'pets' ] ) . to include ( 'dog' , 'cat' , 'hamster' )
2012-09-06 03:33:43 -04:00
end
2012-07-21 16:44:10 -04:00
context " with a locator that doesn't exist " do
it " should raise an error " do
2017-07-19 18:59:40 -04:00
msg = " Unable to find visible checkbox \" does not exist \" that is not disabled "
2012-10-30 09:26:19 -04:00
expect do
2012-07-21 16:44:10 -04:00
@session . check ( 'does not exist' )
2012-10-30 09:26:19 -04:00
end . to raise_error ( Capybara :: ElementNotFound , msg )
2009-12-15 14:58:51 -05:00
end
end
2012-09-09 11:18:03 -04:00
context " with a disabled checkbox " do
it " should raise an error " do
2012-10-30 09:26:19 -04:00
expect do
2012-09-09 11:18:03 -04:00
@session . check ( 'Disabled Checkbox' )
2012-10-30 09:26:19 -04:00
end . to raise_error ( Capybara :: ElementNotFound )
2012-09-09 11:18:03 -04:00
end
end
2013-02-24 10:48:25 -05:00
context " with :exact option " do
it " should accept partial matches when false " do
2018-02-28 19:11:41 -05:00
@session . check ( 'Ham' , exact : false )
2013-02-24 10:48:25 -05:00
@session . click_button ( 'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'pets' ] ) . to include ( 'hamster' )
2013-02-24 10:48:25 -05:00
end
it " should not accept partial matches when true " do
expect do
2018-02-28 19:11:41 -05:00
@session . check ( 'Ham' , exact : true )
2013-02-24 10:48:25 -05:00
end . to raise_error ( Capybara :: ElementNotFound )
end
end
2013-10-20 13:59:07 -04:00
context " with `option` option " do
it " can check boxes by their value " do
2016-10-04 14:10:29 -04:00
@session . check ( 'form[pets][]' , option : " cat " )
2013-10-20 13:59:07 -04:00
@session . click_button ( 'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'pets' ] ) . to include ( 'cat' )
2013-10-20 13:59:07 -04:00
end
it " should raise an error if option not found " do
expect do
2016-10-04 14:10:29 -04:00
@session . check ( 'form[pets][]' , option : " elephant " )
2013-10-20 13:59:07 -04:00
end . to raise_error ( Capybara :: ElementNotFound )
end
end
2016-04-21 17:25:14 -04:00
2016-04-22 13:11:19 -04:00
context " when checkbox hidden " do
2016-08-01 17:14:10 -04:00
context " with Capybara.automatic_label_click == true " do
around do | spec |
old_click_label , Capybara . automatic_label_click = Capybara . automatic_label_click , true
spec . run
Capybara . automatic_label_click = old_click_label
end
it " should check via clicking the label with :for attribute if possible " do
expect ( @session . find ( :checkbox , 'form_cars_tesla' , unchecked : true , visible : :hidden ) ) . to be
@session . check ( 'form_cars_tesla' )
@session . click_button ( 'awesome' )
expect ( extract_results ( @session ) [ 'cars' ] ) . to include ( 'tesla' )
end
it " should check via clicking the wrapping label if possible " do
expect ( @session . find ( :checkbox , 'form_cars_mclaren' , unchecked : true , visible : :hidden ) ) . to be
@session . check ( 'form_cars_mclaren' )
@session . click_button ( 'awesome' )
expect ( extract_results ( @session ) [ 'cars' ] ) . to include ( 'mclaren' )
end
it " should not click the label if unneeded " do
expect ( @session . find ( :checkbox , 'form_cars_jaguar' , checked : true , visible : :hidden ) ) . to be
@session . check ( 'form_cars_jaguar' )
@session . click_button ( 'awesome' )
expect ( extract_results ( @session ) [ 'cars' ] ) . to include ( 'jaguar' )
end
it " should raise original error when no label available " do
2017-07-19 18:59:40 -04:00
expect { @session . check ( 'form_cars_ariel' ) } . to raise_error ( Capybara :: ElementNotFound , 'Unable to find visible checkbox "form_cars_ariel" that is not disabled' )
2016-08-01 17:14:10 -04:00
end
it " should raise error if not allowed to click label " do
2018-02-28 19:11:41 -05:00
expect { @session . check ( 'form_cars_mclaren' , allow_label_click : false ) } . to raise_error ( Capybara :: ElementNotFound , 'Unable to find visible checkbox "form_cars_mclaren" that is not disabled' )
2016-08-01 17:14:10 -04:00
end
end
context " with Capybara.automatic_label_click == false " do
around do | spec |
old_label_click , Capybara . automatic_label_click = Capybara . automatic_label_click , false
spec . run
Capybara . automatic_label_click = old_label_click
end
it " should raise error if checkbox not visible " do
2018-02-28 19:11:41 -05:00
expect { @session . check ( 'form_cars_mclaren' ) } . to raise_error ( Capybara :: ElementNotFound , 'Unable to find visible checkbox "form_cars_mclaren" that is not disabled' )
2016-08-01 17:14:10 -04:00
end
2016-09-21 15:10:32 -04:00
context " with allow_label_click == true " do
it " should check via the label if input is hidden " do
expect ( @session . find ( :checkbox , 'form_cars_tesla' , unchecked : true , visible : :hidden ) ) . to be
@session . check ( 'form_cars_tesla' , allow_label_click : true )
@session . click_button ( 'awesome' )
expect ( extract_results ( @session ) [ 'cars' ] ) . to include ( 'tesla' )
end
2016-11-09 14:58:29 -05:00
it " should not wait the full time if label can be clicked " do
expect ( @session . find ( :checkbox , 'form_cars_tesla' , unchecked : true , visible : :hidden ) ) . to be
start_time = Time . now
@session . check ( 'form_cars_tesla' , allow_label_click : true , wait : 10 )
end_time = Time . now
expect ( end_time - start_time ) . to be < 10
end
2016-09-21 15:10:32 -04:00
it " should check via the label if input is moved off the left edge of the page " do
expect ( @session . find ( :checkbox , 'form_cars_pagani' , unchecked : true , visible : :all ) ) . to be
@session . check ( 'form_cars_pagani' , allow_label_click : true )
@session . click_button ( 'awesome' )
expect ( extract_results ( @session ) [ 'cars' ] ) . to include ( 'pagani' )
end
2016-08-01 17:14:10 -04:00
end
2016-04-22 13:11:19 -04:00
end
2016-04-21 17:25:14 -04:00
end
2010-08-27 14:40:25 -04:00
end