Remove unnecessary instance variables from specs

This commit is contained in:
Thomas Walpole 2019-02-25 15:50:24 -08:00
parent 6c2d0f3c1f
commit 1b70cec73e
20 changed files with 458 additions and 477 deletions

View File

@ -125,7 +125,7 @@ RSpec/ExampleWording:
Enabled: false
RSpec/InstanceVariable:
Enabled: false
AssignmentOnly: true
RSpec/ExampleLength:
Enabled: false

View File

@ -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

View File

@ -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 <button> tag', requires: [:form_attribute] do
let(:results) { extract_results(@session) }
before do
@session.click_button('outside_button')
@results = extract_results(@session)
end
it 'should submit the associated form' do
expect(@results['which_form']).to eq('form2')
expect(results['which_form']).to eq('form2')
end
it 'should submit the button that was clicked, but not other buttons' do
expect(@results['outside_button']).to eq('outside_button')
expect(@results['unused']).to be_nil
expect(results['outside_button']).to eq('outside_button')
expect(results['unused']).to be_nil
end
end
context "with submit button outside the form defined by <input type='submit'> tag", requires: [:form_attribute] do
let(:results) { extract_results(@session) }
before do
@session.click_button('outside_submit')
@results = extract_results(@session)
end
it 'should submit the associated form' do
expect(@results['which_form']).to eq('form1')
expect(results['which_form']).to eq('form1')
end
it 'should submit the button that was clicked, but not other buttons' do
expect(@results['outside_submit']).to eq('outside_submit')
expect(@results['submit_form1']).to be_nil
expect(results['outside_submit']).to eq('outside_submit')
expect(results['submit_form1']).to be_nil
end
end
@ -303,9 +308,9 @@ Capybara::SpecHelper.spec '#click_button' do
it 'should serialize and send GET forms' do
@session.visit('/form')
@session.click_button('med')
@results = extract_results(@session)
expect(@results['middle_name']).to eq('Darren')
expect(@results['foo']).to be_nil
results = extract_results(@session)
expect(results['middle_name']).to eq('Darren')
expect(results['foo']).to be_nil
end
end
@ -357,45 +362,45 @@ Capybara::SpecHelper.spec '#click_button' do
context 'with formaction attribute on button' do
it 'should submit to the formaction attribute' do
@session.click_button('Formaction button')
@results = extract_results(@session)
results = extract_results(@session)
expect(@session.current_path).to eq '/form'
expect(@results['which_form']).to eq 'formaction form'
expect(results['which_form']).to eq 'formaction form'
end
end
context 'with formmethod attribute on button' do
it 'should submit to the formethod attribute' do
@session.click_button('Formmethod button')
@results = extract_results(@session)
results = extract_results(@session)
expect(@session.current_path).to eq '/form/get'
expect(@results['which_form']).to eq 'formaction form'
expect(results['which_form']).to eq 'formaction form'
end
end
it 'should serialize and send valueless buttons that were clicked' do
@session.click_button('No Value!')
@results = extract_results(@session)
expect(@results['no_value']).not_to be_nil
results = extract_results(@session)
expect(results['no_value']).not_to be_nil
end
it 'should send button in document order' do
@session.click_button('outside_button')
@results = extract_results(@session)
expect(@results.keys).to eq %w[for_form2 outside_button which_form post_count]
results = extract_results(@session)
expect(results.keys).to eq %w[for_form2 outside_button which_form post_count]
end
it 'should not send image buttons that were not clicked' do
@session.click_button('Click me!')
@results = extract_results(@session)
expect(@results['okay']).to be_nil
results = extract_results(@session)
expect(results['okay']).to be_nil
end
it 'should serialize and send GET forms' do
@session.visit('/form')
@session.click_button('med')
@results = extract_results(@session)
expect(@results['middle_name']).to eq('Darren')
expect(@results['foo']).to be_nil
results = extract_results(@session)
expect(results['middle_name']).to eq('Darren')
expect(results['foo']).to be_nil
end
it 'should follow redirects' do

View File

@ -1,31 +1,32 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#match_selector?' do
let(:element) { @session.find(:xpath, '//span', text: '42') }
before do
@session.visit('/with_html')
@element = @session.find('//span', text: '42')
end
it 'should be true if the element matches the given selector' do
expect(@element).to match_selector(:xpath, '//span')
expect(@element).to match_selector(:css, 'span.number')
expect(@element.matches_selector?(:css, 'span.number')).to be true
expect(element).to match_selector(:xpath, '//span')
expect(element).to match_selector(:css, 'span.number')
expect(element.matches_selector?(:css, 'span.number')).to be true
end
it 'should be false if the element does not match the given selector' do
expect(@element).not_to match_selector(:xpath, '//div')
expect(@element).not_to match_selector(:css, 'span.not_a_number')
expect(@element.matches_selector?(:css, 'span.not_a_number')).to be false
expect(element).not_to match_selector(:xpath, '//div')
expect(element).not_to match_selector(:css, 'span.not_a_number')
expect(element.matches_selector?(:css, 'span.not_a_number')).to be false
end
it 'should use default selector' do
Capybara.default_selector = :css
expect(@element).not_to match_selector('span.not_a_number')
expect(@element).to match_selector('span.number')
expect(element).not_to match_selector('span.not_a_number')
expect(element).to match_selector('span.number')
end
it 'should work with elements located via a sibling selector' do
sibling = @element.sibling(:css, 'span', text: 'Other span')
sibling = element.sibling(:css, 'span', text: 'Other span')
expect(sibling).to match_selector(:xpath, '//span')
expect(sibling).to match_selector(:css, 'span')
end
@ -37,23 +38,23 @@ Capybara::SpecHelper.spec '#match_selector?' do
context 'with text' do
it 'should discard all matches where the given string is not contained' do
expect(@element).to match_selector('//span', text: '42')
expect(@element).not_to match_selector('//span', text: 'Doesnotexist')
expect(element).to match_selector('//span', text: '42')
expect(element).not_to match_selector('//span', text: 'Doesnotexist')
end
end
it 'should have css sugar' do
expect(@element.matches_css?('span.number')).to be true
expect(@element.matches_css?('span.not_a_number')).to be false
expect(@element.matches_css?('span.number', text: '42')).to be true
expect(@element.matches_css?('span.number', text: 'Nope')).to be false
expect(element.matches_css?('span.number')).to be true
expect(element.matches_css?('span.not_a_number')).to be false
expect(element.matches_css?('span.number', text: '42')).to be true
expect(element.matches_css?('span.number', text: 'Nope')).to be false
end
it 'should have xpath sugar' do
expect(@element.matches_xpath?('//span')).to be true
expect(@element.matches_xpath?('//div')).to be false
expect(@element.matches_xpath?('//span', text: '42')).to be true
expect(@element.matches_xpath?('//span', text: 'Nope')).to be false
expect(element.matches_xpath?('//span')).to be true
expect(element.matches_xpath?('//div')).to be false
expect(element.matches_xpath?('//span', text: '42')).to be true
expect(element.matches_xpath?('//span', text: 'Nope')).to be false
end
it 'should accept selector filters' do
@ -73,47 +74,47 @@ Capybara::SpecHelper.spec '#match_selector?' do
end
Capybara::SpecHelper.spec '#not_matches_selector?' do
let(:element) { @session.find(:css, 'span', text: 42) }
before do
@session.visit('/with_html')
@element = @session.find(:css, 'span', text: 42)
end
it 'should be false if the given selector matches the element' do
expect(@element).not_to not_match_selector(:xpath, '//span')
expect(@element).not_to not_match_selector(:css, 'span.number')
expect(@element.not_matches_selector?(:css, 'span.number')).to be false
expect(element).not_to not_match_selector(:xpath, '//span')
expect(element).not_to not_match_selector(:css, 'span.number')
expect(element.not_matches_selector?(:css, 'span.number')).to be false
end
it 'should be true if the given selector does not match the element' do
expect(@element).to not_match_selector(:xpath, '//abbr')
expect(@element).to not_match_selector(:css, 'p a#doesnotexist')
expect(@element.not_matches_selector?(:css, 'p a#doesnotexist')).to be true
expect(element).to not_match_selector(:xpath, '//abbr')
expect(element).to not_match_selector(:css, 'p a#doesnotexist')
expect(element.not_matches_selector?(:css, 'p a#doesnotexist')).to be true
end
it 'should use default selector' do
Capybara.default_selector = :css
expect(@element).to not_match_selector('p a#doesnotexist')
expect(@element).not_to not_match_selector('span.number')
expect(element).to not_match_selector('p a#doesnotexist')
expect(element).not_to not_match_selector('span.number')
end
context 'with text' do
it 'should discard all matches where the given string is contained' do
expect(@element).not_to not_match_selector(:css, 'span.number', text: '42')
expect(@element).to not_match_selector(:css, 'span.number', text: 'Doesnotexist')
expect(element).not_to not_match_selector(:css, 'span.number', text: '42')
expect(element).to not_match_selector(:css, 'span.number', text: 'Doesnotexist')
end
end
it 'should have CSS sugar' do
expect(@element.not_matches_css?('span.number')).to be false
expect(@element.not_matches_css?('p a#doesnotexist')).to be true
expect(@element.not_matches_css?('span.number', text: '42')).to be false
expect(@element.not_matches_css?('span.number', text: 'Doesnotexist')).to be true
expect(element.not_matches_css?('span.number')).to be false
expect(element.not_matches_css?('p a#doesnotexist')).to be true
expect(element.not_matches_css?('span.number', text: '42')).to be false
expect(element.not_matches_css?('span.number', text: 'Doesnotexist')).to be true
end
it 'should have xpath sugar' do
expect(@element.not_matches_xpath?('//span')).to be false
expect(@element.not_matches_xpath?('//div')).to be true
expect(@element.not_matches_xpath?('//span', text: '42')).to be false
expect(@element.not_matches_xpath?('//span', text: 'Doesnotexist')).to be true
expect(element.not_matches_xpath?('//span')).to be false
expect(element.not_matches_xpath?('//div')).to be true
expect(element.not_matches_xpath?('//span', text: '42')).to be false
expect(element.not_matches_xpath?('//span', text: 'Doesnotexist')).to be true
end
end

