Skip to content

Commit

Permalink
Extracted save_and_open_page related functionality to a module and in…
Browse files Browse the repository at this point in the history
…cluded it in SeleniumSession as well as the standard webrat session. Also added save_and_open_screengrab method to SeleniumSession.
  • Loading branch information
lukemelia committed Dec 19, 2008
1 parent f3dfa32 commit 7d63aa1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 40 deletions.
1 change: 1 addition & 0 deletions lib/webrat/core.rb
Expand Up @@ -11,3 +11,4 @@
require "webrat/core/session"
require "webrat/core/methods"
require "webrat/core/matchers"
require "webrat/core/save_and_open_page"
50 changes: 50 additions & 0 deletions 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
41 changes: 2 additions & 39 deletions lib/webrat/core/session.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -30,6 +31,7 @@ def self.session_class
class Session
extend Forwardable
include Logging
include SaveAndOpenPage

attr_reader :current_url
attr_reader :elements
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -247,9 +214,5 @@ def reset
@_page_scope = nil
end

# accessor for testing
def ruby_platform
RUBY_PLATFORM
end
end
end
5 changes: 4 additions & 1 deletion lib/webrat/selenium.rb
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions lib/webrat/selenium/selenium_session.rb
@@ -1,3 +1,5 @@
require "webrat/core/save_and_open_page"

module Webrat
class TimeoutError < WebratError
end
Expand All @@ -17,6 +19,7 @@ def selenium
end

class SeleniumSession
include Webrat::SaveAndOpenPage

def initialize(*args) # :nodoc:
end
Expand Down Expand Up @@ -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:
Expand Down

2 comments on commit 7d63aa1

@bmabey
Copy link
Contributor

@bmabey bmabey commented on 7d63aa1 Dec 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome, thanks for adding it! I was just wishing for something just like this today…

@lukemelia
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No prob, I have this crazy idea of being able to make a little movie from a story, perhaps with the story text as subtitles…

Please sign in to comment.