From d620e66bd8f4f763d2e52c1a5be11539c30aebfc Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 14 Jun 2009 21:38:04 -0400 Subject: [PATCH] Use Launchy to handle opening pages in the browser with cross-platform compatibility --- History.txt | 1 + lib/webrat/core/save_and_open_page.rb | 10 ++++------ spec/private/core/session_spec.rb | 21 --------------------- spec/public/save_and_open_spec.rb | 16 +++++++++++++--- 4 files changed, 18 insertions(+), 30 deletions(-) diff --git a/History.txt b/History.txt index 008a386..0bee8e8 100644 --- a/History.txt +++ b/History.txt @@ -8,6 +8,7 @@ * Minor enhancements + * Use Launchy to handle opening pages in the browser with cross-platform compatibility (Bryan Helmkamp) * Added support for field_labeled_locators ending in non word characters lh 148 (Zach Dennis) * Filled in tests on click link lh 195 (diabolo) diff --git a/lib/webrat/core/save_and_open_page.rb b/lib/webrat/core/save_and_open_page.rb index 0b4aef6..44264e9 100644 --- a/lib/webrat/core/save_and_open_page.rb +++ b/lib/webrat/core/save_and_open_page.rb @@ -18,12 +18,10 @@ module Webrat 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 + require "launchy" + Launchy::Browser.run(path) + rescue LoadError + warn "Sorry, you need to install launchy to open pages: `gem install launchy`" end def rewrite_css_and_image_references(response_html) # :nodoc: diff --git a/spec/private/core/session_spec.rb b/spec/private/core/session_spec.rb index 7f20af8..f53af09 100644 --- a/spec/private/core/session_spec.rb +++ b/spec/private/core/session_spec.rb @@ -21,27 +21,6 @@ describe Webrat::Session do session.should respond_to(:current_dom) end - it "should open the page in the browser in MacOSX" do - session = Webrat::Session.new - session.stub!(:ruby_platform => 'darwin') - session.should_receive(:`).with("open path") - session.open_in_browser("path") - end - - it "should open the page in the browser in cygwin" do - session = Webrat::Session.new - session.stub!(:ruby_platform => 'i386-cygwin') - session.should_receive(:`).with("rundll32 url.dll,FileProtocolHandler path\\to\\file") - session.open_in_browser("path/to/file") - end - - it "should open the page in the browser in Win32" do - session = Webrat::Session.new - session.stub!(:ruby_platform => 'win32') - session.should_receive(:`).with("rundll32 url.dll,FileProtocolHandler path\\to\\file") - session.open_in_browser("path/to/file") - end - it "should provide a current_page for backwards compatibility" do session = Webrat::Session.new current_page = session.current_page diff --git a/spec/public/save_and_open_spec.rb b/spec/public/save_and_open_spec.rb index 150a6e1..7adba92 100644 --- a/spec/public/save_and_open_spec.rb +++ b/spec/public/save_and_open_spec.rb @@ -17,7 +17,9 @@ describe "save_and_open_page" do File.stub!(:exist? => true) Time.stub!(:now => 1234) - webrat_session.stub!(:open_in_browser) + + require "launchy" + Launchy::Browser.stub!(:run) @file_handle = mock("file handle") File.stub!(:open).with(filename, 'w').and_yield(@file_handle) @@ -48,11 +50,19 @@ describe "save_and_open_page" do save_and_open_page end - it "should open the temp file in a browser" do - webrat_session.should_receive(:open_in_browser).with(filename) + it "should open the temp file in a browser with Launchy" do + Launchy::Browser.should_receive(:run) save_and_open_page end + it "should fail gracefully if Launchy is not available" do + Launchy::Browser.should_receive(:run).and_raise(LoadError) + + lambda do + save_and_open_page + end.should_not raise_error + end + def filename File.expand_path("./webrat-#{Time.now}.html") end