View File

@ -25,9 +25,9 @@ Capybara::SpecHelper.spec 'node' do
describe '#query_scope' do
it 'should have a reference to the element the query was evaluated on if there is one' do
@node = @session.find(:css, '#first')
expect(@node.query_scope).to eq(@node.session.document)
expect(@node.find(:css, '#foo').query_scope).to eq(@node)
node = @session.find(:css, '#first')
expect(node.query_scope).to eq(node.session.document)
expect(node.find(:css, '#foo').query_scope).to eq(node)
end
end

View File

@ -96,25 +96,26 @@ Capybara::SpecHelper.spec '#reset_session!' do
end
context 'When reuse_server == false' do
let!(:orig_reuse_server) { Capybara.reuse_server }
before do
@reuse_server = Capybara.reuse_server
Capybara.reuse_server = false
end
after do
Capybara.reuse_server = @reuse_server
Capybara.reuse_server = orig_reuse_server
end
it 'raises any standard errors caught inside the server during a second session', requires: [:server] do
Capybara.using_driver(@session.mode) do
Capybara.using_session(:another_session) do
@another_session = Capybara.current_session
quietly { @another_session.visit('/error') }
another_session = Capybara.current_session
quietly { another_session.visit('/error') }
expect do
@another_session.reset_session!
another_session.reset_session!
end.to raise_error(TestApp::TestAppError)
@another_session.visit('/')
expect(@another_session.current_path).to eq('/')
another_session.visit('/')
expect(another_session.current_path).to eq('/')
end
end
end

View File

@ -1,38 +1,41 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#become_closed', requires: %i[windows js] do
before do
@window = @session.current_window
@session.visit('/with_windows')
@other_window = @session.window_opened_by do
let!(:window) { @session.current_window }
let(:other_window) do
@session.window_opened_by do
@session.find(:css, '#openWindow').click
end
end
before do
@session.visit('/with_windows')
end
after do
@session.document.synchronize(5, errors: [Capybara::CapybaraError]) do
raise Capybara::CapybaraError if @session.windows.size != 1
end
@session.switch_to_window(@window)
@session.switch_to_window(window)
end
context 'with :wait option' do
it 'should wait if value of :wait is more than timeout' do
@session.within_window @other_window do
@session.within_window other_window do
@session.execute_script('setTimeout(function(){ window.close(); }, 500);')
end
Capybara.using_wait_time 0.1 do
expect(@other_window).to become_closed(wait: 5)
expect(other_window).to become_closed(wait: 5)
end
end
it 'should raise error if value of :wait is less than timeout' do
@session.within_window @other_window do
@session.within_window other_window do
@session.execute_script('setTimeout(function(){ window.close(); }, 1000);')
end
Capybara.using_wait_time 2 do
expect do
expect(@other_window).to become_closed(wait: 0.2)
expect(other_window).to become_closed(wait: 0.2)
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /\Aexpected #<Window @handle=".+"> to become closed after 0.2 seconds\Z/)
end
end
@ -40,21 +43,21 @@ Capybara::SpecHelper.spec '#become_closed', requires: %i[windows js] do
context 'without :wait option' do
it 'should wait if value of default_max_wait_time is more than timeout' do
@session.within_window @other_window do
@session.within_window other_window do
@session.execute_script('setTimeout(function(){ window.close(); }, 500);')
end
Capybara.using_wait_time 5 do
expect(@other_window).to become_closed
expect(other_window).to become_closed
end
end
it 'should raise error if value of default_max_wait_time is less than timeout' do
@session.within_window @other_window do
@session.within_window other_window do
@session.execute_script('setTimeout(function(){ window.close(); }, 900);')
end
Capybara.using_wait_time 0.4 do
expect do
expect(@other_window).to become_closed
expect(other_window).to become_closed
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /\Aexpected #<Window @handle=".+"> to become closed after 0.4 seconds\Z/)
end
end
@ -62,23 +65,23 @@ Capybara::SpecHelper.spec '#become_closed', requires: %i[windows js] do
context 'with not_to' do
it "should not raise error if window doesn't close before default_max_wait_time" do
@session.within_window @other_window do
@session.within_window other_window do
@session.execute_script('setTimeout(function(){ window.close(); }, 1000);')
end
Capybara.using_wait_time 0.3 do
expect do
expect(@other_window).not_to become_closed
expect(other_window).not_to become_closed
end.not_to raise_error
end
end
it 'should raise error if window closes before default_max_wait_time' do
@session.within_window @other_window do
@session.within_window other_window do
@session.execute_script('setTimeout(function(){ window.close(); }, 700);')
end
Capybara.using_wait_time 3.1 do
expect do
expect(@other_window).not_to become_closed
expect(other_window).not_to become_closed
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /\Aexpected #<Window @handle=".+"> not to become closed after 3.1 seconds\Z/)
end
end

