Save and open page directory specified via configuration

This commit is contained in:
Noah Davis 2010-01-16 19:18:28 -05:00
parent db168bedec
commit 71dcfb327d
8 changed files with 25 additions and 21 deletions

View File

@ -1,3 +1,7 @@
* Minor enhancements
* Save and open page directory specified via configuration (Noah Davis)
* Bug fixes * Bug fixes
* Make "should contain" ignore extra whitespace when doing string comparisons (Noah Davis) * Make "should contain" ignore extra whitespace when doing string comparisons (Noah Davis)

View File

@ -15,10 +15,6 @@ module Webrat
File.expand_path(File.join(RAILS_ROOT, 'public')) File.expand_path(File.join(RAILS_ROOT, 'public'))
end end
def saved_page_dir
File.expand_path(File.join(RAILS_ROOT, "tmp"))
end
def get(url, data, headers = nil) def get(url, data, headers = nil)
do_request(:get, url, data, headers) do_request(:get, url, data, headers)
end end

View File

@ -26,6 +26,9 @@ module Webrat
# Save and open pages with error status codes (500-599) in a browser? Defualts to true. # Save and open pages with error status codes (500-599) in a browser? Defualts to true.
attr_writer :open_error_files attr_writer :open_error_files
# Save and open page storage directory, defaults to current directory
attr_accessor :saved_pages_dir
# Which rails environment should the selenium tests be run in? Defaults to selenium. # Which rails environment should the selenium tests be run in? Defaults to selenium.
attr_accessor :application_environment attr_accessor :application_environment
webrat_deprecate :selenium_environment, :application_environment webrat_deprecate :selenium_environment, :application_environment
@ -60,6 +63,7 @@ module Webrat
def initialize # :nodoc: def initialize # :nodoc:
self.open_error_files = true self.open_error_files = true
self.saved_pages_dir = File.expand_path(".")
self.application_environment = :test self.application_environment = :test
self.application_port = 3001 self.application_port = 3001
self.application_address = 'localhost' self.application_address = 'localhost'

View File

@ -6,9 +6,9 @@ module Webrat
# Example: # Example:
# save_and_open_page # save_and_open_page
def save_and_open_page def save_and_open_page
return unless File.exist?(saved_page_dir) return unless File.exist?(Webrat.configuration.saved_pages_dir)
filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.html" filename = "#{Webrat.configuration.saved_pages_dir}/webrat-#{Time.now.to_i}.html"
File.open(filename, "w") do |f| File.open(filename, "w") do |f|
f.write rewrite_css_and_image_references(response_body) f.write rewrite_css_and_image_references(response_body)
@ -29,10 +29,6 @@ module Webrat
response_html.gsub(/("|')\/(stylesheets|images)/, '\1' + doc_root + '/\2') response_html.gsub(/("|')\/(stylesheets|images)/, '\1' + doc_root + '/\2')
end end
def saved_page_dir #:nodoc:
File.expand_path(".")
end
def doc_root #:nodoc: def doc_root #:nodoc:
nil nil
end end

View File

@ -195,9 +195,9 @@ EOS
def save_and_open_screengrab def save_and_open_screengrab
return unless File.exist?(saved_page_dir) return unless File.exist?(Webrat.configuration.saved_pages_dir)
filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.png" filename = "#{Webrat.configuration.saved_pages_dir}/webrat-#{Time.now.to_i}.png"
if $browser.chrome_backend? if $browser.chrome_backend?
$browser.capture_entire_page_screenshot(filename, '') $browser.capture_entire_page_screenshot(filename, '')

View File

@ -17,6 +17,11 @@ describe Webrat::Configuration do
config.should open_error_files config.should open_error_files
end end
it "should default saved_pages_dir to current dir" do
config = Webrat::Configuration.new
config.saved_pages_dir.should == File.expand_path(".")
end
it "should detect infinite redirects after 10" do it "should detect infinite redirects after 10" do
config = Webrat::Configuration.new config = Webrat::Configuration.new
config.infinite_redirect_limit.should == 10 config.infinite_redirect_limit.should == 10

View File

@ -76,10 +76,6 @@ describe Webrat::RailsAdapter do
end end
end end
it "should provide a saved_page_dir" do
Webrat::RailsAdapter.new(mock("integration session")).should respond_to(:saved_page_dir)
end
it "should provide a doc_root" do it "should provide a doc_root" do
Webrat::RailsAdapter.new(mock("integration session")).should respond_to(:doc_root) Webrat::RailsAdapter.new(mock("integration session")).should respond_to(:doc_root)
end end

View File

@ -22,10 +22,17 @@ describe "save_and_open_page" do
Launchy::Browser.stub!(:run) Launchy::Browser.stub!(:run)
@file_handle = mock("file handle") @file_handle = mock("file handle")
File.stub!(:open).with(filename, 'w').and_yield(@file_handle) File.stub!(:open).and_yield(@file_handle)
@file_handle.stub!(:write) @file_handle.stub!(:write)
end end
it "should save pages to the directory configured" do
Webrat.configuration.stub!(:saved_pages_dir => "path/to/dir")
File.should_receive(:open).with("path/to/dir/webrat-1234.html", "w").and_yield(@file_handle)
save_and_open_page
end
it "should rewrite css rules" do it "should rewrite css rules" do
@file_handle.should_receive(:write) do |html| @file_handle.should_receive(:write) do |html|
html.should =~ %r|"#{webrat_session.doc_root}/stylesheets/foo.css"|s html.should =~ %r|"#{webrat_session.doc_root}/stylesheets/foo.css"|s
@ -63,8 +70,4 @@ describe "save_and_open_page" do
end.should_not raise_error end.should_not raise_error
end end
def filename
File.expand_path("./webrat-#{Time.now}.html")
end
end end