diff --git a/.rubocop.yml b/.rubocop.yml index 1e233cef..e180f6d8 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -125,7 +125,7 @@ RSpec/ExampleWording: Enabled: false RSpec/InstanceVariable: - Enabled: false + AssignmentOnly: true RSpec/ExampleLength: Enabled: false diff --git a/lib/capybara/spec/session/attach_file_spec.rb b/lib/capybara/spec/session/attach_file_spec.rb index 7717b997..5a64bf00 100644 --- a/lib/capybara/spec/session/attach_file_spec.rb +++ b/lib/capybara/spec/session/attach_file_spec.rb @@ -1,11 +1,12 @@ # frozen_string_literal: true Capybara::SpecHelper.spec '#attach_file' do + let(:test_file_path) { File.expand_path('../fixtures/test_file.txt', File.dirname(__FILE__)) } + let(:another_test_file_path) { File.expand_path('../fixtures/another_test_file.txt', File.dirname(__FILE__)) } + let(:test_jpg_file_path) { File.expand_path('../fixtures/capybara.jpg', File.dirname(__FILE__)) } + let(:no_extension_file_path) { File.expand_path('../fixtures/no_extension', File.dirname(__FILE__)) } + before do - @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') end @@ -38,15 +39,15 @@ Capybara::SpecHelper.spec '#attach_file' do 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) + @session.attach_file 'form_document', with_os_path_separators(test_file_path) @session.click_button('Upload Single') - expect(@session).to have_content(File.read(@test_file_path)) + expect(@session).to have_content(File.read(test_file_path)) end it 'should set a file path by label' do - @session.attach_file 'Single Document', with_os_path_separators(@test_file_path) + @session.attach_file 'Single Document', with_os_path_separators(test_file_path) @session.click_button('Upload Single') - expect(@session).to have_content(File.read(@test_file_path)) + expect(@session).to have_content(File.read(test_file_path)) end it 'should not break if no file is submitted' do @@ -55,37 +56,37 @@ Capybara::SpecHelper.spec '#attach_file' do end 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) + @session.attach_file 'Single Document', with_os_path_separators(test_file_path) @session.click_button 'Upload Single' expect(@session).to have_content('text/plain') end 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) + @session.attach_file 'Single Document', with_os_path_separators(test_jpg_file_path) @session.click_button 'Upload Single' expect(@session).to have_content('image/jpeg') end it 'should not break when uploading a file without extension' do - @session.attach_file 'Single Document', with_os_path_separators(@no_extension_file_path) + @session.attach_file 'Single Document', with_os_path_separators(no_extension_file_path) @session.click_button 'Upload Single' - expect(@session).to have_content(File.read(@no_extension_file_path)) + expect(@session).to have_content(File.read(no_extension_file_path)) end it 'should not break when using HTML5 multiple file input' do - @session.attach_file 'Multiple Documents', with_os_path_separators(@test_file_path) + @session.attach_file 'Multiple Documents', with_os_path_separators(test_file_path) @session.click_button('Upload Multiple') - expect(@session).to have_content(File.read(@test_file_path)) + expect(@session).to have_content(File.read(test_file_path)) expect(@session.body).to include('1 | ') # number of files end it '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) }) + [test_file_path, another_test_file_path].map { |f| with_os_path_separators(f) }) @session.click_button('Upload Multiple') 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)) + expect(@session.body).to include(File.read(test_file_path)) + expect(@session.body).to include(File.read(another_test_file_path)) end it 'should not send anything when attaching no files to a multiple upload field' do @@ -94,26 +95,26 @@ Capybara::SpecHelper.spec '#attach_file' do end 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) + @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).to include('1 | ') # number of files - expect(@session.body).to include(File.read(@another_test_file_path)) - expect(@session.body).not_to include(File.read(@test_file_path)) + expect(@session.body).to include(File.read(another_test_file_path)) + expect(@session.body).not_to include(File.read(test_file_path)) end it '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) }) + [test_file_path, another_test_file_path].map { |f| with_os_path_separators(f) }) expect(@session).to have_css('.file_change', count: 1) end it '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_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) }) + [test_file_path, another_test_file_path].map { |f| with_os_path_separators(f) }) expect(@session).to have_css('.file_change', count: 2) end end @@ -122,7 +123,7 @@ Capybara::SpecHelper.spec '#attach_file' do it 'should raise an error' do msg = 'Unable to find file field "does not exist"' expect do - @session.attach_file('does not exist', with_os_path_separators(@test_file_path)) + @session.attach_file('does not exist', with_os_path_separators(test_file_path)) end.to raise_error(Capybara::ElementNotFound, msg) end end diff --git a/lib/capybara/spec/session/click_button_spec.rb b/lib/capybara/spec/session/click_button_spec.rb index cd6ab73b..3bc5620a 100644 --- a/lib/capybara/spec/session/click_button_spec.rb +++ b/lib/capybara/spec/session/click_button_spec.rb @@ -45,111 +45,113 @@ Capybara::SpecHelper.spec '#click_button' do context 'with value given on a submit button' do context 'on a form with HTML5 fields' do + let(:results) { extract_results(@session) } + before do @session.click_button('html5_submit') - @results = extract_results(@session) end it 'should serialise and submit search fields' do - expect(@results['html5_search']).to eq('what are you looking for') + expect(results['html5_search']).to eq('what are you looking for') end it 'should serialise and submit email fields' do - expect(@results['html5_email']).to eq('person@email.com') + expect(results['html5_email']).to eq('person@email.com') end it 'should serialise and submit url fields' do - expect(@results['html5_url']).to eq('http://www.example.com') + expect(results['html5_url']).to eq('http://www.example.com') end it 'should serialise and submit tel fields' do - expect(@results['html5_tel']).to eq('911') + expect(results['html5_tel']).to eq('911') end it 'should serialise and submit color fields' do - expect(@results['html5_color'].upcase).to eq('#FFFFFF') + expect(results['html5_color'].upcase).to eq('#FFFFFF') end end context 'on an HTML4 form' do + let(:results) { extract_results(@session) } + before do @session.click_button('awesome') - @results = extract_results(@session) end it 'should serialize and submit text fields' do - expect(@results['first_name']).to eq('John') + expect(results['first_name']).to eq('John') end it 'should escape fields when submitting' do - expect(@results['phone']).to eq('+1 555 7021') + expect(results['phone']).to eq('+1 555 7021') end it 'should serialize and submit password fields' do - expect(@results['password']).to eq('seeekrit') + expect(results['password']).to eq('seeekrit') end it 'should serialize and submit hidden fields' do - expect(@results['token']).to eq('12345') + expect(results['token']).to eq('12345') end it 'should not serialize fields from other forms' do - expect(@results['middle_name']).to be_nil + expect(results['middle_name']).to be_nil end it 'should submit the button that was clicked, but not other buttons' do - expect(@results['awesome']).to eq('awesome') - expect(@results['crappy']).to be_nil + expect(results['awesome']).to eq('awesome') + expect(results['crappy']).to be_nil end it 'should serialize radio buttons' do - expect(@results['gender']).to eq('female') + expect(results['gender']).to eq('female') end it "should default radio value to 'on' if none specified" do - expect(@results['valueless_radio']).to eq('on') + expect(results['valueless_radio']).to eq('on') end it 'should serialize check boxes' do - expect(@results['pets']).to include('dog', 'hamster') - expect(@results['pets']).not_to include('cat') + expect(results['pets']).to include('dog', 'hamster') + expect(results['pets']).not_to include('cat') end it "should default checkbox value to 'on' if none specififed" do - expect(@results['valueless_checkbox']).to eq('on') + expect(results['valueless_checkbox']).to eq('on') end it 'should serialize text areas' do - expect(@results['description']).to eq('Descriptive text goes here') + expect(results['description']).to eq('Descriptive text goes here') end it 'should serialize select tag with values' do - expect(@results['locale']).to eq('en') + expect(results['locale']).to eq('en') end it 'should serialize select tag without values' do - expect(@results['region']).to eq('Norway') + expect(results['region']).to eq('Norway') end it 'should serialize first option for select tag with no selection' do - expect(@results['city']).to eq('London') + expect(results['city']).to eq('London') end it 'should not serialize a select tag without options' do - expect(@results['tendency']).to be_nil + expect(results['tendency']).to be_nil end it 'should convert lf to cr/lf in submitted textareas' do - expect(@results['newline']).to eq("\r\nNew line after and before textarea tag\r\n") + expect(results['newline']).to eq("\r\nNew line after and before textarea tag\r\n") end it 'should not submit disabled fields' do - expect(@results['disabled_text_field']).to be_nil - expect(@results['disabled_textarea']).to be_nil - expect(@results['disabled_checkbox']).to be_nil - expect(@results['disabled_radio']).to be_nil - expect(@results['disabled_select']).to be_nil - expect(@results['disabled_file']).to be_nil + expect(results['disabled_text_field']).to be_nil + expect(results['disabled_textarea']).to be_nil + expect(results['disabled_checkbox']).to be_nil + expect(results['disabled_radio']).to be_nil + expect(results['disabled_select']).to be_nil + expect(results['disabled_file']).to be_nil end end end @@ -172,57 +174,60 @@ Capybara::SpecHelper.spec '#click_button' do end context 'with fields associated with the form using the form attribute', requires: [:form_attribute] do + let(:results) { extract_results(@session) } + before do @session.click_button('submit_form1') - @results = extract_results(@session) end it 'should serialize and submit text fields' do - expect(@results['outside_input']).to eq('outside_input') + expect(results['outside_input']).to eq('outside_input') end it 'should serialize text areas' do - expect(@results['outside_textarea']).to eq('Some text here') + expect(results['outside_textarea']).to eq('Some text here') end it 'should serialize select tags' do - expect(@results['outside_select']).to eq('Ruby') + expect(results['outside_select']).to eq('Ruby') end it 'should not serliaze fields associated with a different form' do - expect(@results['for_form2']).to be_nil + expect(results['for_form2']).to be_nil end end context 'with submit button outside the form defined by