View File

@ -5,89 +5,87 @@
# using Capybara provided assertions with builtin waiting behavior.
Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
let!(:orig_window) { @session.current_window }
before do
@window = @session.current_window
@session.visit('/with_windows')
end
after do
(@session.windows - [@window]).each do |w|
(@session.windows - [orig_window]).each do |w|
@session.switch_to_window w
w.close
end
@session.switch_to_window(@window)
@session.switch_to_window(orig_window)
end
describe '#exists?' do
before do
@other_window = @session.window_opened_by do
it 'should become false after window was closed' do
other_window = @session.window_opened_by do
@session.find(:css, '#openWindow').click
end
end
it 'should become false after window was closed' do
expect do
@session.switch_to_window @other_window
@other_window.close
end.to change { @other_window.exists? }.from(true).to(false)
@session.switch_to_window other_window
other_window.close
end.to change(other_window, :exists?).from(true).to(false)
end
end
describe '#closed?' do
it 'should become true after window was closed' do
@other_window = @session.window_opened_by do
other_window = @session.window_opened_by do
@session.find(:css, '#openWindow').click
end
expect do
@session.switch_to_window @other_window
@other_window.close
end.to change { @other_window.closed? }.from(false).to(true)
@session.switch_to_window other_window
other_window.close
end.to change { other_window.closed? }.from(false).to(true)
end
end
describe '#current?' do
before do
@other_window = @session.window_opened_by do
let(:other_window) do
@session.window_opened_by do
@session.find(:css, '#openWindow').click
end
end
it 'should become true after switching to window' do
expect do
@session.switch_to_window(@other_window)
end.to change { @other_window.current? }.from(false).to(true)
@session.switch_to_window(other_window)
end.to change(other_window, :current?).from(false).to(true)
end
it 'should return false if window is closed' do
@session.switch_to_window(@other_window)
@other_window.close
expect(@other_window.current?).to eq(false)
@session.switch_to_window(other_window)
other_window.close
expect(other_window.current?).to eq(false)
end
end
describe '#close' do
before do
@other_window = @session.window_opened_by do
let!(:other_window) do
@session.window_opened_by do
@session.find(:css, '#openWindow').click
end
end
it 'should switch to original window if invoked not for current window' do
expect(@session.windows.size).to eq(2)
expect(@session.current_window).to eq(@window)
@other_window.close
expect(@session.current_window).to eq(orig_window)
other_window.close
expect(@session.windows.size).to eq(1)
expect(@session.current_window).to eq(@window)
expect(@session.current_window).to eq(orig_window)
end
it 'should make subsequent invocations of other methods raise no_such_window_error if invoked for current window' do
@session.switch_to_window(@other_window)
expect(@session.current_window).to eq(@other_window)
@other_window.close
@session.switch_to_window(other_window)
expect(@session.current_window).to eq(other_window)
other_window.close
expect do
@session.find(:css, '#some_id')
end.to raise_error(@session.driver.no_such_window_error)
@session.switch_to_window(@window)
@session.switch_to_window(orig_window)
end
end
@ -101,43 +99,41 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
end
it 'should switch to original window if invoked not for current window' do
@other_window = @session.window_opened_by do
other_window = @session.window_opened_by do
@session.find(:css, '#openWindow').click
end
sleep 1
size = @session.within_window(@other_window) do
size = @session.within_window(other_window) do
win_size
end
expect(@other_window.size).to eq(size)
expect(@session.current_window).to eq(@window)
expect(other_window.size).to eq(size)
expect(@session.current_window).to eq(orig_window)
end
end
describe '#resize_to' do
before do
@initial_size = @session.current_window.size
end
let!(:initial_size) { @session.current_window.size }
after do
@session.current_window.resize_to(*@initial_size)
@session.current_window.resize_to(*initial_size)
sleep 1
end
it 'should be able to resize window', requires: %i[windows js] do
width, height = @initial_size
width, height = initial_size
@session.current_window.resize_to(width - 100, height - 100)
sleep 1
expect(@session.current_window.size).to eq([width - 100, height - 100])
end
it 'should stay on current window if invoked not for current window', requires: %i[windows js] do
@other_window = @session.window_opened_by do
other_window = @session.window_opened_by do
@session.find(:css, '#openWindow').click
end
@other_window.resize_to(600, 300)
expect(@session.current_window).to eq(@window)
other_window.resize_to(600, 300)
expect(@session.current_window).to eq(orig_window)
@session.within_window(@other_window) do
@session.within_window(other_window) do
expect(@session.current_window.size).to eq([600, 300])
end
end
@ -169,19 +165,19 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
end
it 'should stay on current window if invoked not for current window', requires: %i[windows js] do
@other_window = @session.window_opened_by do
other_window = @session.window_opened_by do
@session.find(:css, '#openWindow').click
end
@other_window.resize_to(400, 300)
other_window.resize_to(400, 300)
sleep 0.5
@other_window.maximize
other_window.maximize
sleep 0.5 # The timing on maximize is finicky on Travis -- wait a bit for maximize to occur
expect(@session.current_window).to eq(@window)
expect(@session.current_window).to eq(orig_window)
# Maximizing the browser affects all tabs so this may not be valid in real browsers
# expect(@session.current_window.size).to eq(@initial_size)
ow_width, ow_height = @other_window.size
ow_width, ow_height = other_window.size
expect(ow_width).to be > 400
expect(ow_height).to be > 300
end

View File

@ -4,12 +4,12 @@ require 'spec_helper'
RSpec.describe Capybara do
describe 'default_max_wait_time' do
after do
Capybara.default_max_wait_time = @previous_default_time
end
before { @previous_default_time = Capybara.default_max_wait_time }
after { Capybara.default_max_wait_time = @previous_default_time } # rubocop:disable RSpec/InstanceVariable
it 'should be changeable' do
@previous_default_time = Capybara.default_max_wait_time
expect(Capybara.default_max_wait_time).not_to eq(5)
Capybara.default_max_wait_time = 5
expect(Capybara.default_max_wait_time).to eq(5)
end

View File

@ -2,6 +2,7 @@
require 'spec_helper'
# rubocop:disable RSpec/InstanceVariable
RSpec.describe Capybara::Selector::CSSBuilder do
let :builder do
::Capybara::Selector::CSSBuilder.new(@css)
@ -97,3 +98,4 @@ RSpec.describe Capybara::Selector::CSSBuilder do
end
end
end
# rubocop:enable RSpec/InstanceVariable

View File

