From 6dc161550469380e173a6242434f0d360c81f3bd Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Mon, 17 Feb 2020 11:14:51 -0800 Subject: [PATCH 1/2] Query rewriting in rack_test form submission --- lib/capybara/rack_test/browser.rb | 4 +++- lib/capybara/spec/views/form.erb | 1 + spec/rack_test_spec.rb | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/capybara/rack_test/browser.rb b/lib/capybara/rack_test/browser.rb index d2d583d3..4e4e23b1 100644 --- a/lib/capybara/rack_test/browser.rb +++ b/lib/capybara/rack_test/browser.rb @@ -30,7 +30,9 @@ class Capybara::RackTest::Browser def submit(method, path, attributes) path = request_path if path.nil? || path.empty? - process_and_follow_redirects(method, path, attributes, 'HTTP_REFERER' => current_url) + uri = build_uri(path) + uri.query = '' if method&.to_s&.downcase == 'get' + process_and_follow_redirects(method, uri.to_s, attributes, 'HTTP_REFERER' => current_url) end def follow(method, path, **attributes) diff --git a/lib/capybara/spec/views/form.erb b/lib/capybara/spec/views/form.erb index 28c96ab7..7e2f97e9 100644 --- a/lib/capybara/spec/views/form.erb +++ b/lib/capybara/spec/views/form.erb @@ -517,6 +517,7 @@ New line after and before textarea tag

+

