Basic spike of WWW:Mechanize support
This commit is contained in:
parent
cf6b9d26c0
commit
5bb3026daf
|
@ -12,6 +12,7 @@
|
||||||
|
|
||||||
* Fix bug with empty select list option (Patch from Kyle Hargraves)
|
* Fix bug with empty select list option (Patch from Kyle Hargraves)
|
||||||
* Fix regression of not sending default values in password fields
|
* 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
|
== 0.2.0 / 2008-04-04
|
||||||
|
|
||||||
|
|
2
Rakefile
2
Rakefile
|
@ -56,7 +56,7 @@ end
|
||||||
|
|
||||||
require 'spec/rake/verify_rcov'
|
require 'spec/rake/verify_rcov'
|
||||||
RCov::VerifyTask.new(:verify_rcov => :rcov) do |t|
|
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
|
end
|
||||||
|
|
||||||
remove_task "default"
|
remove_task "default"
|
||||||
|
|
|
@ -2,5 +2,8 @@ module Webrat
|
||||||
VERSION = '0.2.1'
|
VERSION = '0.2.1'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
require "rubygems"
|
||||||
|
require "active_support"
|
||||||
|
|
||||||
require File.dirname(__FILE__) + "/webrat/core"
|
require File.dirname(__FILE__) + "/webrat/core"
|
||||||
require File.dirname(__FILE__) + "/webrat/rails"
|
require File.dirname(__FILE__) + "/webrat/rails" if defined?(RAILS_ENV)
|
||||||
|
|
|
@ -6,7 +6,7 @@ module Webrat
|
||||||
if %w[submit image].include?(element["type"])
|
if %w[submit image].include?(element["type"])
|
||||||
field_class = "button"
|
field_class = "button"
|
||||||
else
|
else
|
||||||
field_class = element["type"]
|
field_class = element["type"] || "text"
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
field_class = element.name
|
field_class = element.name
|
||||||
|
@ -93,6 +93,8 @@ module Webrat
|
||||||
if defined?(CGIMethods)
|
if defined?(CGIMethods)
|
||||||
CGIMethods
|
CGIMethods
|
||||||
else
|
else
|
||||||
|
require "action_controller"
|
||||||
|
require "action_controller/integration"
|
||||||
ActionController::AbstractRequest
|
ActionController::AbstractRequest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
require "rubygems"
|
||||||
require "hpricot"
|
require "hpricot"
|
||||||
require "English"
|
require "English"
|
||||||
|
|
||||||
|
@ -105,13 +106,15 @@ module Webrat
|
||||||
# Example:
|
# Example:
|
||||||
# save_and_open
|
# save_and_open
|
||||||
def 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"
|
filename = "#{session.saved_page_dir}/webrat-#{Time.now.to_i}.html"
|
||||||
File.open(RAILS_ROOT + "/tmp/#{filename}", "w") do |f|
|
|
||||||
f.write response.body
|
File.open(filename, "w") do |f|
|
||||||
|
f.write session.response_body
|
||||||
end
|
end
|
||||||
`open tmp/#{filename}`
|
|
||||||
|
`open #{filename}`
|
||||||
end
|
end
|
||||||
|
|
||||||
# Issues a request for the URL pointed to by a link on the current page,
|
# Issues a request for the URL pointed to by a link on the current page,
|
||||||
|
@ -256,14 +259,9 @@ module Webrat
|
||||||
|
|
||||||
def reset_dom
|
def reset_dom
|
||||||
@dom = nil
|
@dom = nil
|
||||||
@links = nil
|
|
||||||
@forms = nil
|
@forms = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def links
|
|
||||||
@links ||= links_within(nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
def links_within(selector)
|
def links_within(selector)
|
||||||
(dom / selector / "a[@href]").map do |link_element|
|
(dom / selector / "a[@href]").map do |link_element|
|
||||||
Link.new(self, link_element)
|
Link.new(self, link_element)
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
module Webrat
|
module Webrat
|
||||||
class Session
|
class Session
|
||||||
|
|
||||||
|
def saved_page_dir
|
||||||
|
File.expand_path(".")
|
||||||
|
end
|
||||||
|
|
||||||
def current_page
|
def current_page
|
||||||
@current_page ||= Page.new(self)
|
@current_page ||= Page.new(self)
|
||||||
end
|
end
|
||||||
|
@ -16,6 +21,10 @@ module Webrat
|
||||||
super || current_page.respond_to?(name)
|
super || current_page.respond_to?(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def save_and_open_page
|
||||||
|
current_page.save_and_open
|
||||||
|
end
|
||||||
|
|
||||||
def method_missing(name, *args)
|
def method_missing(name, *args)
|
||||||
if current_page.respond_to?(name)
|
if current_page.respond_to?(name)
|
||||||
current_page.send(name, *args)
|
current_page.send(name, *args)
|
||||||
|
@ -23,5 +32,6 @@ module Webrat
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -0,0 +1,5 @@
|
||||||
|
require "rubygems"
|
||||||
|
require "mechanize"
|
||||||
|
|
||||||
|
require File.dirname(__FILE__) + "/mechanize/mechanize_session"
|
||||||
|
|
|
@ -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
|
|
@ -5,6 +5,10 @@ module Webrat
|
||||||
@integration_session = integration_session
|
@integration_session = integration_session
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def saved_page_dir
|
||||||
|
File.expand_path(File.join(RAILS_ROOT, "tmp"))
|
||||||
|
end
|
||||||
|
|
||||||
def get(url, data)
|
def get(url, data)
|
||||||
@integration_session.get_via_redirect(url, data)
|
@integration_session.get_via_redirect(url, data)
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,10 +15,6 @@ module ActionController
|
||||||
Webrat::Page.new(webrat_session, *args)
|
Webrat::Page.new(webrat_session, *args)
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_and_open_page
|
|
||||||
webrat_session.save_and_open_page
|
|
||||||
end
|
|
||||||
|
|
||||||
def respond_to?(name)
|
def respond_to?(name)
|
||||||
super || webrat_session.respond_to?(name)
|
super || webrat_session.respond_to?(name)
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
|
@ -5,6 +5,7 @@ RAILS_ROOT = "." unless defined?(RAILS_ROOT)
|
||||||
describe "reloads" do
|
describe "reloads" do
|
||||||
before do
|
before do
|
||||||
@session = Webrat::TestSession.new
|
@session = Webrat::TestSession.new
|
||||||
|
@session.response_body = "Hello world"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should reload the page" do
|
it "should reload the page" do
|
||||||
|
|
|
@ -5,6 +5,7 @@ RAILS_ROOT = "." unless defined?(RAILS_ROOT)
|
||||||
describe "visits" do
|
describe "visits" do
|
||||||
before do
|
before do
|
||||||
@session = Webrat::TestSession.new
|
@session = Webrat::TestSession.new
|
||||||
|
@session.response_body = "Hello world"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should use get" do
|
it "should use get" do
|
||||||
|
|
Loading…
Reference in New Issue