@ -115,14 +115,11 @@ RSpec.describe Capybara::DSL do
end
end
# rubocop:disable RSpec/InstanceVariable
describe '#using_wait_time' do
before do
@previous_wait_time = Capybara.default_max_wait_time
end
before { @previous_wait_time = Capybara.default_max_wait_time }
after do
Capybara.default_max_wait_time = @previous_wait_time
end
after { Capybara.default_max_wait_time = @previous_wait_time }
it 'should switch the wait time and switch it back' do
in_block = nil
@ -142,6 +139,7 @@ RSpec.describe Capybara::DSL do
expect(Capybara.default_max_wait_time).to eq(@previous_wait_time)
end
end
# rubocop:enable RSpec/InstanceVariable
describe '#app' do
it 'should be changeable' do
@ -249,31 +247,29 @@ RSpec.describe Capybara::DSL do
end
describe 'the DSL' do
before do
@session = Class.new { include Capybara::DSL }.new
end
let(:session) { Class.new { include Capybara::DSL }.new }
it 'should be possible to include it in another class' do
@session.visit('/with_html')
@session.click_link('ullamco')
expect(@session.body).to include('Another World')
session.visit('/with_html')
session.click_link('ullamco')
expect(session.body).to include('Another World')
end
it "should provide a 'page' shortcut for more expressive tests" do
@session.page.visit('/with_html')
@session.page.click_link('ullamco')
expect(@session.page.body).to include('Another World')
session.page.visit('/with_html')
session.page.click_link('ullamco')
expect(session.page.body).to include('Another World')
end
it "should provide an 'using_session' shortcut" do
allow(Capybara).to receive(:using_session)
@session.using_session(:name)
session.using_session(:name)
expect(Capybara).to have_received(:using_session).with(:name)
end
it "should provide a 'using_wait_time' shortcut" do
allow(Capybara).to receive(:using_wait_time)
@session.using_wait_time(6)
session.using_wait_time(6)
expect(Capybara).to have_received(:using_wait_time).with(6)
end
end

View File

@ -37,55 +37,53 @@ end
RSpec.describe Capybara::Session do # rubocop:disable RSpec/MultipleDescribes
context 'with rack test driver' do
before do
@session = TestSessions::RackTest
end
let(:session) { TestSessions::RackTest }
describe '#driver' do
it 'should be a rack test driver' do
expect(@session.driver).to be_an_instance_of(Capybara::RackTest::Driver)
expect(session.driver).to be_an_instance_of(Capybara::RackTest::Driver)
end
end
describe '#mode' do
it 'should remember the mode' do
expect(@session.mode).to eq(:rack_test)
expect(session.mode).to eq(:rack_test)
end
end
describe '#click_link' do
after do
@session.driver.options[:respect_data_method] = false
session.driver.options[:respect_data_method] = false
end
it 'should use data-method if option is true' do
@session.driver.options[:respect_data_method] = true
@session.visit '/with_html'
@session.click_link 'A link with data-method'
expect(@session.html).to include('The requested object was deleted')
session.driver.options[:respect_data_method] = true
session.visit '/with_html'
session.click_link 'A link with data-method'
expect(session.html).to include('The requested object was deleted')
end
it 'should not use data-method if option is false' do
@session.driver.options[:respect_data_method] = false
@session.visit '/with_html'
@session.click_link 'A link with data-method'
expect(@session.html).to include('Not deleted')
session.driver.options[:respect_data_method] = false
session.visit '/with_html'
session.click_link 'A link with data-method'
expect(session.html).to include('Not deleted')
end
it "should use data-method if available even if it's capitalized" do
@session.driver.options[:respect_data_method] = true
@session.visit '/with_html'
@session.click_link 'A link with capitalized data-method'
expect(@session.html).to include('The requested object was deleted')
session.driver.options[:respect_data_method] = true
session.visit '/with_html'
session.click_link 'A link with capitalized data-method'
expect(session.html).to include('The requested object was deleted')
end
end
describe '#fill_in' do
it 'should warn that :fill_options are not supported' do
allow_any_instance_of(Capybara::RackTest::Node).to receive(:warn)
@session.visit '/with_html'
field = @session.fill_in 'test_field', with: 'not_monkey', fill_options: { random: true }
expect(@session).to have_field('test_field', with: 'not_monkey')
session.visit '/with_html'
field = session.fill_in 'test_field', with: 'not_monkey', fill_options: { random: true }
expect(session).to have_field('test_field', with: 'not_monkey')
expect(field.base).to have_received(:warn).with("Options passed to Node#set but the RackTest driver doesn't support any - ignoring")
end
end
@ -93,50 +91,50 @@ RSpec.describe Capybara::Session do # rubocop:disable RSpec/MultipleDescribes
describe '#attach_file' do
context 'with multipart form' do
it 'should submit an empty form-data section if no file is submitted' do
@session.visit('/form')
@session.click_button('Upload Empty')
expect(@session.html).to include('Successfully ignored empty file field.')
session.visit('/form')
session.click_button('Upload Empty')
expect(session.html).to include('Successfully ignored empty file field.')
end
end
it 'should not submit an obsolete mime type' do
@test_jpg_file_path = File.expand_path('fixtures/capybara.csv', File.dirname(__FILE__))
@session.visit('/form')
@session.attach_file 'form_document', @test_jpg_file_path
@session.click_button('Upload Single')
expect(@session).to have_content('Content-type: text/csv')
test_jpg_file_path = File.expand_path('fixtures/capybara.csv', File.dirname(__FILE__))
session.visit('/form')
session.attach_file 'form_document', test_jpg_file_path
session.click_button('Upload Single')
expect(session).to have_content('Content-type: text/csv')
end
end
describe '#click' do
context 'on a label' do
it 'should toggle the associated checkbox' do
@session.visit('/form')
expect(@session).to have_unchecked_field('form_pets_cat')
@session.find(:label, 'Cat').click
expect(@session).to have_checked_field('form_pets_cat')
@session.find(:label, 'Cat').click
expect(@session).to have_unchecked_field('form_pets_cat')
@session.find(:label, 'McLaren').click
expect(@session).to have_checked_field('form_cars_mclaren', visible: :hidden)
session.visit('/form')
expect(session).to have_unchecked_field('form_pets_cat')
session.find(:label, 'Cat').click
expect(session).to have_checked_field('form_pets_cat')
session.find(:label, 'Cat').click
expect(session).to have_unchecked_field('form_pets_cat')
session.find(:label, 'McLaren').click
expect(session).to have_checked_field('form_cars_mclaren', visible: :hidden)
end
it 'should toggle the associated radio' do
@session.visit('/form')
expect(@session).to have_unchecked_field('gender_male')
@session.find(:label, 'Male').click
expect(@session).to have_checked_field('gender_male')
@session.find(:label, 'Female').click
expect(@session).to have_unchecked_field('gender_male')
session.visit('/form')
expect(session).to have_unchecked_field('gender_male')
session.find(:label, 'Male').click
expect(session).to have_checked_field('gender_male')
session.find(:label, 'Female').click
expect(session).to have_unchecked_field('gender_male')
end
end
end
describe '#text' do
it 'should return original text content for textareas' do
@session.visit('/with_html')
@session.find_field('normal', type: 'textarea', with: 'banana').set('hello')
normal = @session.find(:css, '#normal')
session.visit('/with_html')
session.find_field('normal', type: 'textarea', with: 'banana').set('hello')
normal = session.find(:css, '#normal')
expect(normal.value).to eq 'hello'
expect(normal.text).to eq 'banana'
end
@ -144,8 +142,8 @@ RSpec.describe Capybara::Session do # rubocop:disable RSpec/MultipleDescribes
describe '#style' do
it 'should raise an error' do
@session.visit('/with_html')
el = @session.find(:css, '#first')
session.visit('/with_html')
el = session.find(:css, '#first')
expect { el.style('display') }.to raise_error NotImplementedError, /not process CSS/
end
end
@ -153,87 +151,81 @@ RSpec.describe Capybara::Session do # rubocop:disable RSpec/MultipleDescribes
end
RSpec.describe Capybara::RackTest::Driver do
before do
@driver = TestSessions::RackTest.driver
end
let(:driver) { TestSessions::RackTest.driver }
describe ':headers option' do
it 'should always set headers' do
@driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
@driver.visit('/get_header')
expect(@driver.html).to include('foobar')
driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
driver.visit('/get_header')
expect(driver.html).to include('foobar')
end
it 'should keep headers on link clicks' do
@driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
@driver.visit('/header_links')
@driver.find_xpath('.//a').first.click
expect(@driver.html).to include('foobar')
driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
driver.visit('/header_links')
driver.find_xpath('.//a').first.click
expect(driver.html).to include('foobar')
end
it 'should keep headers on form submit' do
@driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
@driver.visit('/header_links')
@driver.find_xpath('.//input').first.click
expect(@driver.html).to include('foobar')
driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
driver.visit('/header_links')
driver.find_xpath('.//input').first.click
expect(driver.html).to include('foobar')
end
it 'should keep headers on redirects' do
@driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
@driver.visit('/get_header_via_redirect')
expect(@driver.html).to include('foobar')
driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
driver.visit('/get_header_via_redirect')
expect(driver.html).to include('foobar')
end
end
describe ':follow_redirects option' do
it 'defaults to following redirects' do
@driver = Capybara::RackTest::Driver.new(TestApp)
driver = Capybara::RackTest::Driver.new(TestApp)
@driver.visit('/redirect')
expect(@driver.response.header['Location']).to be_nil
expect(@driver.current_url).to match %r{/landed$}
driver.visit('/redirect')
expect(driver.response.header['Location']).to be_nil
expect(driver.current_url).to match %r{/landed$}
end
it 'is possible to not follow redirects' do
@driver = Capybara::RackTest::Driver.new(TestApp, follow_redirects: false)
driver = Capybara::RackTest::Driver.new(TestApp, follow_redirects: false)
@driver.visit('/redirect')
expect(@driver.response.header['Location']).to match %r{/redirect_again$}
expect(@driver.current_url).to match %r{/redirect$}
driver.visit('/redirect')
expect(driver.response.header['Location']).to match %r{/redirect_again$}
expect(driver.current_url).to match %r{/redirect$}
end
end
describe ':redirect_limit option' do
context 'with default redirect limit' do
before do
@driver = Capybara::RackTest::Driver.new(TestApp)
end
let(:driver) { Capybara::RackTest::Driver.new(TestApp) }
it 'should follow 5 redirects' do
@driver.visit('/redirect/5/times')
expect(@driver.html).to include('redirection complete')
driver.visit('/redirect/5/times')
expect(driver.html).to include('redirection complete')
end
it 'should not follow more than 6 redirects' do
expect do
@driver.visit('/redirect/6/times')
driver.visit('/redirect/6/times')
end.to raise_error(Capybara::InfiniteRedirectError)
end
end
context 'with 21 redirect limit' do
before do
@driver = Capybara::RackTest::Driver.new(TestApp, redirect_limit: 21)
end
let(:driver) { Capybara::RackTest::Driver.new(TestApp, redirect_limit: 21) }
it 'should follow 21 redirects' do
@driver.visit('/redirect/21/times')
expect(@driver.html).to include('redirection complete')
driver.visit('/redirect/21/times')
expect(driver.html).to include('redirection complete')
end
it 'should not follow more than 21 redirects' do
expect do
@driver.visit('/redirect/22/times')
driver.visit('/redirect/22/times')
end.to raise_error(Capybara::InfiniteRedirectError)
end
end

