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