From 5bb3026dafc7e32f1a2ea2197a5183e05c97f9a3 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 12 May 2008 00:23:37 -0400 Subject: [PATCH] Basic spike of WWW:Mechanize support --- History.txt | 1 + Rakefile | 2 +- lib/webrat.rb | 5 ++++- lib/webrat/core/field.rb | 4 +++- lib/webrat/core/page.rb | 18 ++++++++-------- lib/webrat/core/session.rb | 10 +++++++++ lib/webrat/mechanize.rb | 5 +++++ lib/webrat/mechanize/mechanize_session.rb | 25 +++++++++++++++++++++++ lib/webrat/rails/rails_session.rb | 4 ++++ lib/webrat/rails/session.rb | 4 ---- mechanize_spike.rb | 10 +++++++++ spec/api/reloads_spec.rb | 1 + spec/api/visits_spec.rb | 1 + 13 files changed, 73 insertions(+), 17 deletions(-) create mode 100644 lib/webrat/mechanize.rb create mode 100644 lib/webrat/mechanize/mechanize_session.rb create mode 100644 mechanize_spike.rb diff --git a/History.txt b/History.txt index 77af696..a18a7a4 100644 --- a/History.txt +++ b/History.txt @@ -12,6 +12,7 @@ * Fix bug with empty select list option (Patch from Kyle Hargraves) * Fix regression of not sending default values in password fields + * Don't explode if encountering inputs with no type attribute (assume text) == 0.2.0 / 2008-04-04 diff --git a/Rakefile b/Rakefile index 0bd0729..d635877 100644 --- a/Rakefile +++ b/Rakefile @@ -56,7 +56,7 @@ end require 'spec/rake/verify_rcov' RCov::VerifyTask.new(:verify_rcov => :rcov) do |t| - t.threshold = 91.4 # Make sure you have rcov 0.7 or higher! + t.threshold = 95.7 # Make sure you have rcov 0.7 or higher! end remove_task "default" diff --git a/lib/webrat.rb b/lib/webrat.rb index 3b968ae..dffddac 100644 --- a/lib/webrat.rb +++ b/lib/webrat.rb @@ -2,5 +2,8 @@ module Webrat VERSION = '0.2.1' end +require "rubygems" +require "active_support" + require File.dirname(__FILE__) + "/webrat/core" -require File.dirname(__FILE__) + "/webrat/rails" +require File.dirname(__FILE__) + "/webrat/rails" if defined?(RAILS_ENV) diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index e45a192..471d0a4 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -6,7 +6,7 @@ module Webrat if %w[submit image].include?(element["type"]) field_class = "button" else - field_class = element["type"] + field_class = element["type"] || "text" end else field_class = element.name @@ -93,6 +93,8 @@ module Webrat if defined?(CGIMethods) CGIMethods else + require "action_controller" + require "action_controller/integration" ActionController::AbstractRequest end end diff --git a/lib/webrat/core/page.rb b/lib/webrat/core/page.rb index 2d8228b..55cc675 100644 --- a/lib/webrat/core/page.rb +++ b/lib/webrat/core/page.rb @@ -1,3 +1,4 @@ +require "rubygems" require "hpricot" require "English" @@ -105,13 +106,15 @@ module Webrat # Example: # save_and_open def save_and_open - return unless File.exist?(RAILS_ROOT + "/tmp") + return unless File.exist?(session.saved_page_dir) - filename = "webrat-#{Time.now.to_i}.html" - File.open(RAILS_ROOT + "/tmp/#{filename}", "w") do |f| - f.write response.body + filename = "#{session.saved_page_dir}/webrat-#{Time.now.to_i}.html" + + File.open(filename, "w") do |f| + f.write session.response_body end - `open tmp/#{filename}` + + `open #{filename}` end # Issues a request for the URL pointed to by a link on the current page, @@ -256,14 +259,9 @@ module Webrat def reset_dom @dom = nil - @links = nil @forms = nil end - def links - @links ||= links_within(nil) - end - def links_within(selector) (dom / selector / "a[@href]").map do |link_element| Link.new(self, link_element) diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index fed45fa..30876dc 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -1,5 +1,10 @@ module Webrat class Session + + def saved_page_dir + File.expand_path(".") + end + def current_page @current_page ||= Page.new(self) end @@ -16,6 +21,10 @@ module Webrat super || current_page.respond_to?(name) end + def save_and_open_page + current_page.save_and_open + end + def method_missing(name, *args) if current_page.respond_to?(name) current_page.send(name, *args) @@ -23,5 +32,6 @@ module Webrat super end end + end end \ No newline at end of file diff --git a/lib/webrat/mechanize.rb b/lib/webrat/mechanize.rb new file mode 100644 index 0000000..5b8f1aa --- /dev/null +++ b/lib/webrat/mechanize.rb @@ -0,0 +1,5 @@ +require "rubygems" +require "mechanize" + +require File.dirname(__FILE__) + "/mechanize/mechanize_session" + diff --git a/lib/webrat/mechanize/mechanize_session.rb b/lib/webrat/mechanize/mechanize_session.rb new file mode 100644 index 0000000..2f48c17 --- /dev/null +++ b/lib/webrat/mechanize/mechanize_session.rb @@ -0,0 +1,25 @@ +module Webrat + class MechanizeSession < Session + + def initialize(mechanize = WWW::Mechanize.new) + @mechanize = mechanize + end + + def get(url, data) + @mechanize_page = @mechanize.get(url, data) + end + + def post(url, data) + @mechanize_page = @mechanize.post(url, data) + end + + def response_body + @mechanize_page.content + end + + def response_code + @mechanize_page.code.to_i + end + + end +end \ No newline at end of file diff --git a/lib/webrat/rails/rails_session.rb b/lib/webrat/rails/rails_session.rb index 07953d6..960fc71 100644 --- a/lib/webrat/rails/rails_session.rb +++ b/lib/webrat/rails/rails_session.rb @@ -5,6 +5,10 @@ module Webrat @integration_session = integration_session end + def saved_page_dir + File.expand_path(File.join(RAILS_ROOT, "tmp")) + end + def get(url, data) @integration_session.get_via_redirect(url, data) end diff --git a/lib/webrat/rails/session.rb b/lib/webrat/rails/session.rb index 4dee465..1b73721 100644 --- a/lib/webrat/rails/session.rb +++ b/lib/webrat/rails/session.rb @@ -15,10 +15,6 @@ module ActionController Webrat::Page.new(webrat_session, *args) end - def save_and_open_page - webrat_session.save_and_open_page - end - def respond_to?(name) super || webrat_session.respond_to?(name) end diff --git a/mechanize_spike.rb b/mechanize_spike.rb new file mode 100644 index 0000000..095afd0 --- /dev/null +++ b/mechanize_spike.rb @@ -0,0 +1,10 @@ +require "lib/webrat" +require "lib/webrat/mechanize" + +include Webrat + +sess = MechanizeSession.new +sess.visits "http://www.google.com/" +sess.fills_in "q", :with => "Webrat" +sess.clicks_button +sess.save_and_open_page \ No newline at end of file diff --git a/spec/api/reloads_spec.rb b/spec/api/reloads_spec.rb index 895116a..5a0861d 100644 --- a/spec/api/reloads_spec.rb +++ b/spec/api/reloads_spec.rb @@ -5,6 +5,7 @@ RAILS_ROOT = "." unless defined?(RAILS_ROOT) describe "reloads" do before do @session = Webrat::TestSession.new + @session.response_body = "Hello world" end it "should reload the page" do diff --git a/spec/api/visits_spec.rb b/spec/api/visits_spec.rb index fbe13ae..df50c96 100644 --- a/spec/api/visits_spec.rb +++ b/spec/api/visits_spec.rb @@ -5,6 +5,7 @@ RAILS_ROOT = "." unless defined?(RAILS_ROOT) describe "visits" do before do @session = Webrat::TestSession.new + @session.response_body = "Hello world" end it "should use get" do