View File

@ -3,6 +3,7 @@
require 'spec_helper'
require 'capybara/rspec'
# rubocop:disable RSpec/InstanceVariable
RSpec.configuration.before(:each, file_path: './spec/rspec/features_spec.rb') do
@in_filtered_hook = true
end
@ -53,6 +54,7 @@ feature "Capybara's feature DSL" do
end
end
end
# rubocop:enable RSpec/InstanceVariable
feature 'given and given! aliases to let and let!' do
given(:value) { :available }

View File

@ -555,21 +555,20 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
context 'with wait' do
before do
@session = session
@session.visit('/with_js')
session.visit('/with_js')
end
it 'waits if wait time is more than timeout' do
@session.click_link('Change title')
session.click_link('Change title')
using_wait_time 0 do
expect(@session).to have_title('changed title', wait: 2)
expect(session).to have_title('changed title', wait: 2)
end
end
it "doesn't wait if wait time is less than timeout" do
@session.click_link('Change title')
session.click_link('Change title')
using_wait_time 3 do
expect(@session).not_to have_title('changed title', wait: 0)
expect(session).not_to have_title('changed title', wait: 0)
end
end
end
@ -600,21 +599,20 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
context 'with wait' do
before do
@session = session
@session.visit('/with_js')
session.visit('/with_js')
end
it 'waits if wait time is more than timeout' do
@session.click_link('Change page')
session.click_link('Change page')
using_wait_time 0 do
expect(@session).to have_current_path('/with_html', wait: 2)
expect(session).to have_current_path('/with_html', wait: 2)
end
end
it "doesn't wait if wait time is less than timeout" do
@session.click_link('Change page')
session.click_link('Change page')
using_wait_time 0 do
expect(@session).not_to have_current_path('/with_html')
expect(session).not_to have_current_path('/with_html')
end
end
end
@ -848,10 +846,11 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
end
context 'compounding timing' do
let(:session) { session }
let(:el) { session.find(:css, '#reload-me') }
before do
@session = session
@session.visit('/with_js')
@el = @session.find(:css, '#reload-me')
session.visit('/with_js')
end
context '#and' do
@ -860,82 +859,82 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
matcher = have_text('this is not there').and have_text('neither is this')
expect(Benchmark.realtime do
expect do
expect(@el).to matcher
expect(el).to matcher
end.to raise_error RSpec::Expectations::ExpectationNotMetError
end).to be_between(2, 3)
end
end
it "should run 'concurrently' and retry" do
@session.click_link('reload-link')
@session.using_wait_time(2) do
session.click_link('reload-link')
session.using_wait_time(2) do
expect(Benchmark.realtime do
expect do
expect(@el).to have_text('waiting to be reloaded').and(have_text('has been reloaded'))
expect(el).to have_text('waiting to be reloaded').and(have_text('has been reloaded'))
end.to raise_error RSpec::Expectations::ExpectationNotMetError, /expected to find text "waiting to be reloaded" in "has been reloaded"/
end).to be_between(2, 3)
end
end
it 'should ignore :wait options' do
@session.using_wait_time(2) do
session.using_wait_time(2) do
matcher = have_text('this is not there', wait: 5).and have_text('neither is this', wait: 6)
expect(Benchmark.realtime do
expect do
expect(@el).to matcher
expect(el).to matcher
end.to raise_error RSpec::Expectations::ExpectationNotMetError
end).to be_between(2, 3)
end
end
it 'should work on the session' do
@session.using_wait_time(2) do
@session.click_link('reload-link')
expect(@session).to have_selector(:css, 'h1', text: 'FooBar').and have_text('has been reloaded')
session.using_wait_time(2) do
session.click_link('reload-link')
expect(session).to have_selector(:css, 'h1', text: 'FooBar').and have_text('has been reloaded')
end
end
end
context '#and_then' do
it 'should run sequentially' do
@session.click_link('reload-link')
expect(@el).to have_text('waiting to be reloaded').and_then have_text('has been reloaded')
session.click_link('reload-link')
expect(el).to have_text('waiting to be reloaded').and_then have_text('has been reloaded')
end
end
context '#or' do
it "should run 'concurrently'" do
@session.using_wait_time(3) do
session.using_wait_time(3) do
expect(Benchmark.realtime do
expect(@el).to have_text('has been reloaded').or have_text('waiting to be reloaded')
expect(el).to have_text('has been reloaded').or have_text('waiting to be reloaded')
end).to be < 1
end
end
it 'should retry' do
@session.using_wait_time(3) do
session.using_wait_time(3) do
expect(Benchmark.realtime do
expect do
expect(@el).to have_text('has been reloaded').or have_text('random stuff')
expect(el).to have_text('has been reloaded').or have_text('random stuff')
end.to raise_error RSpec::Expectations::ExpectationNotMetError
end).to be > 3
end
end
it 'should ignore :wait options' do
@session.using_wait_time(2) do
session.using_wait_time(2) do
expect(Benchmark.realtime do
expect do
expect(@el).to have_text('this is not there', wait: 10).or have_text('neither is this', wait: 15)
expect(el).to have_text('this is not there', wait: 10).or have_text('neither is this', wait: 15)
end.to raise_error RSpec::Expectations::ExpectationNotMetError
end).to be_between(2, 3)
end
end
it 'should work on the session' do
@session.using_wait_time(2) do
@session.click_link('reload-link')
expect(@session).to have_selector(:css, 'h1', text: 'Not on the page').or have_text('has been reloaded')
session.using_wait_time(2) do
session.click_link('reload-link')
expect(session).to have_selector(:css, 'h1', text: 'Not on the page').or have_text('has been reloaded')
end
end
end

