2016-03-07 19:52:19 -05:00
# frozen_string_literal: true
2018-02-28 19:11:41 -05:00
2014-02-16 12:13:58 -05:00
Capybara :: SpecHelper . spec '#assert_text' do
2018-07-10 17:18:39 -04:00
it 'should be true if the given text is on the page' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
expect ( @session . assert_text ( 'est' ) ) . to eq ( true )
expect ( @session . assert_text ( 'Lorem' ) ) . to eq ( true )
expect ( @session . assert_text ( 'Redirect' ) ) . to eq ( true )
2018-02-28 19:11:41 -05:00
expect ( @session . assert_text ( :Redirect ) ) . to eq ( true )
2018-03-05 17:57:33 -05:00
expect ( @session . assert_text ( 'text with whitespace' ) ) . to eq ( true )
2014-02-16 12:13:58 -05:00
end
2018-07-24 19:43:10 -04:00
it 'should support collapsing whitespace' do
@session . visit ( '/with_html' )
2018-07-30 12:23:29 -04:00
expect ( @session . assert_text ( 'text with whitespace' , normalize_ws : true ) ) . to eq ( true )
2018-07-24 19:43:10 -04:00
end
2018-08-24 03:20:28 -04:00
context 'with enabled default collapsing whitespace' do
before { Capybara . default_normalize_ws = true }
it 'should be true if the given unnormalized text is on the page' do
@session . visit ( '/with_html' )
expect ( @session . assert_text ( 'text with whitespace' , normalize_ws : false ) ) . to eq ( true )
end
it 'should support collapsing whitespace' do
@session . visit ( '/with_html' )
expect ( @session . assert_text ( 'text with whitespace' ) ) . to eq ( true )
end
end
2018-07-10 17:18:39 -04:00
it 'should take scopes into account' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
@session . within ( " //a[@title='awesome title'] " ) do
expect ( @session . assert_text ( 'labore' ) ) . to eq ( true )
end
end
2018-07-10 17:18:39 -04:00
it 'should raise if scoped to an element which does not have the text' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
@session . within ( " //a[@title='awesome title'] " ) do
expect do
@session . assert_text ( 'monkey' )
end . to raise_error ( Capybara :: ExpectationNotMet , 'expected to find text "monkey" in "labore"' )
end
end
2018-07-10 17:18:39 -04:00
it 'should be true if :all given and text is invisible.' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
expect ( @session . assert_text ( :all , 'Some of this text is hidden!' ) ) . to eq ( true )
end
2018-07-10 17:18:39 -04:00
it 'should be true if `Capybara.ignore_hidden_elements = true` and text is invisible.' do
2014-02-16 12:13:58 -05:00
Capybara . ignore_hidden_elements = false
@session . visit ( '/with_html' )
expect ( @session . assert_text ( 'Some of this text is hidden!' ) ) . to eq ( true )
end
2018-07-10 17:18:39 -04:00
it 'should raise error with a helpful message if the requested text is present but invisible' do
2015-11-02 11:04:20 -05:00
@session . visit ( '/with_html' )
el = @session . find ( :css , '#hidden-text' )
expect do
el . assert_text ( :visible , 'Some of this text is hidden!' )
2016-07-21 14:31:59 -04:00
end . to raise_error ( Capybara :: ExpectationNotMet , / it was found 1 time including non-visible text / )
end
2018-07-10 17:18:39 -04:00
it 'should raise error with a helpful message if the requested text is present but with incorrect case' do
2016-07-21 14:31:59 -04:00
@session . visit ( '/with_html' )
expect do
2018-03-05 17:57:33 -05:00
@session . assert_text ( 'Text With Whitespace' )
2016-07-21 14:31:59 -04:00
end . to raise_error ( Capybara :: ExpectationNotMet , / it was found 1 time using a case insensitive search / )
2016-08-18 13:05:10 -04:00
end
2018-07-10 17:18:39 -04:00
it 'should raise error with helpful message if requested text is present but invisible and with incorrect case' , requires : [ :js ] do
2017-11-21 21:19:16 -05:00
@session . visit ( '/with_html' )
el = @session . find ( :css , '#uppercase' )
expect do
el . assert_text ( 'text here' )
end . to raise_error ( Capybara :: ExpectationNotMet , / it was found 1 time using a case insensitive search and it was found 1 time including non-visible text / )
end
2018-07-10 17:18:39 -04:00
it 'should raise the correct error if requested text is missing but contains regex special characters' do
2016-08-18 13:05:10 -04:00
@session . visit ( '/with_html' )
2016-08-17 22:50:59 -04:00
expect do
2016-08-18 13:05:10 -04:00
@session . assert_text ( '[]*.' )
end . to raise_error ( Capybara :: ExpectationNotMet , / expected to find text " \ [ \ ] \ * \ ." / )
2015-11-02 11:04:20 -05:00
end
2018-07-10 17:18:39 -04:00
it 'should be true if the text in the page matches given regexp' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
expect ( @session . assert_text ( / Lorem / ) ) . to eq ( true )
end
2018-07-24 19:43:10 -04:00
it " should raise error if the text in the page doesn't match given regexp " do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
expect do
@session . assert_text ( / xxxxyzzz / )
2018-05-14 17:30:34 -04:00
end . to raise_error ( Capybara :: ExpectationNotMet , %r{ \ Aexpected to find text matching /xxxxyzzz/ in "This is a test \\ nHeader Class(.+)" \ Z } )
2014-02-16 12:13:58 -05:00
end
2018-07-10 17:18:39 -04:00
it 'should escape any characters that would have special meaning in a regexp' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
expect do
@session . assert_text ( '.orem' )
end . to raise_error ( Capybara :: ExpectationNotMet )
end
2018-07-10 17:18:39 -04:00
it 'should wait for text to appear' , requires : [ :js ] do
2018-07-05 21:08:59 -04:00
Capybara . default_max_wait_time = 2
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_js' )
@session . click_link ( 'Click me' )
expect ( @session . assert_text ( 'Has been clicked' ) ) . to eq ( true )
end
2018-07-10 17:18:39 -04:00
context 'with between' do
it 'should be true if the text occurs within the range given' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_count' )
expect ( @session . assert_text ( 'count' , between : 1 .. 3 ) ) . to eq ( true )
end
2018-07-10 17:18:39 -04:00
it 'should be false if the text occurs more or fewer times than range' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
expect do
@session . find ( :css , '.number' ) . assert_text ( / \ d / , between : 0 .. 1 )
2018-07-10 17:18:39 -04:00
end . to raise_error ( Capybara :: ExpectationNotMet , 'expected to find text matching /\\d/ between 0 and 1 times but found 2 times in "42"' )
2014-02-16 12:13:58 -05:00
end
end
2018-07-10 17:18:39 -04:00
context 'with wait' , requires : [ :js ] do
it 'should find element if it appears before given wait duration' do
2014-02-16 12:13:58 -05:00
Capybara . using_wait_time ( 0 ) do
@session . visit ( '/with_js' )
@session . find ( :css , '#reload-list' ) . click
2018-03-05 17:57:33 -05:00
@session . find ( :css , '#the-list' ) . assert_text ( " Foo \n Bar " , wait : 0 . 9 )
2014-02-16 12:13:58 -05:00
end
end
2018-07-10 17:18:39 -04:00
it 'should raise error if it appears after given wait duration' do
2014-02-16 12:13:58 -05:00
Capybara . using_wait_time ( 0 ) do
@session . visit ( '/with_js' )
@session . find ( :css , '#reload-list' ) . click
el = @session . find ( :css , '#the-list' , visible : false )
expect do
el . assert_text ( :all , 'Foo Bar' , wait : 0 . 3 )
end . to raise_error ( Capybara :: ExpectationNotMet )
end
end
end
context 'with multiple count filters' do
2018-04-27 14:01:47 -04:00
before do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
end
it 'ignores other filters when :count is specified' do
2018-02-28 19:11:41 -05:00
o = { count : 5 ,
minimum : 6 ,
maximum : 0 ,
between : 0 .. 4 }
2019-12-27 21:28:23 -05:00
expect { @session . assert_text ( 'Header' , ** o ) } . not_to raise_error
2014-02-16 12:13:58 -05:00
end
2019-10-15 20:40:27 -04:00
2014-02-16 12:13:58 -05:00
context 'with no :count expectation' do
it 'fails if :minimum is not met' do
2018-02-28 19:11:41 -05:00
o = { minimum : 6 ,
maximum : 5 ,
between : 2 .. 7 }
2019-12-27 21:28:23 -05:00
expect { @session . assert_text ( 'Header' , ** o ) } . to raise_error ( Capybara :: ExpectationNotMet )
2014-02-16 12:13:58 -05:00
end
2019-10-15 20:40:27 -04:00
2014-02-16 12:13:58 -05:00
it 'fails if :maximum is not met' do
2018-02-28 19:11:41 -05:00
o = { minimum : 0 ,
maximum : 0 ,
between : 2 .. 7 }
2019-12-27 21:28:23 -05:00
expect { @session . assert_text ( 'Header' , ** o ) } . to raise_error ( Capybara :: ExpectationNotMet )
2014-02-16 12:13:58 -05:00
end
2019-10-15 20:40:27 -04:00
2014-02-16 12:13:58 -05:00
it 'fails if :between is not met' do
2018-02-28 19:11:41 -05:00
o = { minimum : 0 ,
maximum : 5 ,
between : 0 .. 4 }
2019-12-27 21:28:23 -05:00
expect { @session . assert_text ( 'Header' , ** o ) } . to raise_error ( Capybara :: ExpectationNotMet )
2014-02-16 12:13:58 -05:00
end
2019-10-15 20:40:27 -04:00
2014-02-16 12:13:58 -05:00
it 'succeeds if all combineable expectations are met' do
2018-02-28 19:11:41 -05:00
o = { minimum : 0 ,
maximum : 5 ,
between : 2 .. 7 }
2019-12-27 21:28:23 -05:00
expect { @session . assert_text ( 'Header' , ** o ) } . not_to raise_error
2014-02-16 12:13:58 -05:00
end
end
end
end
Capybara :: SpecHelper . spec '#assert_no_text' do
2018-07-10 17:18:39 -04:00
it 'should raise error if the given text is on the page at least once' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
expect do
@session . assert_no_text ( 'Lorem' )
2018-03-05 17:57:33 -05:00
end . to raise_error ( Capybara :: ExpectationNotMet , / \ Aexpected not to find text "Lorem" in "This is a test.*" \ z / )
2014-02-16 12:13:58 -05:00
end
2018-07-10 17:18:39 -04:00
it 'should be true if scoped to an element which does not have the text' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
@session . within ( " //a[@title='awesome title'] " ) do
expect ( @session . assert_no_text ( 'monkey' ) ) . to eq ( true )
end
end
2018-07-10 17:18:39 -04:00
it 'should be true if the given text is on the page but not visible' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
expect ( @session . assert_no_text ( 'Inside element with hidden ancestor' ) ) . to eq ( true )
end
2018-07-10 17:18:39 -04:00
it 'should raise error if :all given and text is invisible.' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
el = @session . find ( :css , '#hidden-text' , visible : false )
expect do
el . assert_no_text ( :all , 'Some of this text is hidden!' )
end . to raise_error ( Capybara :: ExpectationNotMet , 'expected not to find text "Some of this text is hidden!" in "Some of this text is hidden!"' )
end
2018-07-10 17:18:39 -04:00
it 'should raise error if :all given and text is invisible.' do
2015-11-02 11:04:20 -05:00
@session . visit ( '/with_html' )
el = @session . find ( :css , '#some-hidden-text' , visible : false )
expect do
el . assert_no_text ( :visible , 'hidden' )
end . to raise_error ( Capybara :: ExpectationNotMet , 'expected not to find text "hidden" in "Some of this text is not hidden"' )
end
2014-02-16 12:13:58 -05:00
it " should be true if the text in the page doesn't match given regexp " do
@session . visit ( '/with_html' )
@session . assert_no_text ( / xxxxyzzz / )
end
2018-07-10 17:18:39 -04:00
context 'with count' do
it 'should be true if the text occurs within the range given' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_count' )
expect ( @session . assert_text ( 'count' , count : 2 ) ) . to eq ( true )
end
2018-07-10 17:18:39 -04:00
it 'should be false if the text occurs more or fewer times than range' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_html' )
expect do
@session . find ( :css , '.number' ) . assert_text ( / \ d / , count : 1 )
2018-07-10 17:18:39 -04:00
end . to raise_error ( Capybara :: ExpectationNotMet , 'expected to find text matching /\\d/ 1 time but found 2 times in "42"' )
2014-02-16 12:13:58 -05:00
end
end
2018-07-10 17:18:39 -04:00
context 'with wait' , requires : [ :js ] do
it 'should not find element if it appears after given wait duration' do
2014-02-16 12:13:58 -05:00
@session . visit ( '/with_js' )
@session . click_link ( 'Click me' )
@session . find ( :css , '#reload-list' ) . click
2016-10-04 14:10:29 -04:00
@session . find ( :css , '#the-list' ) . assert_no_text ( 'Foo Bar' , wait : 0 . 3 )
2014-02-16 12:13:58 -05:00
end
end
end