2016-03-07 19:52:19 -05:00
# frozen_string_literal: true
2012-07-21 16:44:10 -04:00
Capybara :: SpecHelper . spec " # choose " do
before do
@session . visit ( '/form' )
end
2009-12-15 14:58:51 -05:00
2012-07-21 16:44:10 -04:00
it " should choose a radio button by id " do
@session . choose ( " gender_male " )
@session . click_button ( 'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'gender' ] ) . to eq ( 'male' )
2012-07-21 16:44:10 -04:00
end
2009-12-15 14:58:51 -05:00
2012-07-21 16:44:10 -04:00
it " should choose a radio button by label " do
@session . choose ( " Both " )
@session . click_button ( 'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'gender' ] ) . to eq ( 'both' )
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
@session . choose ( " Both " )
@session . click_button ( :'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'gender' ] ) . to eq ( 'both' )
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
msg = " Unable to find radio button \" does not exist \" "
2012-10-30 09:26:19 -04:00
expect do
2012-07-21 16:44:10 -04:00
@session . choose ( '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 radio button " 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 . choose ( 'Disabled Radio' )
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:14 -05:00
context " with :exact option " do
it " should accept partial matches when false " do
2016-10-04 14:10:29 -04:00
@session . choose ( " Mal " , exact : false )
2013-02-24 10:48:14 -05:00
@session . click_button ( 'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'gender' ] ) . to eq ( 'male' )
2013-02-24 10:48:14 -05:00
end
it " should not accept partial matches when true " do
expect do
2016-10-04 14:10:29 -04:00
@session . choose ( " Mal " , exact : true )
2013-02-24 10:48:14 -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 radio buttons by their value " do
2016-10-04 14:10:29 -04:00
@session . choose ( 'form[gender]' , option : " male " )
2013-10-20 13:59:07 -04:00
@session . click_button ( 'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'gender' ] ) . to eq ( " male " )
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 . choose ( 'form[gender]' , option : " hermaphrodite " )
2013-10-20 13:59:07 -04:00
end . to raise_error ( Capybara :: ElementNotFound )
end
end
2016-04-21 19:30:44 -04:00
2016-04-22 13:11:19 -04:00
context " with hidden radio buttons " 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 select by clicking the link if available " do
@session . choose ( " party_democrat " )
@session . click_button ( 'awesome' )
expect ( extract_results ( @session ) [ 'party' ] ) . to eq ( 'democrat' )
end
2016-04-22 13:11:19 -04:00
2016-08-01 17:14:10 -04:00
it " should raise error if not allowed to click label " do
expect { @session . choose ( " party_democrat " , allow_label_click : false ) } . to raise_error ( Capybara :: ElementNotFound , 'Unable to find radio button "party_democrat"' )
end
2016-04-22 13:11:19 -04:00
end
2016-04-21 19:30:44 -04:00
end
2016-10-10 17:43:40 -04:00
it " should return the chosen radio button " do
el = @session . find ( :radio_button , 'gender_male' )
expect ( @session . choose ( " gender_male " ) ) . to eq el
end
2010-01-18 14:33:22 -05:00
end