View File

@ -75,40 +75,38 @@ RSpec.describe 'capybara/rspec' do
context 'Type: Other', type: :other do
context 'when RSpec::Matchers is included after Capybara::DSL' do
before do
let(:test_class_instance) do
class DSLMatchersTest
include Capybara::DSL
include RSpec::Matchers
end
@test_class_instance = DSLMatchersTest.new
end.new
end
context '#all' do
it 'allows access to the Capybara finder' do
@test_class_instance.visit('/with_html')
expect(@test_class_instance.all(:css, 'h2.head').size).to eq(5)
test_class_instance.visit('/with_html')
expect(test_class_instance.all(:css, 'h2.head').size).to eq(5)
end
it 'allows access to the RSpec matcher' do
@test_class_instance.visit('/with_html')
test_class_instance.visit('/with_html')
strings = %w[test1 test2]
expect(strings).to @test_class_instance.all(be_a(String))
expect(strings).to test_class_instance.all(be_a(String))
end
end
context '#within' do
it 'allows access to the Capybara scoper' do
@test_class_instance.visit('/with_html')
test_class_instance.visit('/with_html')
expect do
@test_class_instance.within(:css, '#does_not_exist') { @test_class_instance.click_link 'Go to simple' }
test_class_instance.within(:css, '#does_not_exist') { test_class_instance.click_link 'Go to simple' }
end.to raise_error(Capybara::ElementNotFound)
end
it 'allows access to the RSpec matcher' do
@test_class_instance.visit('/with_html')
test_class_instance.visit('/with_html')
# This reads terribly, but must call #within
expect(@test_class_instance.find(:css, 'span.number').text.to_i).to @test_class_instance.within(1).of(41)
expect(test_class_instance.find(:css, 'span.number').text.to_i).to test_class_instance.within(1).of(41)
end
end
@ -124,7 +122,7 @@ RSpec.describe 'capybara/rspec' do
it 'can be called with `not_to`' do
# This test is for a bug in jruby where `super` isn't defined correctly - https://github.com/jruby/jruby/issues/4678
# Reported in https://github.com/teamcapybara/capybara/issues/2115
@test_class_instance.instance_eval do
test_class_instance.instance_eval do
expect do
expect(true).not_to only_match_matcher(false) # rubocop:disable RSpec/ExpectActual
end.not_to raise_error

View File

@ -50,23 +50,23 @@ RSpec.describe 'Capybara::Session with chrome' do
context 'storage' do
describe '#reset!' do
it 'clears storage by default' do
@session = TestSessions::Chrome
@session.visit('/with_js')
@session.find(:css, '#set-storage').click
@session.reset!
@session.visit('/with_js')
expect(@session.evaluate_script('Object.keys(localStorage)')).to be_empty
expect(@session.evaluate_script('Object.keys(sessionStorage)')).to be_empty
session = TestSessions::Chrome
session.visit('/with_js')
session.find(:css, '#set-storage').click
session.reset!
session.visit('/with_js')
expect(session.evaluate_script('Object.keys(localStorage)')).to be_empty
expect(session.evaluate_script('Object.keys(sessionStorage)')).to be_empty
end
it 'does not clear storage when false' do
@session = Capybara::Session.new(:selenium_chrome_not_clear_storage, TestApp)
@session.visit('/with_js')
@session.find(:css, '#set-storage').click
@session.reset!
@session.visit('/with_js')
expect(@session.evaluate_script('Object.keys(localStorage)')).not_to be_empty
expect(@session.evaluate_script('Object.keys(sessionStorage)')).not_to be_empty
session = Capybara::Session.new(:selenium_chrome_not_clear_storage, TestApp)
session.visit('/with_js')
session.find(:css, '#set-storage').click
session.reset!
session.visit('/with_js')
expect(session.evaluate_script('Object.keys(localStorage)')).not_to be_empty
expect(session.evaluate_script('Object.keys(sessionStorage)')).not_to be_empty
end
end
end
@ -79,29 +79,29 @@ RSpec.describe 'Capybara::Session with chrome' do
describe 'filling in Chrome-specific date and time fields with keystrokes' do
let(:datetime) { Time.new(1983, 6, 19, 6, 30) }
let(:session) { TestSessions::Chrome }
before do
@session = TestSessions::Chrome
@session.visit('/form')
session.visit('/form')
end
it 'should fill in a date input with a String' do
@session.fill_in('form_date', with: '06/19/1983')
@session.click_button('awesome')
expect(Date.parse(extract_results(@session)['date'])).to eq datetime.to_date
session.fill_in('form_date', with: '06/19/1983')
session.click_button('awesome')
expect(Date.parse(extract_results(session)['date'])).to eq datetime.to_date
end
it 'should fill in a time input with a String' do
@session.fill_in('form_time', with: '06:30A')
@session.click_button('awesome')
results = extract_results(@session)['time']
session.fill_in('form_time', with: '06:30A')
session.click_button('awesome')
results = extract_results(session)['time']
expect(Time.parse(results).strftime('%r')).to eq datetime.strftime('%r')
end
it 'should fill in a datetime input with a String' do
@session.fill_in('form_datetime', with: "06/19/1983\t06:30A")
@session.click_button('awesome')
expect(Time.parse(extract_results(@session)['datetime'])).to eq datetime
session.fill_in('form_datetime', with: "06/19/1983\t06:30A")
session.click_button('awesome')
expect(Time.parse(extract_results(session)['datetime'])).to eq datetime
end
end
end

