Skip to content

Commit

Permalink
Upaded to webrat newest
Browse files Browse the repository at this point in the history
Signed-off-by: gaffo <mike@uberu.com>
  • Loading branch information
gaffo committed Jan 4, 2009
2 parents fe5d183 + e19b1cc commit 0f0dab0
Show file tree
Hide file tree
Showing 104 changed files with 1,914 additions and 232 deletions.
1 change: 1 addition & 0 deletions History.txt
Expand Up @@ -21,6 +21,7 @@

* Minor enhancements
* Added assert_contain, assert_not_contain [#86] (Mike Gaffney)
* Add configuration options for the Selenium environment and port (Kieran Pilkington)
* Maximize the browser window after initializing Selenium (Luke Melia)
* Better inspect output for Webrat elements
* Sets the Webrat mode with Configuration#mode= in the config block [#85] (Mike Gaffney)
Expand Down
43 changes: 35 additions & 8 deletions Rakefile
@@ -1,4 +1,4 @@
require 'rubygems'
# require 'rubygems'
require "rake/gempackagetask"
require 'rake/rdoctask'
require "rake/clean"
Expand Down Expand Up @@ -28,8 +28,8 @@ spec = Gem::Specification.new do |s|
s.extra_rdoc_files = %w(README.rdoc MIT-LICENSE.txt)

# Dependencies
s.add_dependency "nokogiri", ">= 1.0.6"
s.add_dependency "nokogiri", ">= 1.1.0"

s.rubyforge_project = "webrat"
end

Expand All @@ -52,13 +52,13 @@ end
desc "Run API and Core specs"
Spec::Rake::SpecTask.new do |t|
t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
t.spec_files = FileList['spec/**/*_spec.rb']
t.spec_files = FileList['spec/public/**/*_spec.rb'] + FileList['spec/private/**/*_spec.rb']
end

desc "Run all specs in spec directory with RCov"
Spec::Rake::SpecTask.new(:rcov) do |t|
t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
t.spec_files = FileList['spec/**/*_spec.rb']
t.spec_files = FileList['spec/public/**/*_spec.rb'] + FileList['spec/private/**/*_spec.rb']
t.rcov = true
t.rcov_opts = lambda do
IO.readlines(File.dirname(__FILE__) + "/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
Expand All @@ -71,8 +71,8 @@ end

desc 'Install the package as a gem.'
task :install_gem => [:clean, :package] do
gem = Dir['pkg/*.gem'].first
sh "sudo gem install --local #{gem}"
gem_filename = Dir['pkg/*.gem'].first
sh "sudo gem install --local #{gem_filename}"
end

desc "Delete generated RDoc"
Expand All @@ -99,6 +99,33 @@ task :spec_deps do
end
end

task :prepare do
system "ln -s ../../../../.. ./spec/integration/rails/vendor/plugins/webrat"
end

namespace :spec do
desc "Run the integration specs"
task :integration => ["integration:rails", "integration:merb"]

namespace :integration do
desc "Run the Rails integration specs"
task :rails do
Dir.chdir "spec/integration/rails" do
result = system "rake test:integration"
raise "Tests failed" unless result
end
end

desc "Run the Merb integration specs"
task :merb do
Dir.chdir "spec/integration/merb" do
result = system "rake spec"
raise "Tests failed" unless result
end
end
end
end

task :default => :spec

task :precommit => ["spec", "spec:jruby"]
task :precommit => ["spec", "spec:jruby", "spec:integration"]
8 changes: 8 additions & 0 deletions lib/webrat/core/configuration.rb
Expand Up @@ -27,9 +27,17 @@ class Configuration
# Save and open pages with error status codes (500-599) in a browser? Defualts to true.
attr_writer :open_error_files

# Which environment should the selenium tests be run in? Defaults to selenium.
attr_accessor :selenium_environment

# Which port should the selenium tests be run on? Defaults to 3001.
attr_accessor :selenium_port

def initialize # :nodoc:
self.open_error_files = true
self.parse_with_nokogiri = !Webrat.on_java?
self.selenium_environment = :selenium
self.selenium_port = 3001
end

def parse_with_nokogiri? #:nodoc:
Expand Down
75 changes: 44 additions & 31 deletions lib/webrat/core/session.rb
Expand Up @@ -8,7 +8,7 @@ module Webrat
# A page load or form submission returned an unsuccessful response code (500-599)
class PageLoadError < WebratError
end

def self.session_class
case Webrat.configuration.mode
when :rails
Expand All @@ -24,32 +24,41 @@ def self.session_class
when :mechanize
MechanizeSession
else
raise WebratError.new("Unknown Webrat mode: #{Webrat.configuration.mode.inspect}")
raise WebratError.new(<<-STR)
Unknown Webrat mode: #{Webrat.configuration.mode.inspect}
Please ensure you have a Webrat configuration block that specifies a mode
in your test_helper.rb, spec_helper.rb, or env.rb (for Cucumber).
For example:
Webrat.configure do |config|
config.mode = :rails
end
STR
end
end

class Session
extend Forwardable
include Logging
include SaveAndOpenPage

attr_reader :current_url
attr_reader :elements

def initialize(context = nil) #:nodoc:
@http_method = :get
@data = {}
@default_headers = {}
@custom_headers = {}
@context = context

reset
end

def current_dom #:nodoc:
current_scope.dom
end

# For backwards compatibility -- removing in 1.0
def current_page #:nodoc:
page = OpenStruct.new
Expand All @@ -58,7 +67,7 @@ def current_page #:nodoc:
page.data = @data
page
end

def doc_root #:nodoc:
nil
end
Expand All @@ -70,7 +79,7 @@ def header(key, value)
def http_accept(mime_type)
header('Accept', Webrat::MIME.mime_type(mime_type))
end

def basic_auth(user, pass)
encoded_login = ["#{user}:#{pass}"].pack("m*")
header('HTTP_AUTHORIZATION', "Basic #{encoded_login}")
Expand All @@ -93,39 +102,45 @@ def request_page(url, http_method, data) #:nodoc:

save_and_open_page if exception_caught? && Webrat.configuration.open_error_files?
raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?

reset

@current_url = url
@http_method = http_method
@data = data


request_page(response.headers["Location"], :get, data) if redirect?

return response
end

def success_code? #:nodoc:
(200..499).include?(response_code)
end

def redirect? #:nodoc:
response_code / 100 == 3
end

def exception_caught? #:nodoc:
response_body =~ /Exception caught/
end

def current_scope #:nodoc:
scopes.last || page_scope
end

# Reloads the last page requested. Note that this will resubmit forms
# and their data.
def reloads
request_page(@current_url, @http_method, @data)
end

webrat_deprecate :reload, :reloads


# Works like click_link, but only looks for the link text within a given selector
#
#
# Example:
# click_link_within "#user_12", "Vote"
def click_link_within(selector, link_text)
Expand All @@ -135,14 +150,14 @@ def click_link_within(selector, link_text)
end

webrat_deprecate :clicks_link_within, :click_link_within

def within(selector)
scopes.push(Scope.from_scope(self, current_scope, selector))
ret = yield(current_scope)
scopes.pop
return ret
end

# Issues a GET request for a page, follows any redirects, and verifies the final page
# load was successful.
#
Expand All @@ -151,7 +166,7 @@ def within(selector)
def visit(url = nil, http_method = :get, data = {})
request_page(url, http_method, data)
end

webrat_deprecate :visits, :visit

# Subclasses can override this to show error messages without html
Expand All @@ -166,25 +181,25 @@ def scopes #:nodoc:
def page_scope #:nodoc:
@_page_scope ||= Scope.from_page(self, response, response_body)
end

def dom
page_scope.dom
end

def xml_content_type?
false
end

def simulate
return if Webrat.configuration.mode == :selenium
yield
end

def automate
return unless Webrat.configuration.mode == :selenium
yield
end

def_delegators :current_scope, :fill_in, :fills_in
def_delegators :current_scope, :set_hidden_field
def_delegators :current_scope, :submit_form
Expand All @@ -199,15 +214,13 @@ def automate
def_delegators :current_scope, :click_area, :clicks_area
def_delegators :current_scope, :click_link, :clicks_link
def_delegators :current_scope, :click_button, :clicks_button
def_delegators :current_scope, :should_see
def_delegators :current_scope, :should_not_see
def_delegators :current_scope, :field_labeled
def_delegators :current_scope, :field_by_xpath
def_delegators :current_scope, :field_with_id
def_delegators :current_scope, :select_option

private

def reset
@elements = {}
@_scopes = nil
Expand Down
9 changes: 0 additions & 9 deletions lib/webrat/merb.rb
Expand Up @@ -42,11 +42,6 @@ def do_request(url, data, headers, method)
:params => (data && data.any?) ? data : nil,
:headers => headers,
:method => method)
follow_redirect
end

def follow_redirect
self.get(@response.headers['Location'], nil, @response.headers) if @response.status == 302
end

end
Expand All @@ -59,10 +54,6 @@ def request(uri, env = {})
@_webrat_session ||= Webrat::MerbSession.new
@_webrat_session.response = @_webrat_session.request(uri, env)
end

def follow_redirect
@_webrat_session.follow_redirect
end
end
end
end
Expand Down

0 comments on commit 0f0dab0

Please sign in to comment.