Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:brynary/webrat
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Jun 20, 2008
2 parents b3bc544 + 7d94c16 commit 856b6cb
Show file tree
Hide file tree
Showing 38 changed files with 749 additions and 373 deletions.
5 changes: 4 additions & 1 deletion History.txt
Expand Up @@ -6,11 +6,14 @@
* Support button elements (Patch from Nick Sieger)
* Support matching select options by regexp (Patch from Kyle Hargraves)
* Support relative links, including href="?foo=bar" (Patch from Kyle Hargraves)
* Support links to fully qualified URLs starting with http:// or https:// (Luke Melia)
* save_and_open_page rewrites css and image references to provide a friendlier debugging experience (Luke Melia)

* Bug fixes

* 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

Expand All @@ -27,7 +30,7 @@
* Added reloads method to reload the page (Patch from Kamal Fariz Mahyuddi)
* Prevent making a request if clicking on local anchor link (Patch from Kamal Fariz Mahyuddi)
* Added clicks_link_within(selector, link_text), allowing restricting link search
to within a given css selector (Path from Luke Melia)
to within a given css selector (Patch from Luke Melia)
* Allow specifying the input name/label when doing a select (Patch from David Chelimsky)
* Raise a specific exception if the developer tries to manipulate form elements before loading a page (Patch from James Deville)
* Add support for alternate POST, PUT and DELETE link clicking (Patch from Kyle Hargraves)
Expand Down
54 changes: 23 additions & 31 deletions Manifest.txt
@@ -1,32 +1,20 @@
.git/HEAD
.git/config
.git/description
.git/hooks/applypatch-msg
.git/hooks/commit-msg
.git/hooks/post-commit
.git/hooks/post-receive
.git/hooks/post-update
.git/hooks/pre-applypatch
.git/hooks/pre-commit
.git/hooks/pre-rebase
.git/hooks/update
.git/index
.git/info/exclude
.git/logs/HEAD
.git/logs/refs/heads/master
.git/logs/refs/remotes/origin/master
.git/objects/pack/pack-3243faf8e8f6f0e226adf39a2c2e8c19c18da6c4.idx
.git/objects/pack/pack-3243faf8e8f6f0e226adf39a2c2e8c19c18da6c4.pack
.git/refs/heads/master
.git/refs/remotes/origin/HEAD
.git/refs/remotes/origin/master
.gitignore
History.txt
MIT-LICENSE.txt
Manifest.txt
README.txt
Rakefile
TODO.txt
coverage/index.html
coverage/lib-webrat-field_rb.html
coverage/lib-webrat-form_rb.html
coverage/lib-webrat-label_rb.html
coverage/lib-webrat-link_rb.html
coverage/lib-webrat-logging_rb.html
coverage/lib-webrat-page_rb.html
coverage/lib-webrat-redirect_actions_rb.html
coverage/lib-webrat-select_option_rb.html
coverage/lib-webrat_rb.html
init.rb
install.rb
lib/webrat.rb
Expand All @@ -38,12 +26,16 @@ lib/webrat/logging.rb
lib/webrat/page.rb
lib/webrat/redirect_actions.rb
lib/webrat/select_option.rb
test/checks_test.rb
test/chooses_test.rb
test/clicks_button_test.rb
test/clicks_link_test.rb
test/fills_in_test.rb
test/helper.rb
test/reloads_test.rb
test/selects_test.rb
test/visits_test.rb
spec/attaches_file_spec.rb
spec/checks_spec.rb
spec/chooses_spec.rb
spec/clicks_button_spec.rb
spec/clicks_link_spec.rb
spec/fills_in_spec.rb
spec/rcov.opts
spec/reloads_spec.rb
spec/save_and_open_page_spec.rb
spec/selects_spec.rb
spec/spec.opts
spec/spec_helper.rb
spec/visits_spec.rb
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -56,7 +56,7 @@ end

require 'spec/rake/verify_rcov'
RCov::VerifyTask.new(:verify_rcov => :rcov) do |t|
t.threshold = 95.5 # Make sure you have rcov 0.7 or higher!
t.threshold = 96.9 # Make sure you have rcov 0.7 or higher!
end

remove_task "default"
Expand Down
46 changes: 4 additions & 42 deletions lib/webrat.rb
@@ -1,47 +1,9 @@
Dir[File.join(File.dirname(__FILE__), "webrat", "*.rb")].each do |file|
require File.expand_path(file)
end

module Webrat
VERSION = '0.2.1'
end

module ActionController
module Integration
class Session

unless instance_methods.include?("put_via_redirect")
include Webrat::RedirectActions
end

def current_page
@current_page ||= Webrat::Page.new(self)
end

def current_page=(new_page)
@current_page = new_page
end

# Issues a GET request for a page, follows any redirects, and verifies the final page
# load was successful.
#
# Example:
# visits "/"
def visits(*args)
@current_page = Webrat::Page.new(self, *args)
end

def save_and_open_page
current_page.save_and_open
end

[:reloads, :fills_in, :clicks_button, :selects, :attaches_file, :chooses, :checks, :unchecks, :clicks_link, :clicks_link_within, :clicks_put_link, :clicks_get_link, :clicks_post_link, :clicks_delete_link].each do |method_name|
define_method(method_name) do |*args|
current_page.send(method_name, *args)
end
end

end
end
end
require "rubygems"
require "active_support"

require File.dirname(__FILE__) + "/webrat/core"
require File.dirname(__FILE__) + "/webrat/rails" if defined?(RAILS_ENV)
3 changes: 3 additions & 0 deletions lib/webrat/core.rb
@@ -0,0 +1,3 @@
Dir[File.join(File.dirname(__FILE__), "core", "*.rb")].each do |file|
require File.expand_path(file)
end
4 changes: 3 additions & 1 deletion lib/webrat/field.rb → lib/webrat/core/field.rb
Expand Up @@ -6,7 +6,7 @@ def self.class_for_element(element)
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
Expand Down Expand Up @@ -93,6 +93,8 @@ def param_parser
if defined?(CGIMethods)
CGIMethods
else
require "action_controller"
require "action_controller/integration"
ActionController::AbstractRequest
end
end
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 9 additions & 3 deletions lib/webrat/link.rb → lib/webrat/core/link.rb
Expand Up @@ -9,8 +9,8 @@ def initialize(page, element)
def click(method = nil)
method ||= http_method
return if href =~ /^#/ && method == :get
Page.new(@page.session, absolute_href, method, authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token})

Page.new(@page.session, absolute_href, method, data)
end

def matches_text?(link_text)
Expand All @@ -22,13 +22,19 @@ def text
end

protected

def data
authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token}
end

def href
@element["href"]
end

def absolute_href
if href =~ /^\?/
if href =~ %r{^https?://www.example.com(/.*)}
$LAST_MATCH_INFO.captures.first
elsif href =~ /^\?/
"#{@page.url}#{href}"
elsif href !~ /^\//
"#{@page.url}/#{href}"
Expand Down
File renamed without changes.
43 changes: 23 additions & 20 deletions lib/webrat/page.rb → lib/webrat/core/page.rb
@@ -1,3 +1,4 @@
require "rubygems"
require "hpricot"
require "English"

Expand Down Expand Up @@ -99,19 +100,25 @@ def attaches_file(id_or_name_or_label, path)
field.set(path)
end

# Saves the currently loaded page out to RAILS_ROOT/tmp/ and opens it in the default
# Saves the page out to RAILS_ROOT/tmp/ and opens it in the default
# web browser if on OS X. Useful for debugging.
#
# 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 rewrite_css_and_image_references(session.response_body)
end
`open tmp/#{filename}`

open_in_browser(filename)
end

def open_in_browser(path) # :nodoc
`open #{path}`
end

# Issues a request for the URL pointed to by a link on the current page,
Expand Down Expand Up @@ -244,30 +251,21 @@ def find_field(id_or_name_or_label, *field_types)
def request_page(url, method, data)
debug_log "REQUESTING PAGE: #{method.to_s.upcase} #{url} with #{data.inspect}"

session.send "#{method}_via_redirect", url, data || {}
session.send "#{method}", url, data || {}

if response.body =~ /Exception caught/ || response.body.blank?
if session.response_body =~ /Exception caught/ || session.response_body.blank?
save_and_open
end

session.assert_response :success
flunk("Page load was not successful (Code: #{session.response_code.inspect})") unless (200..299).include?(session.response_code)
reset_dom
end

def response
session.response
end

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)
Expand All @@ -284,13 +282,18 @@ def forms

def dom # :nodoc:
return @dom if defined?(@dom) && @dom
flunk("You must visit a path before working with the page.") unless @session.response
@dom = Hpricot(@session.response.body)
flunk("You must visit a path before working with the page.") unless @session.response_code
@dom = Hpricot(@session.response_body)
end

def flunk(message)
raise message
end

def rewrite_css_and_image_references(response_html) # :nodoc
return response_html unless session.doc_root
response_html.gsub(/"\/(stylesheets|images)/, session.doc_root + '/\1')
end

end
end
File renamed without changes.
41 changes: 41 additions & 0 deletions lib/webrat/core/session.rb
@@ -0,0 +1,41 @@
module Webrat
class Session

def doc_root
nil
end

def saved_page_dir
File.expand_path(".")
end

def current_page
@current_page ||= Page.new(self)
end

def current_page=(new_page)
@current_page = new_page
end

def visits(*args)
Page.new(self, *args)
end

def respond_to?(name)
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)
else
super
end
end

end
end
5 changes: 5 additions & 0 deletions lib/webrat/mechanize.rb
@@ -0,0 +1,5 @@
require "rubygems"
require "mechanize"

require File.dirname(__FILE__) + "/mechanize/mechanize_session"

25 changes: 25 additions & 0 deletions 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
3 changes: 3 additions & 0 deletions lib/webrat/rails.rb
@@ -0,0 +1,3 @@
require File.dirname(__FILE__) + "/rails/redirect_actions"
require File.dirname(__FILE__) + "/rails/rails_session"
require File.dirname(__FILE__) + "/rails/session"

0 comments on commit 856b6cb

Please sign in to comment.