diff --git a/spec/rack_test_spec.rb b/spec/rack_test_spec.rb index 6419bac2..031d0304 100644 --- a/spec/rack_test_spec.rb +++ b/spec/rack_test_spec.rb @@ -127,6 +127,20 @@ RSpec.describe Capybara::Session do # rubocop:disable RSpec/MultipleDescribes session.find(:label, 'Female').click expect(session).to have_unchecked_field('gender_male') end + + it 'should rewrite the forms action query for get submission' do + session.visit('/form') + session.click_button('mediocre') + puts session.current_url + expect(session).not_to have_current_path(/foo|bar/) + end + + it 'should rewrite the submit buttons formaction query for get submission' do + session.visit('/form') + session.click_button('mediocre2') + puts session.current_url + expect(session).not_to have_current_path(/foo|bar/) + end end end From a7cd97ed4d24e8cd4fc59b2349b639fca9fcb82b Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Mon, 17 Feb 2020 11:49:12 -0800 Subject: [PATCH 2/2] Style updates --- .rubocop.yml | 3 ++ lib/capybara/spec/session/find_spec.rb | 19 ++++--- lib/capybara/spec/session/has_css_spec.rb | 15 +++--- lib/capybara/spec/session/node_spec.rb | 51 +++++++++++-------- .../spec/session/window/window_spec.rb | 14 ++--- lib/capybara/spec/spec_helper.rb | 3 +- 6 files changed, 61 insertions(+), 44 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c917993a..fadd7119 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -162,5 +162,8 @@ RSpec/FilePath: RSpec/PredicateMatcher: Enabled: false +# RSpec/InstanceVariable: +# Enabled: false + Capybara/FeatureMethods: Enabled: false diff --git a/lib/capybara/spec/session/find_spec.rb b/lib/capybara/spec/session/find_spec.rb index 2dc09aba..150786d4 100644 --- a/lib/capybara/spec/session/find_spec.rb +++ b/lib/capybara/spec/session/find_spec.rb @@ -451,32 +451,35 @@ Capybara::SpecHelper.spec '#find' do context 'with spatial filters', requires: [:spatial] do before do @session.visit('/spatial') - @center = @session.find(:css, 'div.center') + end + + let :center do + @session.find(:css, 'div.center') end it 'should find an element above another element' do - expect(@session.find(:css, 'div:not(.corner)', above: @center).text).to eq('2') + expect(@session.find(:css, 'div:not(.corner)', above: center).text).to eq('2') end it 'should find an element below another element' do - expect(@session.find(:css, 'div:not(.corner):not(.footer)', below: @center).text).to eq('8') + expect(@session.find(:css, 'div:not(.corner):not(.footer)', below: center).text).to eq('8') end it 'should find an element left of another element' do - expect(@session.find(:css, 'div:not(.corner)', left_of: @center).text).to eq('4') + expect(@session.find(:css, 'div:not(.corner)', left_of: center).text).to eq('4') end it 'should find an element right of another element' do - expect(@session.find(:css, 'div:not(.corner)', right_of: @center).text).to eq('6') + expect(@session.find(:css, 'div:not(.corner)', right_of: center).text).to eq('6') end it 'should combine spatial filters' do - expect(@session.find(:css, 'div', left_of: @center, above: @center).text).to eq('1') - expect(@session.find(:css, 'div', right_of: @center, below: @center).text).to eq('9') + expect(@session.find(:css, 'div', left_of: center, above: center).text).to eq('1') + expect(@session.find(:css, 'div', right_of: center, below: center).text).to eq('9') end it 'should find an element "near" another element' do - expect(@session.find(:css, 'div.distance', near: @center).text).to eq('2') + expect(@session.find(:css, 'div.distance', near: center).text).to eq('2') end end diff --git a/lib/capybara/spec/session/has_css_spec.rb b/lib/capybara/spec/session/has_css_spec.rb index 02486a6f..93328e1b 100644 --- a/lib/capybara/spec/session/has_css_spec.rb +++ b/lib/capybara/spec/session/has_css_spec.rb @@ -234,18 +234,21 @@ Capybara::SpecHelper.spec '#has_css?' do context 'with spatial requirements', requires: [:spatial] do before do @session.visit('/spatial') - @center = @session.find(:css, '.center') + end + + let :center do + @session.find(:css, '.center') end it 'accepts spatial options' do - expect(@session).to have_css('div', above: @center).thrice - expect(@session).to have_css('div', above: @center, right_of: @center).once + expect(@session).to have_css('div', above: center).thrice + expect(@session).to have_css('div', above: center, right_of: center).once end it 'supports spatial sugar' do - expect(@session).to have_css('div').left_of(@center).thrice - expect(@session).to have_css('div').below(@center).right_of(@center).once - expect(@session).to have_css('div').near(@center).exactly(8).times + expect(@session).to have_css('div').left_of(center).thrice + expect(@session).to have_css('div').below(center).right_of(center).once + expect(@session).to have_css('div').near(center).exactly(8).times end end diff --git a/lib/capybara/spec/session/node_spec.rb b/lib/capybara/spec/session/node_spec.rb index 6d5e57c6..8527381c 100644 --- a/lib/capybara/spec/session/node_spec.rb +++ b/lib/capybara/spec/session/node_spec.rb @@ -808,7 +808,10 @@ Capybara::SpecHelper.spec 'node' do context 'offset', requires: [:js] do before do @session.visit('/offset') - @clicker = @session.find(:id, 'clicker') + end + + let :clicker do + @session.find(:id, 'clicker') end context 'when w3c_click_offset is false' do @@ -817,17 +820,17 @@ Capybara::SpecHelper.spec 'node' do end it 'should offset from top left of element' do - @clicker.click(x: 10, y: 5) + clicker.click(x: 10, y: 5) expect(@session).to have_text(/clicked at 110,105/) end it 'should offset outside the element' do - @clicker.click(x: -15, y: -10) + clicker.click(x: -15, y: -10) expect(@session).to have_text(/clicked at 85,90/) end it 'should default to click the middle' do - @clicker.click + clicker.click expect(@session).to have_text(/clicked at 150,150/) end end @@ -838,17 +841,17 @@ Capybara::SpecHelper.spec 'node' do end it 'should offset from center of element' do - @clicker.click(x: 10, y: 5) + clicker.click(x: 10, y: 5) expect(@session).to have_text(/clicked at 160,155/) end it 'should offset outside from center of element' do - @clicker.click(x: -65, y: -60) + clicker.click(x: -65, y: -60) expect(@session).to have_text(/clicked at 85,90/) end it 'should default to click the middle' do - @clicker.click + clicker.click expect(@session).to have_text(/clicked at 150,150/) end end @@ -891,7 +894,10 @@ Capybara::SpecHelper.spec 'node' do context 'offset', requires: [:js] do before do @session.visit('/offset') - @clicker = @session.find(:id, 'clicker') + end + + let :clicker do + @session.find(:id, 'clicker') end context 'when w3c_click_offset is false' do @@ -900,17 +906,17 @@ Capybara::SpecHelper.spec 'node' do end it 'should offset from top left of element' do - @clicker.double_click(x: 10, y: 5) + clicker.double_click(x: 10, y: 5) expect(@session).to have_text(/clicked at 110,105/) end it 'should offset outside the element' do - @clicker.double_click(x: -15, y: -10) + clicker.double_click(x: -15, y: -10) expect(@session).to have_text(/clicked at 85,90/) end it 'should default to click the middle' do - @clicker.double_click + clicker.double_click expect(@session).to have_text(/clicked at 150,150/) end end @@ -921,17 +927,17 @@ Capybara::SpecHelper.spec 'node' do end it 'should offset from center of element' do - @clicker.double_click(x: 10, y: 5) + clicker.double_click(x: 10, y: 5) expect(@session).to have_text(/clicked at 160,155/) end it 'should offset outside from center of element' do - @clicker.double_click(x: -65, y: -60) + clicker.double_click(x: -65, y: -60) expect(@session).to have_text(/clicked at 85,90/) end it 'should default to click the middle' do - @clicker.double_click + clicker.double_click expect(@session).to have_text(/clicked at 150,150/) end end @@ -974,7 +980,10 @@ Capybara::SpecHelper.spec 'node' do context 'offset', requires: [:js] do before do @session.visit('/offset') - @clicker = @session.find(:id, 'clicker') + end + + let :clicker do + @session.find(:id, 'clicker') end context 'when w3c_click_offset is false' do @@ -983,17 +992,17 @@ Capybara::SpecHelper.spec 'node' do end it 'should offset from top left of element' do - @clicker.right_click(x: 10, y: 5) + clicker.right_click(x: 10, y: 5) expect(@session).to have_text(/clicked at 110,105/) end it 'should offset outside the element' do - @clicker.right_click(x: -15, y: -10) + clicker.right_click(x: -15, y: -10) expect(@session).to have_text(/clicked at 85,90/) end it 'should default to click the middle' do - @clicker.right_click + clicker.right_click expect(@session).to have_text(/clicked at 150,150/) end end @@ -1004,17 +1013,17 @@ Capybara::SpecHelper.spec 'node' do end it 'should offset from center of element' do - @clicker.right_click(x: 10, y: 5) + clicker.right_click(x: 10, y: 5) expect(@session).to have_text(/clicked at 160,155/) end it 'should offset outside from center of element' do - @clicker.right_click(x: -65, y: -60) + clicker.right_click(x: -65, y: -60) expect(@session).to have_text(/clicked at 85,90/) end it 'should default to click the middle' do - @clicker.right_click + clicker.right_click expect(@session).to have_text(/clicked at 150,150/) end end diff --git a/lib/capybara/spec/session/window/window_spec.rb b/lib/capybara/spec/session/window/window_spec.rb index 9ef12e34..468f9b70 100644 --- a/lib/capybara/spec/session/window/window_spec.rb +++ b/lib/capybara/spec/session/window/window_spec.rb @@ -141,12 +141,12 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do end describe '#maximize' do - before do - @initial_size = @session.current_window.size + let! :initial_size do + @session.current_window.size end after do - @session.current_window.resize_to(*@initial_size) + @session.current_window.resize_to(*initial_size) sleep 0.5 end @@ -176,7 +176,7 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do 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) + # expect(@session.current_window.size).to eq(initial_size) ow_width, ow_height = other_window.size expect(ow_width).to be > 400 @@ -185,12 +185,12 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do end describe '#fullscreen' do - before do - @initial_size = @session.current_window.size + let! :initial_size do + @session.current_window.size end after do - @session.current_window.resize_to(*@initial_size) + @session.current_window.resize_to(*initial_size) sleep 1 end diff --git a/lib/capybara/spec/spec_helper.rb b/lib/capybara/spec/spec_helper.rb index dff9d8bb..ec3c5a77 100644 --- a/lib/capybara/spec/spec_helper.rb +++ b/lib/capybara/spec/spec_helper.rb @@ -60,7 +60,7 @@ module Capybara RSpec.describe Capybara::Session, name, options do # rubocop:disable RSpec/EmptyExampleGroup include Capybara::SpecHelper include Capybara::RSpecMatchers - # rubocop:disable RSpec/ScatteredSetup + before do |example| @session = session instance_exec(example, &filter_block) if filter_block @@ -81,7 +81,6 @@ module Capybara before :each, :exact_false do Capybara.exact = false end - # rubocop:enable RSpec/ScatteredSetup specs.each do |spec_name, spec_options, block| describe spec_name, *spec_options do # rubocop:disable RSpec/EmptyExampleGroup