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')
|
|
|
|
extract_results(@session)['image'].should == File.basename(__FILE__)
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|
2012-09-17 08:48:13 -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')
|
|
|
|
extract_results(@session)['image'].should == 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')
|
|
|
|
extract_results(@session)['image'].should == File.basename(__FILE__)
|
|
|
|
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')
|
2012-08-30 09:58:28 -04:00
|
|
|
@session.should 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')
|
2012-08-30 09:58:28 -04:00
|
|
|
@session.should 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')
|
2012-08-30 09:58:28 -04:00
|
|
|
@session.should 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'
|
2012-08-30 09:58:28 -04:00
|
|
|
@session.should 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'
|
2012-08-30 09:58:28 -04:00
|
|
|
@session.should have_content('image/jpeg')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
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')
|
2012-09-17 08:48:13 -04:00
|
|
|
@session.body.should include("1 | ")#number of files
|
2012-08-30 09:58:28 -04:00
|
|
|
@session.should have_content(File.read(@test_file_path))
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|
2012-09-17 08:48:13 -04:00
|
|
|
|
|
|
|
it "should not break when using HTML5 multiple file input uploading multiple files" do
|
|
|
|
pending "Selenium is buggy on this, see http://code.google.com/p/selenium/issues/detail?id=2239" if @session.respond_to?(:mode) && @session.mode == :selenium
|
|
|
|
@session.attach_file "Multiple Documents", [@test_file_path, @another_test_file_path]
|
|
|
|
@session.click_button('Upload Multiple')
|
|
|
|
@session.body.should include("2 | ")#number of files
|
|
|
|
@session.body.should include(File.read(@test_file_path))
|
|
|
|
@session.body.should include(File.read(@another_test_file_path))
|
|
|
|
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\""
|
|
|
|
running do
|
|
|
|
@session.attach_file('does not exist', @test_file_path)
|
|
|
|
end.should 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
|
|
|
|
running { @session.attach_file('Image', '/no_such_file.png') }.should raise_error(Capybara::FileNotFound)
|
2009-12-15 14:58:51 -05:00
|
|
|
end
|
|
|
|
end
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|