Use Launchy to handle opening pages in the browser with cross-platform compatibility

This commit is contained in:
Bryan Helmkamp 2009-06-14 21:38:04 -04:00
parent d60671cd3d
commit d620e66bd8
4 changed files with 18 additions and 30 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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

View File

@ -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