From 7d63aa1a4d3057dd7a51bfd1fbfc88dfe4939fa6 Mon Sep 17 00:00:00 2001 From: Luke Melia Date: Fri, 19 Dec 2008 00:47:26 -0500 Subject: [PATCH] Extracted save_and_open_page related functionality to a module and included it in SeleniumSession as well as the standard webrat session. Also added save_and_open_screengrab method to SeleniumSession. --- lib/webrat/core.rb | 1 + lib/webrat/core/save_and_open_page.rb | 50 +++++++++++++++++++++++++ lib/webrat/core/session.rb | 41 +------------------- lib/webrat/selenium.rb | 5 ++- lib/webrat/selenium/selenium_session.rb | 17 +++++++++ 5 files changed, 74 insertions(+), 40 deletions(-) create mode 100644 lib/webrat/core/save_and_open_page.rb diff --git a/lib/webrat/core.rb b/lib/webrat/core.rb index 26d99623..5d8a57ea 100644 --- a/lib/webrat/core.rb +++ b/lib/webrat/core.rb @@ -11,3 +11,4 @@ require "webrat/core/session" require "webrat/core/methods" require "webrat/core/matchers" +require "webrat/core/save_and_open_page" diff --git a/lib/webrat/core/save_and_open_page.rb b/lib/webrat/core/save_and_open_page.rb new file mode 100644 index 00000000..a3c97f53 --- /dev/null +++ b/lib/webrat/core/save_and_open_page.rb @@ -0,0 +1,50 @@ +module Webrat + module SaveAndOpenPage + # 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_page + def save_and_open_page + return unless File.exist?(saved_page_dir) + + filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.html" + + File.open(filename, "w") do |f| + f.write rewrite_css_and_image_references(response_body) + end + + open_in_browser(filename) + end + + def open_in_browser(path) # :nodoc + platform = ruby_platform + if platform =~ /cygwin/ || platform =~ /win32/ + `rundll32 url.dll,FileProtocolHandler #{path.gsub("/", "\\\\")}` + elsif platform =~ /darwin/ + `open #{path}` + end + end + + def rewrite_css_and_image_references(response_html) # :nodoc: + return response_html unless doc_root + response_html.gsub(/"\/(stylesheets|images)/, doc_root + '/\1') + end + + def saved_page_dir #:nodoc: + File.expand_path(".") + end + + def doc_root #:nodoc: + nil + end + + private + + # accessor for testing + def ruby_platform + RUBY_PLATFORM + end + + end +end \ No newline at end of file diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 0ca3ffa1..fe4f44c0 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -2,6 +2,7 @@ require "ostruct" require "webrat/core/mime" +require "webrat/core/save_and_open_page" module Webrat # A page load or form submission returned an unsuccessful response code (500-599) @@ -30,6 +31,7 @@ def self.session_class class Session extend Forwardable include Logging + include SaveAndOpenPage attr_reader :current_url attr_reader :elements @@ -43,23 +45,6 @@ def initialize(context = nil) #:nodoc: reset end - - # 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_page - def save_and_open_page - return unless File.exist?(saved_page_dir) - - filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.html" - - File.open(filename, "w") do |f| - f.write rewrite_css_and_image_references(response_body) - end - - open_in_browser(filename) - end def current_dom #:nodoc: current_scope.dom @@ -78,10 +63,6 @@ def doc_root #:nodoc: nil end - def saved_page_dir #:nodoc: - File.expand_path(".") - end - def header(key, value) @custom_headers[key] = value end @@ -172,20 +153,6 @@ def visit(url = nil, http_method = :get, data = {}) end webrat_deprecate :visits, :visit - - def open_in_browser(path) # :nodoc - platform = ruby_platform - if platform =~ /cygwin/ || platform =~ /win32/ - `rundll32 url.dll,FileProtocolHandler #{path.gsub("/", "\\\\")}` - elsif platform =~ /darwin/ - `open #{path}` - end - end - - def rewrite_css_and_image_references(response_html) # :nodoc: - return response_html unless doc_root - response_html.gsub(/"\/(stylesheets|images)/, doc_root + '/\1') - end # Subclasses can override this to show error messages without html def formatted_error #:nodoc: @@ -247,9 +214,5 @@ def reset @_page_scope = nil end - # accessor for testing - def ruby_platform - RUBY_PLATFORM - end end end diff --git a/lib/webrat/selenium.rb b/lib/webrat/selenium.rb index 59b56c46..3bf646da 100644 --- a/lib/webrat/selenium.rb +++ b/lib/webrat/selenium.rb @@ -90,7 +90,10 @@ def response def wait_for(*args, &block) webrat_session.wait_for(*args, &block) end - + + def save_and_open_screengrab + webrat_session.save_and_open_screengrab + end end end end diff --git a/lib/webrat/selenium/selenium_session.rb b/lib/webrat/selenium/selenium_session.rb index 2f2c4afb..460269e6 100644 --- a/lib/webrat/selenium/selenium_session.rb +++ b/lib/webrat/selenium/selenium_session.rb @@ -1,3 +1,5 @@ +require "webrat/core/save_and_open_page" + module Webrat class TimeoutError < WebratError end @@ -17,6 +19,7 @@ def selenium end class SeleniumSession + include Webrat::SaveAndOpenPage def initialize(*args) # :nodoc: end @@ -160,6 +163,20 @@ def selenium webrat_deprecate :browser, :selenium + + def save_and_open_screengrab + return unless File.exist?(saved_page_dir) + + filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.png" + + if $browser.chrome_backend? + $browser.capture_entire_page_screenshot(filename, '') + else + $browser.capture_screenshot(filename) + end + open_in_browser(filename) + end + protected def setup #:nodoc: