Skip to content
This repository was archived by the owner on Feb 28, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions lib/capybara/mechanize/form.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
6 changes: 4 additions & 2 deletions lib/capybara/mechanize/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down