Skip to content

Commit

Permalink
Save and open page directory specified via configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
noahd1 committed Jan 17, 2010
1 parent db168be commit 71dcfb3
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 21 deletions.
4 changes: 4 additions & 0 deletions History.txt
@@ -1,3 +1,7 @@
* Minor enhancements

* Save and open page directory specified via configuration (Noah Davis)

* Bug fixes

* Make "should contain" ignore extra whitespace when doing string comparisons (Noah Davis)
Expand Down
4 changes: 0 additions & 4 deletions lib/webrat/adapters/rails.rb
Expand Up @@ -15,10 +15,6 @@ def doc_root
File.expand_path(File.join(RAILS_ROOT, 'public'))
end

def saved_page_dir
File.expand_path(File.join(RAILS_ROOT, "tmp"))
end

def get(url, data, headers = nil)
do_request(:get, url, data, headers)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/webrat/core/configuration.rb
Expand Up @@ -26,6 +26,9 @@ class Configuration
# Save and open pages with error status codes (500-599) in a browser? Defualts to true.
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.
attr_accessor :application_environment
webrat_deprecate :selenium_environment, :application_environment
Expand Down Expand Up @@ -60,6 +63,7 @@ class Configuration

def initialize # :nodoc:
self.open_error_files = true
self.saved_pages_dir = File.expand_path(".")
self.application_environment = :test
self.application_port = 3001
self.application_address = 'localhost'
Expand Down
8 changes: 2 additions & 6 deletions lib/webrat/core/save_and_open_page.rb
Expand Up @@ -6,9 +6,9 @@ module SaveAndOpenPage
# Example:
# 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|
f.write rewrite_css_and_image_references(response_body)
Expand All @@ -29,10 +29,6 @@ def rewrite_css_and_image_references(response_html) # :nodoc:
response_html.gsub(/("|')\/(stylesheets|images)/, '\1' + doc_root + '/\2')
end

def saved_page_dir #:nodoc:
File.expand_path(".")
end

def doc_root #:nodoc:
nil
end
Expand Down
4 changes: 2 additions & 2 deletions lib/webrat/selenium/selenium_session.rb
Expand Up @@ -195,9 +195,9 @@ def selenium


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?
$browser.capture_entire_page_screenshot(filename, '')
Expand Down
5 changes: 5 additions & 0 deletions spec/private/core/configuration_spec.rb
Expand Up @@ -17,6 +17,11 @@
config.should open_error_files
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
config = Webrat::Configuration.new
config.infinite_redirect_limit.should == 10
Expand Down
4 changes: 0 additions & 4 deletions spec/private/rails/rails_adapter_spec.rb
Expand Up @@ -76,10 +76,6 @@
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
Webrat::RailsAdapter.new(mock("integration session")).should respond_to(:doc_root)
end
Expand Down
13 changes: 8 additions & 5 deletions spec/public/save_and_open_spec.rb
Expand Up @@ -22,10 +22,17 @@
Launchy::Browser.stub!(:run)

@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)
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
@file_handle.should_receive(:write) do |html|
html.should =~ %r|"#{webrat_session.doc_root}/stylesheets/foo.css"|s
Expand Down Expand Up @@ -63,8 +70,4 @@
end.should_not raise_error
end

def filename
File.expand_path("./webrat-#{Time.now}.html")
end

end

0 comments on commit 71dcfb3

Please sign in to comment.