View File

@ -74,51 +74,47 @@ RSpec.describe 'Capybara::Session with firefox' do # rubocop:disable RSpec/Multi
end
RSpec.describe Capybara::Selenium::Driver do
before do
@driver = Capybara::Selenium::Driver.new(TestApp, browser: :firefox, options: browser_options)
end
let(:driver) { Capybara::Selenium::Driver.new(TestApp, browser: :firefox, options: browser_options) }
describe '#quit' do
it 'should reset browser when quit' do
expect(@driver.browser).to be_truthy
@driver.quit
expect(driver.browser).to be_truthy
driver.quit
# access instance variable directly so we don't create a new browser instance
expect(@driver.instance_variable_get(:@browser)).to be_nil
expect(driver.instance_variable_get(:@browser)).to be_nil
end
context 'with errors' do
before do
@original_browser = @driver.browser
end
let!(:original_browser) { driver.browser }
after do
# Ensure browser is actually quit so we don't leave hanging processe
RSpec::Mocks.space.proxy_for(@original_browser).reset
@original_browser.quit
RSpec::Mocks.space.proxy_for(original_browser).reset
original_browser.quit
end
it 'warns UnknownError returned during quit because the browser is probably already gone' do
allow(@driver).to receive(:warn)
allow(@driver.browser).to(
allow(driver).to receive(:warn)
allow(driver.browser).to(
receive(:quit)
.and_raise(Selenium::WebDriver::Error::UnknownError, 'random message')
)
expect { @driver.quit }.not_to raise_error
expect(@driver.instance_variable_get(:@browser)).to be_nil
expect(@driver).to have_received(:warn).with(/random message/)
expect { driver.quit }.not_to raise_error
expect(driver.instance_variable_get(:@browser)).to be_nil
expect(driver).to have_received(:warn).with(/random message/)
end
it 'ignores silenced UnknownError returned during quit because the browser is almost definitely already gone' do
allow(@driver).to receive(:warn)
allow(@driver.browser).to(
allow(driver).to receive(:warn)
allow(driver.browser).to(
receive(:quit)
.and_raise(Selenium::WebDriver::Error::UnknownError, 'Error communicating with the remote browser')
)
expect { @driver.quit }.not_to raise_error
expect(@driver.instance_variable_get(:@browser)).to be_nil
expect(@driver).not_to have_received(:warn)
expect { driver.quit }.not_to raise_error
expect(driver.instance_variable_get(:@browser)).to be_nil
expect(driver).not_to have_received(:warn)
end
end
end
@ -126,23 +122,23 @@ RSpec.describe Capybara::Selenium::Driver do
context 'storage' do
describe '#reset!' do
it 'clears storage by default' do
@session = TestSessions::SeleniumFirefox
@session.visit('/with_js')
@session.find(:css, '#set-storage').click
@session.reset!
@session.visit('/with_js')
expect(@session.driver.browser.local_storage.keys).to be_empty
expect(@session.driver.browser.session_storage.keys).to be_empty
session = TestSessions::SeleniumFirefox
session.visit('/with_js')
session.find(:css, '#set-storage').click
session.reset!
session.visit('/with_js')
expect(session.driver.browser.local_storage.keys).to be_empty
expect(session.driver.browser.session_storage.keys).to be_empty
end
it 'does not clear storage when false' do
@session = Capybara::Session.new(:selenium_firefox_not_clear_storage, TestApp)
@session.visit('/with_js')
@session.find(:css, '#set-storage').click
@session.reset!
@session.visit('/with_js')
expect(@session.driver.browser.local_storage.keys).not_to be_empty
expect(@session.driver.browser.session_storage.keys).not_to be_empty
session = Capybara::Session.new(:selenium_firefox_not_clear_storage, TestApp)
session.visit('/with_js')
session.find(:css, '#set-storage').click
session.reset!
session.visit('/with_js')
expect(session.driver.browser.local_storage.keys).not_to be_empty
expect(session.driver.browser.session_storage.keys).not_to be_empty
end
end
end

View File

@ -4,17 +4,17 @@ require 'spec_helper'
RSpec.describe Capybara::Server do
it 'should spool up a rack server' do
@app = proc { |_env| [200, {}, ['Hello Server!']] }
@server = Capybara::Server.new(@app).boot
app = proc { |_env| [200, {}, ['Hello Server!']] }
server = Capybara::Server.new(app).boot
@res = Net::HTTP.start(@server.host, @server.port) { |http| http.get('/') }
res = Net::HTTP.start(server.host, server.port) { |http| http.get('/') }
expect(@res.body).to include('Hello Server')
expect(res.body).to include('Hello Server')
end
it 'should do nothing when no server given' do
expect do
@server = Capybara::Server.new(nil).boot
Capybara::Server.new(nil).boot
end.not_to raise_error
end
@ -42,37 +42,36 @@ RSpec.describe Capybara::Server do
it 'should use specified port' do
Capybara.server_port = 22789
@app = proc { |_env| [200, {}, ['Hello Server!']] }
@server = Capybara::Server.new(@app).boot
app = proc { |_env| [200, {}, ['Hello Server!']] }
server = Capybara::Server.new(app).boot
@res = Net::HTTP.start(@server.host, 22789) { |http| http.get('/') }
expect(@res.body).to include('Hello Server')
res = Net::HTTP.start(server.host, 22789) { |http| http.get('/') }
expect(res.body).to include('Hello Server')
Capybara.server_port = nil
end
it 'should use given port' do
@app = proc { |_env| [200, {}, ['Hello Server!']] }
@server = Capybara::Server.new(@app, port: 22790).boot
app = proc { |_env| [200, {}, ['Hello Server!']] }
server = Capybara::Server.new(app, port: 22790).boot
@res = Net::HTTP.start(@server.host, 22790) { |http| http.get('/') }
expect(@res.body).to include('Hello Server')
res = Net::HTTP.start(server.host, 22790) { |http| http.get('/') }
expect(res.body).to include('Hello Server')
Capybara.server_port = nil
end
it 'should find an available port' do
@app1 = proc { |_env| [200, {}, ['Hello Server!']] }
@app2 = proc { |_env| [200, {}, ['Hello Second Server!']] }
responses = ['Hello Server!', 'Hello Second Server!']
apps = responses.map do |response|
proc { |_env| [200, {}, [response]] }
end
servers = apps.map { |app| Capybara::Server.new(app).boot }
@server1 = Capybara::Server.new(@app1).boot
@server2 = Capybara::Server.new(@app2).boot
@res1 = Net::HTTP.start(@server1.host, @server1.port) { |http| http.get('/') }
expect(@res1.body).to include('Hello Server')
@res2 = Net::HTTP.start(@server2.host, @server2.port) { |http| http.get('/') }
expect(@res2.body).to include('Hello Second Server')
servers.each_with_index do |server, idx|
result = Net::HTTP.start(server.host, server.port) { |http| http.get('/') }
expect(result.body).to include(responses[idx])
end
end
it 'should support SSL' do
@ -98,28 +97,27 @@ RSpec.describe Capybara::Server do
end
context 'When Capybara.reuse_server is true' do
let!(:old_reuse_server) { Capybara.reuse_server }
before do
@old_reuse_server = Capybara.reuse_server
Capybara.reuse_server = true
end
after do
Capybara.reuse_server = @old_reuse_server
Capybara.reuse_server = old_reuse_server
end
it 'should use the existing server if it already running' do
@app = proc { |_env| [200, {}, ['Hello Server!']] }
app = proc { |_env| [200, {}, ['Hello Server!']] }
@server1 = Capybara::Server.new(@app).boot
@server2 = Capybara::Server.new(@app).boot
servers = Array.new(2) { Capybara::Server.new(app).boot }
res = Net::HTTP.start(@server1.host, @server1.port) { |http| http.get('/') }
expect(res.body).to include('Hello Server')
servers.each do |server|
res = Net::HTTP.start(server.host, server.port) { |http| http.get('/') }
expect(res.body).to include('Hello Server')
end
res = Net::HTTP.start(@server2.host, @server2.port) { |http| http.get('/') }
expect(res.body).to include('Hello Server')
expect(@server1.port).to eq(@server2.port)
expect(servers[0].port).to eq(servers[1].port)
end
it 'detects and waits for all reused server sessions pending requests' do
@ -151,22 +149,20 @@ RSpec.describe Capybara::Server do
end
after do
Capybara.reuse_server = @old_reuse_server
Capybara.reuse_server = @old_reuse_server # rubocop:disable RSpec/InstanceVariable
end
it 'should not reuse an already running server' do
@app = proc { |_env| [200, {}, ['Hello Server!']] }
app = proc { |_env| [200, {}, ['Hello Server!']] }
@server1 = Capybara::Server.new(@app).boot
@server2 = Capybara::Server.new(@app).boot
servers = Array.new(2) { Capybara::Server.new(app).boot }
res = Net::HTTP.start(@server1.host, @server1.port) { |http| http.get('/') }
expect(res.body).to include('Hello Server')
servers.each do |server|
res = Net::HTTP.start(server.host, server.port) { |http| http.get('/') }
expect(res.body).to include('Hello Server')
end
res = Net::HTTP.start(@server2.host, @server2.port) { |http| http.get('/') }
expect(res.body).to include('Hello Server')
expect(@server1.port).not_to eq(@server2.port)
expect(servers[0].port).not_to eq(servers[1].port)
end
it 'detects and waits for only one sessions pending requests' do

View File

@ -28,27 +28,28 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode|
end
describe 'exit codes' do
let(:env) { { 'SELENIUM_BROWSER' => session.driver.options[:browser].to_s } }
let!(:orig_dir) { Dir.getwd }
before do
@current_dir = Dir.getwd
Dir.chdir(File.join(File.dirname(__FILE__), '..'))
@env = { 'SELENIUM_BROWSER' => session.driver.options[:browser].to_s }
end
after do
Dir.chdir(@current_dir)
Dir.chdir(orig_dir)
end
it 'should have return code 1 when running selenium_driver_rspec_failure.rb' do
skip 'only setup for local non-headless' if headless_or_remote?
system(@env, 'rspec spec/fixtures/selenium_driver_rspec_failure.rb', out: File::NULL, err: File::NULL)
system(env, 'rspec spec/fixtures/selenium_driver_rspec_failure.rb', out: File::NULL, err: File::NULL)
expect($CHILD_STATUS.exitstatus).to eq(1)
end
it 'should have return code 0 when running selenium_driver_rspec_success.rb' do
skip 'only setup for local non-headless' if headless_or_remote?
system(@env, 'rspec spec/fixtures/selenium_driver_rspec_success.rb', out: File::NULL, err: File::NULL)
system(env, 'rspec spec/fixtures/selenium_driver_rspec_success.rb', out: File::NULL, err: File::NULL)
expect($CHILD_STATUS.exitstatus).to eq(0)
end
end
@ -351,8 +352,8 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode|
pending "IE doesn't support uploading a directory" if ie?(session)
session.visit('/form')
@test_file_dir = File.expand_path('./fixtures', File.dirname(__FILE__))
session.attach_file('Directory Upload', @test_file_dir)
test_file_dir = File.expand_path('./fixtures', File.dirname(__FILE__))
session.attach_file('Directory Upload', test_file_dir)
session.click_button('Upload Multiple')
expect(session.body).to include('5 | ') # number of files
end
@ -366,6 +367,7 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode|
end
end
# rubocop:disable RSpec/InstanceVariable
describe 'Capybara#disable_animation' do
context 'when set to `true`' do
before(:context) do # rubocop:disable RSpec/BeforeAfterAll
@ -375,10 +377,6 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode|
@animation_session = Capybara::Session.new(session.mode, TestApp.new)
end
after(:context) do # rubocop:disable RSpec/BeforeAfterAll
@animation_session = nil
end
it 'should disable CSS transitions' do
@animation_session.visit('with_animation')
@animation_session.click_link('transition me away')
@ -400,10 +398,6 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode|
@animation_session_with_matching_css = Capybara::Session.new(session.mode, TestApp.new)
end
after(:context) do # rubocop:disable RSpec/BeforeAfterAll
@animation_session_with_matching_css = nil
end
it 'should disable CSS transitions' do
@animation_session_with_matching_css.visit('with_animation')
@animation_session_with_matching_css.click_link('transition me away')
@ -425,10 +419,6 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode|
@animation_session_without_matching_css = Capybara::Session.new(session.mode, TestApp.new)
end
after(:context) do # rubocop:disable RSpec/BeforeAfterAll
@animation_session_without_matching_css = nil
end
it 'should not disable CSS transitions' do
@animation_session_without_matching_css.visit('with_animation')
@animation_session_without_matching_css.click_link('transition me away')
@ -446,6 +436,7 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode|
end
end
end
# rubocop:enable RSpec/InstanceVariable
describe ':element selector' do
it 'can find html5 svg elements' do

View File

@ -2,6 +2,7 @@
require 'spec_helper'
# rubocop:disable RSpec/InstanceVariable
RSpec.describe Capybara::Selector::XPathBuilder do
let :builder do
::Capybara::Selector::XPathBuilder.new(@xpath)
@ -89,3 +90,4 @@ RSpec.describe Capybara::Selector::XPathBuilder do
end
end
end
# rubocop:enable RSpec/InstanceVariable