Basic spike of WWW:Mechanize support

This commit is contained in:
Bryan Helmkamp 2008-05-12 00:23:37 -04:00
parent cf6b9d26c0
commit 5bb3026daf
13 changed files with 73 additions and 17 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

5
lib/webrat/mechanize.rb Normal file
View File

@ -0,0 +1,5 @@
require "rubygems"
require "mechanize"
require File.dirname(__FILE__) + "/mechanize/mechanize_session"

View File

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

View File

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

View File

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

10
mechanize_spike.rb Normal file
View File

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

View File

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

View File

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