From be224eeebe830af96d7ae01115b2d89a0996ae86 Mon Sep 17 00:00:00 2001 From: mcrmfc Date: Tue, 22 Nov 2011 17:18:02 +0000 Subject: [PATCH 1/2] fixes Capybara::Session with Mechanize driver it should behave like session it should behave like click_link#click_link should follow relative links --- lib/capybara/mechanize/node.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/capybara/mechanize/node.rb b/lib/capybara/mechanize/node.rb index ffc1b51..d0f9324 100644 --- a/lib/capybara/mechanize/node.rb +++ b/lib/capybara/mechanize/node.rb @@ -2,9 +2,11 @@ class Capybara::Mechanize::Node < Capybara::RackTest::Node def click if tag_name == 'a' method = self["data-method"] || :get - driver.follow(method, self[:href].to_s) + href = self[:href].to_s + href = "/#{href}" if href !~ /^\/|^#|^\?|^http/ + driver.follow(method, href) elsif (tag_name == 'input' and %w(submit image).include?(type)) or - ((tag_name == 'button') and type.nil? or type == "submit") + ((tag_name == 'button') and type.nil? or type == "submit") Capybara::Mechanize::Form.new(driver, form).submit(self) end end From fe4910555d1da71908dcf82e0eba2ff8b6a3ea71 Mon Sep 17 00:00:00 2001 From: mcrmfc Date: Tue, 22 Nov 2011 17:19:24 +0000 Subject: [PATCH 2/2] allows us to handle a Google search scenario i.e. a GET request on a form with a relative action - where mechanize needs the full URL not just the action --- lib/capybara/mechanize/form.rb | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/capybara/mechanize/form.rb b/lib/capybara/mechanize/form.rb index 80a6543..bde8ff9 100644 --- a/lib/capybara/mechanize/form.rb +++ b/lib/capybara/mechanize/form.rb @@ -1,33 +1,32 @@ require 'mechanize' class Capybara::Mechanize::Form < Mechanize::Form - + def initialize(driver, form_node) @driver = driver form_node = form_node.clone - + form_node.search('*[disabled=disabled]').remove - + super(form_node, @driver.browser.agent) end - + def submit(button_node) button = Mechanize::Form::Button.new(button_node) - self.action ||= @driver.current_url - + resolve_action(@driver.current_url) + self.normalize_uploads! self.add_button_to_query(button) - @driver.submit(self) end - + protected - + def normalize_uploads! self.file_uploads.each do |upload| file_path = upload.node["value"] next if file_path.nil? || file_path == '' - + if self.multipart? upload.file_name = file_path else @@ -36,8 +35,19 @@ def normalize_uploads! end end end - + def multipart? !!(self.enctype.downcase =~ /^multipart\/form-data/) end + + private + + def resolve_action(current_url) + if self.method.upcase == 'GET' and self.action !~ /^http/ and self.action.to_s != "" + uri = URI.parse(current_url) + self.action = "#{uri.scheme}://#{uri.host}:#{uri.port}#{self.action}" + else + self.action ||= current_url + end + end end