# frozen_string_literal: trueCapybara::SpecHelper.spec'#attach_file'dobeforedo@test_file_path=File.expand_path('../fixtures/test_file.txt',File.dirname(__FILE__))@another_test_file_path=File.expand_path('../fixtures/another_test_file.txt',File.dirname(__FILE__))@test_jpg_file_path=File.expand_path('../fixtures/capybara.jpg',File.dirname(__FILE__))@no_extension_file_path=File.expand_path('../fixtures/no_extension',File.dirname(__FILE__))@session.visit('/form')endcontext'with normal form'doit'should set a file path by id'do@session.attach_file'form_image',with_os_path_separators(__FILE__)@session.click_button('awesome')expect(extract_results(@session)['image']).toeq(File.basename(__FILE__))endit'should set a file path by label'do@session.attach_file'Image',with_os_path_separators(__FILE__)@session.click_button('awesome')expect(extract_results(@session)['image']).toeq(File.basename(__FILE__))endit'should be able to set on element if no locator passed'doff=@session.find(:file_field,'Image')ff.attach_file(with_os_path_separators(__FILE__))@session.click_button('awesome')expect(extract_results(@session)['image']).toeq(File.basename(__FILE__))endit'casts to string'do@session.attach_file:form_image,with_os_path_separators(__FILE__)@session.click_button('awesome')expect(extract_results(@session)['image']).toeq(File.basename(__FILE__))endendcontext'with multipart form'doit'should set a file path by id'do@session.attach_file'form_document',with_os_path_separators(@test_file_path)@session.click_button('Upload Single')expect(@session).tohave_content(File.read(@test_file_path))endit'should set a file path by label'do@session.attach_file'Single Document',with_os_path_separators(@test_file_path)@session.click_button('Upload Single')expect(@session).tohave_content(File.read(@test_file_path))endit'should not break if no file is submitted'do@session.click_button('Upload Single')expect(@session).tohave_content('No file uploaded')endit'should send content type text/plain when uploading a text file'do@session.attach_file'Single Document',with_os_path_separators(@test_file_path)@session.click_button'Upload Single'expect(@session).tohave_content('text/plain')endit'should send content type image/jpeg when uploading an image'do@session.attach_file'Single Document',with_os_path_separators(@test_jpg_file_path)@session.click_button'Upload Single'expect(@session).tohave_content('image/jpeg')endit'should not break when uploading a file without extension'do@session.attach_file'Single Document',with_os_path_separators(@no_extension_file_path)@session.click_button'Upload Single'expect(@session).tohave_content(File.read(@no_extension_file_path))endit'should not break when using HTML5 multiple file input'do@session.attach_file'Multiple Documents',with_os_path_separators(@test_file_path)@session.click_button('Upload Multiple')expect(@session).tohave_content(File.read(@test_file_path))expect(@session.body).toinclude('1 | ')# number of filesendit'should not break when using HTML5 multiple file input uploading multiple files'do@session.attach_file('Multiple Documents',[@test_file_path,@another_test_file_path].map{|f|with_os_path_separators(f)})@session.click_button('Upload Multiple')expect(@session.body).toinclude('2 | ')# number of filesexpect(@session.body).toinclude(File.read(@test_file_path))expect(@session.body).toinclude(File.read(@another_test_file_path))endit'should not send anything when attaching no files to a multiple upload field'do@session.click_button('Upload Empty Multiple')expect(@session).tohave_content('Successfully ignored empty file field')endit'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)@session.click_button('Upload Multiple')expect(@session.body).toinclude('1 | ')# number of filesexpect(@session.body).toinclude(File.read(@another_test_file_path))expect(@session.body).not_toinclude(File.read(@test_file_path))endit'should fire change once when uploading multiple files from empty',requires: [:js]do@session.visit('with_js')@session.attach_file('multiple-file',[@test_file_path,@another_test_file_path].map{|f|with_os_path_separators(f)})expect(@session).tohave_css('.file_change',count: 1)endit'should fire change once for each set of files uploaded',requires: [:js]do@session.visit('with_js')@session.attach_file('multiple-file',[@test_jpg_file_path].map{|f|with_os_path_separators(f)})@session.attach_file('multiple-file',[@test_file_path,@another_test_file_path].map{|f|with_os_path_separators(f)})expect(@session).tohave_css('.file_change',count: 2)endendcontext"with a locator that doesn't exist"doit'should raise an error'domsg='Unable to find file field "does not exist"'expectdo@session.attach_file('does not exist',with_os_path_separators(@test_file_path))end.toraise_error(Capybara::ElementNotFound,msg)endendcontext"with a path that doesn't exist"doit'should raise an error'doexpect{@session.attach_file('Image','/no_such_file.png')}.toraise_error(Capybara::FileNotFound)endendcontext'with :exact option'doit'should set a file path by partial label when false'do@session.attach_file'Imag',with_os_path_separators(__FILE__),exact: false@session.click_button('awesome')expect(extract_results(@session)['image']).toeq(File.basename(__FILE__))endit'should not allow partial matches when true'doexpectdo@session.attach_file'Imag',with_os_path_separators(__FILE__),exact: trueend.toraise_error(Capybara::ElementNotFound)endendcontext'with :make_visible option',requires: %i[js es_args]doit'applies a default style change when true'do@session.visit('/with_js')expectdo@session.attach_file('hidden_file',with_os_path_separators(__FILE__))end.toraise_errorCapybara::ElementNotFoundexpectdo@session.attach_file('hidden_file',with_os_path_separators(__FILE__),make_visible: true)end.not_toraise_errorendit'accepts a hash of styles to be applied'do@session.visit('/with_js')expectdo@session.attach_file('hidden_file',with_os_path_separators(__FILE__),make_visible: {opacity: 1,display: 'block'})end.not_toraise_errorendit'raises an error when the file input is not made visible'do@session.visit('/with_js')expectdo@session.attach_file('hidden_file',with_os_path_separators(__FILE__),make_visible: {color: 'red'})end.toraise_error(Capybara::ExpectationNotMet)endit'resets the style when done'do@session.visit('/with_js')@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))).toeq'none'endendprivatedefwith_os_path_separators(path)Gem.win_platform??path.to_s.tr('/','\\'):path.to_sendend