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