2016-03-07 19:52:19 -05:00
# frozen_string_literal: true
2012-07-21 16:44:10 -04:00
Capybara :: SpecHelper . spec " # attach_file " do
before do
@test_file_path = File . expand_path ( '../fixtures/test_file.txt' , File . dirname ( __FILE__ ) )
2012-09-17 08:48:13 -04:00
@another_test_file_path = File . expand_path ( '../fixtures/another_test_file.txt' , File . dirname ( __FILE__ ) )
2012-07-21 16:44:10 -04:00
@test_jpg_file_path = File . expand_path ( '../fixtures/capybara.jpg' , File . dirname ( __FILE__ ) )
@session . visit ( '/form' )
end
2009-12-15 14:58:51 -05:00
2012-07-21 16:44:10 -04:00
context " with normal form " do
it " should set a file path by id " do
@session . attach_file " form_image " , __FILE__
@session . click_button ( 'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'image' ] ) . to eq ( File . basename ( __FILE__ ) )
2010-01-18 14:33:22 -05:00
end
2012-10-30 09:26:19 -04:00
2012-07-21 16:44:10 -04:00
it " should set a file path by label " do
@session . attach_file " Image " , __FILE__
@session . click_button ( 'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'image' ] ) . to eq ( File . basename ( __FILE__ ) )
2010-01-18 14:33:22 -05:00
end
2012-09-06 03:33:43 -04:00
it " casts to string " do
@session . attach_file :" form_image " , __FILE__
@session . click_button ( 'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'image' ] ) . to eq ( File . basename ( __FILE__ ) )
2012-09-06 03:33:43 -04:00
end
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
context " with multipart form " do
it " should set a file path by id " do
@session . attach_file " form_document " , @test_file_path
@session . click_button ( 'Upload Single' )
2013-11-14 12:43:36 -05:00
expect ( @session ) . to have_content ( File . read ( @test_file_path ) )
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 set a file path by label " do
@session . attach_file " Single Document " , @test_file_path
@session . click_button ( 'Upload Single' )
2013-11-14 12:43:36 -05:00
expect ( @session ) . to have_content ( File . read ( @test_file_path ) )
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 not break if no file is submitted " do
@session . click_button ( 'Upload Single' )
2013-11-14 12:43:36 -05:00
expect ( @session ) . to have_content ( 'No file uploaded' )
2012-07-21 16:44:10 -04:00
end
2010-01-06 11:30:48 -05:00
2012-07-21 16:44:10 -04:00
it " should send content type text/plain when uploading a text file " do
@session . attach_file " Single Document " , @test_file_path
@session . click_button 'Upload Single'
2013-11-14 12:43:36 -05:00
expect ( @session ) . to have_content ( 'text/plain' )
2012-07-21 16:44:10 -04:00
end
2010-01-06 11:30:48 -05:00
2012-07-21 16:44:10 -04:00
it " should send content type image/jpeg when uploading an image " do
@session . attach_file " Single Document " , @test_jpg_file_path
@session . click_button 'Upload Single'
2013-11-14 12:43:36 -05:00
expect ( @session ) . to have_content ( 'image/jpeg' )
2012-07-21 16:44:10 -04:00
end
2012-10-30 09:26:19 -04:00
2012-09-17 08:48:13 -04:00
it " should not break when using HTML5 multiple file input " do
2012-07-21 16:44:10 -04:00
@session . attach_file " Multiple Documents " , @test_file_path
@session . click_button ( 'Upload Multiple' )
2013-11-14 12:43:36 -05:00
expect ( @session ) . to have_content ( File . read ( @test_file_path ) )
2015-07-21 21:26:12 -04:00
expect ( @session . body ) . to include ( " 1 | " ) #number of files
2010-01-18 14:33:22 -05:00
end
2012-10-30 09:26:19 -04:00
2012-09-17 08:48:13 -04:00
it " should not break when using HTML5 multiple file input uploading multiple files " do
2016-10-18 14:09:26 -04:00
pending " Selenium is buggy on this, see http://code.google.com/p/selenium/issues/detail?id=2239 " if @session . respond_to? ( :mode ) && @session . mode . to_s =~ / ^selenium_(firefox|marionette) /
2012-09-17 08:48:13 -04:00
@session . attach_file " Multiple Documents " , [ @test_file_path , @another_test_file_path ]
@session . click_button ( 'Upload Multiple' )
2013-11-14 12:43:36 -05:00
expect ( @session . body ) . to include ( " 2 | " ) #number of files
expect ( @session . body ) . to include ( File . read ( @test_file_path ) )
expect ( @session . body ) . to include ( File . read ( @another_test_file_path ) )
2012-09-17 08:48:13 -04:00
end
2013-07-02 11:18:15 -04:00
it " should not send anything when attaching no files to a multiple upload field " do
@session . click_button ( 'Upload Empty Multiple' )
2015-07-22 02:41:23 -04:00
expect ( @session ) . to have_content ( " Successfully ignored empty file field " )
2013-07-02 11:18:15 -04:00
end
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
context " with a locator that doesn't exist " do
it " should raise an error " do
msg = " Unable to find file field \" does not exist \" "
2012-10-30 09:26:19 -04:00
expect do
2012-07-21 16:44:10 -04:00
@session . attach_file ( 'does not exist' , @test_file_path )
2012-10-30 09:26:19 -04:00
end . to raise_error ( Capybara :: ElementNotFound , msg )
2011-04-11 04:09:54 -04:00
end
2012-07-21 16:44:10 -04:00
end
2011-04-11 04:09:54 -04:00
2012-07-21 16:44:10 -04:00
context " with a path that doesn't exist " do
it " should raise an error " do
2012-10-30 09:26:19 -04:00
expect { @session . attach_file ( 'Image' , '/no_such_file.png' ) } . to raise_error ( Capybara :: FileNotFound )
2009-12-15 14:58:51 -05:00
end
end
2013-02-24 11:06:01 -05:00
context " with :exact option " do
it " should set a file path by partial label when false " do
2016-10-04 14:10:29 -04:00
@session . attach_file " Imag " , __FILE__ , exact : false
2013-02-24 11:06:01 -05:00
@session . click_button ( 'awesome' )
2013-11-14 12:43:36 -05:00
expect ( extract_results ( @session ) [ 'image' ] ) . to eq ( File . basename ( __FILE__ ) )
2013-02-24 11:06:01 -05:00
end
it " not allow partial matches when true " do
expect do
2016-10-04 14:10:29 -04:00
@session . attach_file " Imag " , __FILE__ , exact : true
2013-02-24 11:06:01 -05:00
end . to raise_error ( Capybara :: ElementNotFound )
end
end
2016-12-30 15:33:57 -05:00
2017-01-03 14:20:58 -05:00
context " with :make_visible option " , requires : [ :js , :es_args ] do
it " applies a default style change when true " do
2016-12-30 15:33:57 -05:00
@session . visit ( '/with_js' )
expect { @session . attach_file ( " hidden_file " , __FILE__ ) } . to raise_error Capybara :: ElementNotFound
2017-01-03 14:20:58 -05:00
expect {
@session . attach_file ( " hidden_file " , __FILE__ , make_visible : true )
} . not_to raise_error
end
it " accepts a hash of styles to be applied " do
@session . visit ( '/with_js' )
expect {
@session . attach_file ( " hidden_file " , __FILE__ , make_visible : { opacity : 1 , display : 'block' } )
} . not_to raise_error
end
it " raises an error when the file input is not made visible " do
@session . visit ( '/with_js' )
expect {
@session . attach_file ( " hidden_file " , __FILE__ , make_visible : { color : 'red' } )
} . to raise_error ( Capybara :: ExpectationNotMet )
2016-12-30 15:33:57 -05:00
end
2017-01-09 15:50:56 -05:00
it " resets the style when done " do
@session . visit ( '/with_js' )
@session . attach_file ( " hidden_file " , __FILE__ , make_visible : true )
expect ( @session . evaluate_script ( " arguments[0].style.display " , @session . find ( :css , '#hidden_file' , visible : :all ) ) ) . to eq 'none'
end
2016-12-30 15:33:57 -05:00
end
2010-01-18 14:33:22 -05:00
end